Class: Call

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

Instance Attribute Summary

Attributes inherited from ArgBucket

#arg_bucket

Instance Method Summary collapse

Methods inherited from ArgBucket

#get, #set

Constructor Details

#initialize(value, args) ⇒ Call

Returns a new instance of Call.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/apidragon/macro.rb', line 7

def initialize(value, args)
  @arg_bucket = args
  @plugin = value['plugin']
  begin
    require_relative "#{PLUGINS}#{@plugin}" unless @plugin.nil?
  rescue LoadError
    puts 'Plugin failed to load. Custom functions will not be run.'
    @plugin = nil
  end
  @function = "#{value['function']}"
  credential_sub get('username'), get('password')
  @mode = "#{value['mode']}"
  check_constraints
  @output = value['output']
  @logging = value['logging']
  @stdout = value['stdout']
  set_call_args value['input'] unless value['input'].nil?
end

Instance Method Details

#check_constraintsObject



26
27
28
29
# File 'lib/apidragon/macro.rb', line 26

def check_constraints
  if @mode.nil? then fail '"mode" not set. syntax= mode: get/post/put/delete.' end
  if @function.nil? then fail '"function" not set. syntax= function: api-url.' end
end

#credential_sub(username, password) ⇒ Object



31
32
33
34
# File 'lib/apidragon/macro.rb', line 31

def credential_sub(username, password)
  @function.gsub!('username', username) unless username.nil?
  @function.gsub!('password', password) unless password.nil?
end

#curl_request(command) ⇒ Object



77
78
79
80
81
82
# File 'lib/apidragon/macro.rb', line 77

def curl_request(command)
  @input.each_pair do |key, value|
    command << " -F '#{key}=#{value}'"
  end
  @response = `#{command}`
end

#log_responseObject



98
99
100
101
# File 'lib/apidragon/macro.rb', line 98

def log_response
  logger = ResponseLogger.new
  logger.object_log @code, @response
end

#parse_responseObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/apidragon/macro.rb', line 107

def parse_response
  @output.each do |var|
    if var.is_a? String
      parser = Parser.new(var, nil, @response)
      parser.parse
      value = parser.output
      set "#{var}", value
    elsif var.is_a? Hash
      parser = Parser.new(var.first[0], get(var.first[1]), @response)
      parser.parse
      value = parser.output
      set var.first[0], value
    end
  end
end

#rest_requestObject



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/apidragon/macro.rb', line 84

def rest_request
  @response = RestClient::Request.execute(method: @mode, url: @function, headers: {params: @input}) { |response, request, result, &block|
    if [301, 302, 307].include? response.code
      response.follow_redirection(request, result, &block)
    else
      response.return!(request, result, &block)
    end
  }
  rescue RestClient::Unauthorized
    puts 'Username or password was invalid.'
  rescue RestClient::Exception
    puts 'Internal Error or requested resource was not found.'
end

#runObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/apidragon/macro.rb', line 45

def run
  case @mode
    when 'get', 'put', 'post', 'delete'
      rest_request
    when 'curl_get'
      command = "curl --url #{@function}"
      curl_request command
    when 'curl_post'
      command = "curl -X POST --url #{@function}"
      curl_request command
      @response = `#{command}`
    when 'plugin'
      begin 
        Object.const_get(@plugin).new(@arg_bucket).run
      rescue
        puts 'Plugin initialization failed. Class or methods may be incorrectly defined.'
      end
    else
      puts "#{@mode} not supported."
  end
  if !@response.nil?
    @code = @response.code unless @response.is_a? String
    xml_to_json
    if @logging == true then log_response end
    @response = JSON.parse @response
    if @stdout == true then puts JSON.pretty_generate @response end
    parse_response unless @output.nil?
    set 'response', @response
  end
  return @arg_bucket
end

#set_call_args(input) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/apidragon/macro.rb', line 36

def set_call_args(input)
  @input = {}
  input.each do |arg|
    value = get arg
    if arg.nil? then fail "Required argument '#{arg}' not defined." end
    @input[arg] = value
  end
end

#xml_to_jsonObject



103
104
105
# File 'lib/apidragon/macro.rb', line 103

def xml_to_json
  @response = Hash.from_xml(@response).to_json unless !@response.include?('<?xml')
end