Class: Songkick::Transport::Curb

Inherits:
Base
  • Object
show all
Defined in:
lib/songkick/transport/curb.rb

Constant Summary collapse

DEFAULT_HEADERS =

Send this so that curb will not implement Section 8.2.3 of RFC 2616, which our Ruby HTTP servers are not equipped to respond to. (If not sent it causes an additional 1s of latency for all requests with post data longer than 1k)

{"Expect" => ""}

Instance Attribute Summary

Attributes inherited from Base

#user_agent, #user_error_codes

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#do_verb, #with_headers, #with_timeout

Constructor Details

#initialize(host, options = {}) ⇒ Curb

Returns a new instance of Curb.



18
19
20
21
22
23
24
25
26
# File 'lib/songkick/transport/curb.rb', line 18

def initialize(host, options = {})
  @host       = host
  @timeout    = options[:timeout] || DEFAULT_TIMEOUT
  @user_agent = options[:user_agent]
  @user_error_codes = options[:user_error_codes] || DEFAULT_USER_ERROR_CODES
  if c = options[:connection]
    Thread.current[:transport_curb_easy] = c
  end
end

Class Method Details

.clear_thread_connectionObject



14
15
16
# File 'lib/songkick/transport/curb.rb', line 14

def self.clear_thread_connection
   Thread.current[:transport_curb_easy] = nil
end

Instance Method Details

#connectionObject



28
29
30
# File 'lib/songkick/transport/curb.rb', line 28

def connection
  Thread.current[:transport_curb_easy] ||= Curl::Easy.new
end

#endpointObject



32
33
34
# File 'lib/songkick/transport/curb.rb', line 32

def endpoint
  @host
end

#execute_request(req) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/songkick/transport/curb.rb', line 36

def execute_request(req)
  connection.reset
  
  connection.url     = req.url
  timeout            = req.timeout || @timeout
  connection.timeout = timeout
  connection.headers.update(DEFAULT_HEADERS.merge(req.headers))
  
  response_headers = {}
  
  connection.on_header do |header_line|
    line = header_line.sub(/\r\n$/, '')
    parts = line.split(/:\s*/)
    if parts.size >= 2
      response_headers[parts.shift] = parts * ':'
    end
    header_line.bytesize
  end
  
  if req.use_body?
    connection.__send__("http_#{req.verb}", req.body)
  else
    connection.http(req.verb.upcase)
  end

  process(req, connection.response_code, response_headers, connection.body_str)

rescue Curl::Err::HostResolutionError => error
  logger.warn "Could not resolve host: #{@host}"
  raise Transport::HostResolutionError, req

rescue Curl::Err::ConnectionFailedError => error
  logger.warn "Could not connect to host: #{@host}"
  raise Transport::ConnectionFailedError, req

rescue Curl::Err::TimeoutError => error
  logger.warn "Request timed out after #{timeout}s : #{req}"
  raise Transport::TimeoutError, req

rescue Curl::Err::GotNothingError => error
  logger.warn "Got nothing: #{req}"
  raise Transport::UpstreamError, req
end