Class: Api

Inherits:
ArgBucket show all
Defined in:
lib/apidragon/api.rb

Instance Attribute Summary

Attributes inherited from ArgBucket

#arg_bucket

Instance Method Summary collapse

Methods inherited from ArgBucket

#get, #set

Constructor Details

#initialize(macro_name, config, username, password) ⇒ Api

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.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/apidragon/api.rb', line 17

def initialize(macro_name, config, username, password)
  @config_file = config
  Dir.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

Instance Method Details

#add_config_var(key, value) ⇒ Object



52
53
54
55
56
57
# File 'lib/apidragon/api.rb', line 52

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

#goObject



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/apidragon/api.rb', line 34

def go
  @macro.each_pair do |_key, value|
    call = Call.new(value, @arg_bucket)
    @arg_bucket = call.run
    unless value['record'].nil?
      value['record'].each do |var|
        if value['record'].include?(var) then add_config_var var, get(var) end
      end
    end
  end
end

#import(args) ⇒ Object



46
47
48
49
50
# File 'lib/apidragon/api.rb', line 46

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

#load_config(config) ⇒ Object



29
30
31
32
# File 'lib/apidragon/api.rb', line 29

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