Sha256: 6cefcf61ffb5bda3c4d479da17a400f8277e67622d6e35a3fe40aaa5c5aee866

Contents?: true

Size: 1.56 KB

Versions: 7

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'

DEFAULT_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, config, username, password)
    @config_file = config
    `mkdir #{PLUGINS}` unless Dir.exist? PLUGINS
    @arg_bucket = {}
    set 'username', username unless username.nil?
    set 'password', password unless password.nil?
    @config = load_config @config_file
    import @config['vars']
    @macro = @config['macros'][macro_name]
    if @macro.nil? then fail "Command: '#{macro_name}' is not defined." end
  end

  def load_config(config)
    if config.nil? then config = DEFAULT_CONFIG end
    YAML.load_file config
  end

  def go
    @macro.each_pair do |_key, value|
      call = Call.new(value, @arg_bucket)
      @arg_bucket = call.run
      if !value['record'].nil?
        value['record'].each do |var|
          add_config_var var, get(var) unless var=='function'
        end
      end
    end
  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_file, 'w'
    file.write(@config.to_yaml.gsub("\n-", "\n\n-"))
    file.close
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
apidragon-1.6.1 lib/apidragon/api.rb
apidragon-1.6.0 lib/apidragon/api.rb
apidragon-1.5.4 lib/apidragon/api.rb
apidragon-1.5.3 lib/apidragon/api.rb
apidragon-1.5.2 lib/apidragon/api.rb
apidragon-1.5.1 lib/apidragon/api.rb
apidragon-1.5.0 lib/apidragon/api.rb