# # Lifted from eDist Gateway::HTTP::Client # require 'httparty' module Daengine module HTTP module Client SLOW_CALL_THRESHOLD = 5 # Available options # :timeout => Timeout is specified in seconds. # :headers - contains string hash such has content-type: {'content-type' => 'application/xml'} # :body => Body to post. # def self.call(path, options = {}) begin method = options.delete(:method) || :get htoptions = {} htoptions[:timeout] = options.delete(:timeout) || 10 headers = options.delete(:headers) || {} headers['rake_task_name'] = (Thread.current[:rake_task_name] || 'unknown').to_s headers['visitor_id'] = (Thread.current[:visitor_id] || 'unknown').to_s headers['uuid'] ||= (Thread.current[:uuid] || 'unknown').to_s htoptions[:headers] = headers case method when :get query = options[:query] || options[:parameters] || options htoptions[:query] = query unless query.blank? when :post, :put, :delete htoptions[:query] = options[:query] if options[:query] body = options[:body] || options[:parameters] || options htoptions[:body] = body else raise ArgumentError.new('you must specify a method of either :get, :post, :put, or :delete') end start = Time.now resp = HTTParty.send(method.to_sym, path, htoptions) elapsed = Time.now - start Daengine.log("***** Daengine::HTTP::Client.call(#{method}, #{path}, #{htoptions}) TOOK #{elapsed} seconds +++++++++++++++++++++", elapsed > SLOW_CALL_THRESHOLD ? 'warn' : 'info') rescue => ex Daengine.log("Error calling service (#{path}) - #{ex.inspect}\n#{ex.backtrace.join("\n\t")}", 'error') #raise ex end Response.new(resp) end end class Response def initialize(response) @resp = response end def headers @resp.headers end def body @resp.body end def status @resp.code end def as_hash @resp.parsed_response end alias :code :status alias :status_code :status def success? !!(status.to_s =~ /^2/) end def failed? !success? end def server_error? !!(status.to_s =~ /^5/) end def client_error? !!(status.to_s =~ /^4/) end def redirection? !!(status.to_s =~ /^3/) end end end end