Class: Songkick::Transport::Base

Inherits:
Object
  • Object
show all
Includes:
API
Defined in:
lib/songkick/transport/base.rb

Direct Known Subclasses

Curb, HttParty::Adapter, RackTest

Defined Under Namespace

Modules: API Classes: BasicAuthDecorator, HeaderDecorator, ParamsDecorator, TimeoutDecorator

Constant Summary collapse

DEFAULT_INSTRUMENTATION_LABEL =
'http.songkick_transport'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from API

#delete, #get, #head, #options, #patch, #post, #put, #with_basic_auth, #with_headers, #with_params, #with_timeout

Constructor Details

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

Returns a new instance of Base.



58
59
60
61
62
63
64
65
66
# File 'lib/songkick/transport/base.rb', line 58

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
  @instrumenter ||= options[:instrumenter]
  @instrumentation_label = options[:instrumentation_label] || DEFAULT_INSTRUMENTATION_LABEL
  @basic_auth = options[:basic_auth]
end

Instance Attribute Details

#basic_authObject (readonly)

Returns the value of attribute basic_auth.



54
55
56
# File 'lib/songkick/transport/base.rb', line 54

def basic_auth
  @basic_auth
end

#hostObject (readonly) Also known as: endpoint

Returns the value of attribute host.



54
55
56
# File 'lib/songkick/transport/base.rb', line 54

def host
  @host
end

#instrumenterObject (readonly)

Returns the value of attribute instrumenter.



54
55
56
# File 'lib/songkick/transport/base.rb', line 54

def instrumenter
  @instrumenter
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



54
55
56
# File 'lib/songkick/transport/base.rb', line 54

def timeout
  @timeout
end

#user_agentObject

Returns the value of attribute user_agent.



53
54
55
# File 'lib/songkick/transport/base.rb', line 53

def user_agent
  @user_agent
end

#user_error_codesObject

Returns the value of attribute user_error_codes.



53
54
55
# File 'lib/songkick/transport/base.rb', line 53

def user_error_codes
  @user_error_codes
end

Instance Method Details

#do_verb(verb, path, params = {}, head = {}, timeout = nil) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/songkick/transport/base.rb', line 68

def do_verb(verb, path, params = {}, head = {}, timeout = nil)
  auth_headers = basic_auth ? Authentication.basic_auth_headers(basic_auth) : {}
  req = Request.new(endpoint, verb, path, params, headers.merge(auth_headers).merge(head), timeout)
  Reporting.log_request(req)

  instrument(req) do |payload|
    begin
      req.response = execute_request(req)
      payload.merge!({ :status => req.response.status,
                       :response_headers => req.response.headers.to_hash }) if req.response
    rescue => error
      req.error = error
      payload.merge!({ :status => error.status,
                       :response_headers => error.headers.to_hash }) if error.is_a?(HttpError)
      Reporting.record(req)
      raise error
    ensure
      payload.merge!(self.instrumentation_payload_extras)
    end
  end

  Reporting.log_response(req)
  Reporting.record(req)

  req.response
end

#instrumentation_payload_extrasObject



95
96
97
# File 'lib/songkick/transport/base.rb', line 95

def instrumentation_payload_extras
  Thread.current[:transport_base_payload_extras] ||= {}
end

#instrumentation_payload_extras=(extras) ⇒ Object



99
100
101
# File 'lib/songkick/transport/base.rb', line 99

def instrumentation_payload_extras=(extras)
  Thread.current[:transport_base_payload_extras] = {}
end