require_relative 'parser' require_relative 'argbucket' class Call < ArgBucket attr_accessor :input def initialize(input, output, mode, function, args) @arg_bucket = args username = get 'username' password = get 'password' check_constraints mode, function @function = "https://#{function}" credential_sub username, password @mode = mode @output = output @input = input 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') 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' @response = RestClient.post @function, params: @input when 'put' puts 'making a put' when 'delete' puts 'making a delete' when 'curl_get' command = "curl --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 log_response unless @response.nil? parse_response unless @response.nil? || @output.nil? return @arg_bucket end def log_response # puts @response.code # puts @response.body puts @response end def xml_to_json @response = Hash.from_xml(@response).to_json unless !@response.include?('