Class: Call
Instance Attribute Summary
Attributes inherited from ArgBucket
Instance Method Summary collapse
- #check_constraints ⇒ Object
- #credential_sub(username, password) ⇒ Object
- #curl_request(command) ⇒ Object
-
#initialize(value, args) ⇒ Call
constructor
A new instance of Call.
- #log_response ⇒ Object
- #parse_response ⇒ Object
- #rest_request ⇒ Object
- #run ⇒ Object
- #set_call_args(input) ⇒ Object
- #xml_to_json ⇒ Object
Methods inherited from ArgBucket
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_constraints ⇒ Object
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
75 76 77 78 79 80 |
# File 'lib/apidragon/macro.rb', line 75 def curl_request(command) @input.each_pair do |key, value| command << " -F '#{key}=#{value}'" end @response = `#{command}` end |
#log_response ⇒ Object
96 97 98 99 |
# File 'lib/apidragon/macro.rb', line 96 def log_response logger = ResponseLogger.new logger.object_log @code, @response end |
#parse_response ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/apidragon/macro.rb', line 105 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_request ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/apidragon/macro.rb', line 82 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 |
#run ⇒ Object
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 |
# 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 when 'curl_put' command = "curl -X PUT --url #{@function} -d #{@data}" curl_request command when 'plugin' Object.const_get(@plugin).new(@arg_bucket).run 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_json ⇒ Object
101 102 103 |
# File 'lib/apidragon/macro.rb', line 101 def xml_to_json @response = Hash.from_xml(@response).to_json unless !@response.include?('<?xml') end |