Class: Strobe::Connection::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/strobe/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Response

Returns a new instance of Response.



43
44
45
46
47
48
49
50
# File 'lib/strobe/connection.rb', line 43

def initialize(response)
  @response = response
  @headers  = {}

  response.each_header do |key, val|
    @headers[key] = val
  end
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



41
42
43
# File 'lib/strobe/connection.rb', line 41

def headers
  @headers
end

Instance Method Details

#bodyObject



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/strobe/connection.rb', line 56

def body
  return @body if @body

  case mime_type
  when :json
    @body = ActiveSupport::JSON.decode(@response.body)
  else
    @body = @response.body
  end

  @body
end

#statusObject



52
53
54
# File 'lib/strobe/connection.rb', line 52

def status
  @response.code.to_i
end

#success?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/strobe/connection.rb', line 69

def success?
  status == 200
end

#validate!Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/strobe/connection.rb', line 73

def validate!
  msg = body['errors'] && body['errors']['request']
  case status
  when 401
    # TODO Fix this on the server
    raise UnauthenticatedError, msg
  when 404
    raise ResourceNotFoundError, msg
  when 500...600
    raise ServerError, "The server puked :(. Don't worry, the error has been reported."
  end

  self
end