Class: Sawyer::Response
- Inherits:
-
Object
- Object
- Sawyer::Response
- Defined in:
- lib/aptible/billforward/response.rb
Instance Method Summary collapse
-
#initialize(agent, res, options = {}) ⇒ Response
constructor
rubocop:disable MethodLength.
- #klass_from_type(result) ⇒ Object
-
#process_data(data) ⇒ Object
rubocop:enable MethodLength.
Constructor Details
#initialize(agent, res, options = {}) ⇒ Response
rubocop:disable MethodLength
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/aptible/billforward/response.rb', line 7 def initialize(agent, res, = {}) fail Aptible::BillForward::ResponseError.new( @status.to_s, response: @res, body: @data, cause: @status ) if res.status >= 400 @res = res @agent = agent @status = res.status @headers = res.headers @env = res.env @rels = process_rels @started = [:sawyer_started] @ended = [:sawyer_ended] @data = if @headers[:content_type] =~ /json|msgpack/ process_data(@agent.decode_body(res.body)) else res.body end end |
Instance Method Details
#klass_from_type(result) ⇒ Object
41 42 43 |
# File 'lib/aptible/billforward/response.rb', line 41 def klass_from_type(result) "Aptible::BillForward::#{result[:@type].classify}".constantize end |
#process_data(data) ⇒ Object
rubocop:enable MethodLength
31 32 33 34 35 36 37 38 39 |
# File 'lib/aptible/billforward/response.rb', line 31 def process_data(data) data = data[:results] if data.key? :results case data when Hash then klass_from_type(data).new(agent, data) when Array then data.map { |hash| process_data(hash) } when nil then nil else data end end |