Class: SeaShanty::Response

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status:, message:, headers:, body:, original_response: ORIGINAL_RESPONSE_NOT_PRESENT) ⇒ Response

Returns a new instance of Response.



18
19
20
21
22
23
24
# File 'lib/sea_shanty/response.rb', line 18

def initialize(status:, message:, headers:, body:, original_response: ORIGINAL_RESPONSE_NOT_PRESENT)
  @status = Integer(status, status.is_a?(String) ? 10 : 0)
  @message = message
  @headers = headers
  @body = body
  @original_response = original_response
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



5
6
7
# File 'lib/sea_shanty/response.rb', line 5

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



5
6
7
# File 'lib/sea_shanty/response.rb', line 5

def headers
  @headers
end

#messageObject (readonly)

Returns the value of attribute message.



5
6
7
# File 'lib/sea_shanty/response.rb', line 5

def message
  @message
end

#original_responseObject (readonly)

Returns the value of attribute original_response.



5
6
7
# File 'lib/sea_shanty/response.rb', line 5

def original_response
  @original_response
end

#statusObject (readonly)

Returns the value of attribute status.



5
6
7
# File 'lib/sea_shanty/response.rb', line 5

def status
  @status
end

Class Method Details

.from_h(hash) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/sea_shanty/response.rb', line 8

def from_h(hash)
  new(
    status: hash.fetch(:status).fetch(:code),
    message: hash.fetch(:status).fetch(:message),
    headers: hash.fetch(:headers),
    body: hash.fetch(:body).fetch(:encoding).empty? ? nil : hash.fetch(:body).fetch(:string)
  )
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



44
45
46
47
48
# File 'lib/sea_shanty/response.rb', line 44

def ==(other)
  self.class === other &&
    status == other.status && message == other.message && headers == other.headers &&
    body == other.body
end

#hashObject



52
53
54
# File 'lib/sea_shanty/response.rb', line 52

def hash
  [self.class, status, message, headers, body].hash
end

#to_hObject



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/sea_shanty/response.rb', line 26

def to_h
  {
    status: {
      code: status,
      message: message
    },
    headers: headers,
    body: {
      string: body.to_s,
      encoding: body.nil? ? "" : body.encoding.name
    }
  }
end

#was_stored?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/sea_shanty/response.rb', line 40

def was_stored?
  ORIGINAL_RESPONSE_NOT_PRESENT != original_response
end