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, request) ⇒ Response

Returns a new instance of Response.



60
61
62
63
64
65
66
67
68
# File 'lib/strobe/connection.rb', line 60

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

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

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



58
59
60
# File 'lib/strobe/connection.rb', line 58

def headers
  @headers
end

#requestObject (readonly)

Returns the value of attribute request.



58
59
60
# File 'lib/strobe/connection.rb', line 58

def request
  @request
end

Instance Method Details

#bodyObject



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

def body
  return @body if @body

  case mime_type
  when :json
    @body = json_decode(@response.body)
  else
    @body = @response.body
  end

  @body
end

#statusObject



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

def status
  @response.code.to_i
end

#success?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/strobe/connection.rb', line 87

def success?
  status == 200
end

#to_hashObject



108
109
110
111
112
113
114
# File 'lib/strobe/connection.rb', line 108

def to_hash
  {
    :body => body,
    :headers => headers,
    :status => status
  }
end

#validate!Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/strobe/connection.rb', line 91

def validate!
  case status
  when 401
    raise UnauthenticatedError, error_message
  when 404
    raise ResourceNotFoundError, error_message
  when 412
    raise OutdatedStrobeVersionError, error_message
  when 503
    headers["x-strobe-maintenance"] ? raise(UnderMaintenanceError) : server_error
  when 500...600
    server_error
  end

  self
end