require_relative 'parser' require_relative 'argbucket' require_relative 'log' class Call < ArgBucket def initialize(input, output, mode, function, args, logging=false, plugin=nil) @arg_bucket = args @plugin = 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 username = get 'username' password = get 'password' check_constraints mode, function @function = function credential_sub username, password @mode = mode @output = output @input = input @logging = logging set_call_args unless input.nil? end def check_constraints(mode, function) 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 puts "WARNING: 'function' should have basic auth. (example syntax= function: username:password@example.com/apicall)" unless function.include?('username') && function.include?('password') || @mode == 'eval' end def credential_sub(username, password) @function.gsub!('username', username) unless username.nil? @function.gsub!('password', password) unless password.nil? end def set_call_args input = @input @input = {} input.each do |arg| value = get arg if arg.nil? then puts 'WARNING: required argument not set.' end @input[arg] = value end end def run begin case @mode when 'get' puts "making a get: #{@function} with #{@input}" @response = RestClient.get @function, params: @input when 'post' puts "making a post: #{@function} with #{@input}" @response = RestClient.post @function, params: @input when 'put' puts "making a put: #{@function} with #{@input}" @response = RestClient.put @function, params: @input when 'delete' puts "making a delete: #{@function} with #{@input}" @response = RestClient.delete @function, params: @input when 'curl_get' command = "curl -v --url #{@function}" @input.each_pair do |key, value| command << " -F #{key}='#{value}'" end @response = `#{command}` when 'curl_post' command = "curl -v -X POST --url #{@function}" @input.each_pair do |key, value| command << " -F #{key}='#{value}'" end @response = `#{command}` else puts "#{@mode} not supported." end rescue RestClient::Unauthorized puts 'Username or password was invalid.' rescue RestClient::Exception puts 'Internal Error or requested resource was not found.' end if !@response.nil? run_plugin_script unless @plugin.nil? log_response unless @logging == false parse_response unless @output.nil? end return @arg_bucket end def run_plugin_script if !@response.is_a? String custom = Plugin.new(@response.body).run else custom = Plugin.new(@response).run end end def log_response logger = ResponseLogger.new if !@response.is_a? String logger.object_log @function, @response.code, @response.body else logger.string_log @function, @response end end def xml_to_json @response = Hash.from_xml(@response).to_json unless !@response.include?('