Class: Songkick::Transport::HttParty::Adapter

Inherits:
Base
  • Object
show all
Includes:
HTTParty
Defined in:
lib/songkick/transport/httparty.rb

Instance Attribute Summary

Attributes inherited from Base

#user_agent, #user_error_codes

Instance Method Summary collapse

Methods inherited from Base

#do_verb, #with_headers, #with_timeout

Instance Method Details

#endpointObject



23
24
25
# File 'lib/songkick/transport/httparty.rb', line 23

def endpoint
  self.class.base_uri
end

#execute_request(req) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/songkick/transport/httparty.rb', line 27

def execute_request(req)
  timeout = req.timeout || self.class.default_options[:timeout]
  
  response = if req.use_body?
    self.class.__send__(req.verb, req.path, :body => req.body, :headers => req.headers, :timeout => timeout)
  else
    self.class.__send__(req.verb, req.url, :headers => req.headers, :timeout => timeout)
  end
  
  process(req, response.code, response.headers, response.parsed_response)

rescue SocketError => error
  logger.warn "Could not connect to host: #{self.class.base_uri}"
  raise ConnectionFailedError, req

rescue Timeout::Error => error
  logger.warn "Request timed out: #{req}"
  raise Transport::TimeoutError, req

rescue UpstreamError => error
  raise error

rescue Object => error
  if error.class.name =~ /json/i or error.message =~ /json/i
    logger.warn("Request returned invalid JSON: #{req}")
    raise Transport::InvalidJSONError, req
  else
    logger.warn("Error trying to call #{req}: #{error.class}: #{error.message}")
    raise UpstreamError, req
  end
end