Class: Songkick::Transport::Response
- Inherits:
-
Object
- Object
- Songkick::Transport::Response
- Defined in:
- lib/songkick/transport/response.rb
Defined Under Namespace
Classes: Created, NoContent, OK, UserError
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Class Method Summary collapse
- .parse(body, content_type) ⇒ Object
- .process(request, status, headers, body, user_error_codes = 409) ⇒ Object
Instance Method Summary collapse
- #errors ⇒ Object
-
#initialize(status, headers, body) ⇒ Response
constructor
A new instance of Response.
Constructor Details
#initialize(status, headers, body) ⇒ Response
Returns a new instance of Response.
29 30 31 32 33 34 |
# File 'lib/songkick/transport/response.rb', line 29 def initialize(status, headers, body) @body = body @data = Response.parse(body, headers['Content-Type']) @headers = Headers.new(headers) @status = status.to_i end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
27 28 29 |
# File 'lib/songkick/transport/response.rb', line 27 def body @body end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
27 28 29 |
# File 'lib/songkick/transport/response.rb', line 27 def data @data end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
27 28 29 |
# File 'lib/songkick/transport/response.rb', line 27 def headers @headers end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
27 28 29 |
# File 'lib/songkick/transport/response.rb', line 27 def status @status end |
Class Method Details
.parse(body, content_type) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/songkick/transport/response.rb', line 19 def self.parse(body, content_type) return body unless body.is_a?(String) return nil if body.strip == '' content_type = (content_type || '').split(/\s*;\s*/).first Transport.parser_for(content_type).parse(body) end |
.process(request, status, headers, body, user_error_codes = 409) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/songkick/transport/response.rb', line 5 def self.process(request, status, headers, body, user_error_codes=409) case status.to_i when 200 then OK.new(status, headers, body) when 201 then Created.new(status, headers, body) when 204 then NoContent.new(status, headers, body) when *user_error_codes then UserError.new(status, headers, body) else raise HttpError.new(request, status, headers, body) end rescue Yajl::ParseError Transport.logger.warn "Request returned invalid JSON: #{request}" raise Transport::InvalidJSONError, request end |
Instance Method Details
#errors ⇒ Object
36 37 38 |
# File 'lib/songkick/transport/response.rb', line 36 def errors data && data['errors'] end |