Sha256: ac6b63197a6798ba5ae4a38effb3ffb00a55abbdb31ad08f3df8afff5299fd22

Contents?: true

Size: 1.56 KB

Versions: 4

Compression:

Stored size: 1.56 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'

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.
  attr_accessor :macro

  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 macro_init
    macro = @macro
    @macro = []
    macro.each_pair do |key, value|
      @macro << Call.new(value['input'], value['output'], value['mode'], value['function'], @arg_bucket)
    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)
      @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

4 entries across 4 versions & 1 rubygems

Version Path
apidragon-1.0.0 lib/apidragon/api.rb
apidragon-0.1.2 lib/apidragon/api.rb
apidragon-0.1.1 lib/apidragon/api.rb
apidragon-0.1.0 lib/apidragon/api.rb