Sha256: 15597285c19b9416caebe5a0d98dca7ef9dea8a68ff0923aa38b1f0ee3e92720

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 KB

Contents

require 'json'
require 'active_support/core_ext/hash'
require 'rest-client'
require 'yaml'
require_relative 'macro'
require_relative 'argbucket'

CONFIG = '/tmp/apidragonconf.yaml'
PLUGINS = '/tmp/apidragonplugins/'
LOG = '/tmp/apidragon.log'

class Api < ArgBucket
  # Uses the configuration data in config.yaml to run a sequence of api calls.
  # All variables are dumped into a variable bucket (@arg_bucket)
  # so that they can be used as parameters for function calls.

  def initialize(macro_name)
    @arg_bucket = {}
    @config = load_config
    import @config['vars']
    @macro = @config['macros'][macro_name]
    if @macro.nil? then fail "Command: '#{macro_name}' is not defined." end
  end

  def load_config
    YAML.load_file CONFIG
  end

  def go
    @macro.each_pair do |key, value|
      call = Call.new(value['input'], value['output'], value['mode'], value['function'], @arg_bucket, value['logging'], value['plugin'])
      @arg_bucket = call.run
      if !value['record'].nil?
        value['record'].each do |var|
          add_config_var var, get(var)
        end
      end
    end
  end

  def rest_get_request(url, **params)
    RestClient.get url, params: params
  end

  def import(args)
    args.each_pair do |key, value|
      set key, value
    end
  end

  def add_config_var(key,value)
    @config['vars'][key] = value
    file = File.open CONFIG, 'w'
    file.write(@config.to_yaml.gsub("\n-", "\n\n-"))
    file.close
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
apidragon-1.2.0 lib/apidragon/api.rb