Class: Songkick::Transport::Response

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

Direct Known Subclasses

Created, NoContent, OK, UserError

Defined Under Namespace

Classes: Created, NoContent, OK, UserError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#bodyObject (readonly)

Returns the value of attribute body.



27
28
29
# File 'lib/songkick/transport/response.rb', line 27

def body
  @body
end

#dataObject (readonly)

Returns the value of attribute data.



27
28
29
# File 'lib/songkick/transport/response.rb', line 27

def data
  @data
end

#headersObject (readonly)

Returns the value of attribute headers.



27
28
29
# File 'lib/songkick/transport/response.rb', line 27

def headers
  @headers
end

#statusObject (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

#errorsObject



36
37
38
# File 'lib/songkick/transport/response.rb', line 36

def errors
  data && data['errors']
end