Module: Daengine::HTTP::Client

Defined in:
lib/daengine/http_client.rb

Constant Summary collapse

SLOW_CALL_THRESHOLD =
5

Class Method Summary collapse

Class Method Details

.call(path, options = {}) ⇒ Object

Available options

:timeout => Timeout is specified in seconds.
:headers - contains string hash such has content-type: {'content-type' => 'application/xml'}
:body => Body to post.


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/daengine/http_client.rb', line 15

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