lib/communication.rb in mxhero-api-1.2.8 vs lib/communication.rb in mxhero-api-1.2.9

- old
+ new

@@ -17,22 +17,24 @@ def call(method, url, body = nil, more_options = {}) unless @client @client ||= HTTPClient.new(:force_basic_auth => true) end @client.set_auth(url, @username, @password) - response = @client.request(method, url, nil, body, headers) + response = @client.request(method, url, nil, body, headers(more_options)) raise "Unauthorized" if response.status == 401 unless more_options[:throw_exception] == false raise "An error ocurred when try to communicate with the API\nError: #{response.inspect}" if (500..600).include?(response.status) end response end # Default headers - def headers - { - 'Accept' => 'application/json', - 'Content-Type' => 'application/json', + def headers(options = {}) + accept = options.fetch(:accept, 'application/json') + content_type = options.fetch(:content_type, 'application/json') + return { + 'Accept' => accept, + 'Content-Type' => content_type, "Authorization" => "Basic " + ::Base64.encode64(@username + ':' + @password).gsub("\n",''), }.merge(@as_user ? { "X-MxHero-As-User" => @as_user } : {}) end # @return [Hash]