Class: FizzyApiClient::Response

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

Direct Known Subclasses

PaginatedResponse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status:, headers:, raw_body:) ⇒ Response

Returns a new instance of Response.



7
8
9
10
11
12
# File 'lib/fizzy_api_client/response.rb', line 7

def initialize(status:, headers:, raw_body:)
  @status = status
  @headers = normalize_headers(headers)
  @raw_body = raw_body
  @body = parse_body(raw_body)
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



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

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#raw_bodyObject (readonly)

Returns the value of attribute raw_body.



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

def raw_body
  @raw_body
end

#statusObject (readonly)

Returns the value of attribute status.



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

def status
  @status
end

Instance Method Details

#created_with_location?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/fizzy_api_client/response.rb', line 34

def created_with_location?
  status == 201 && location && (raw_body.nil? || raw_body.empty?)
end

#etagObject



26
27
28
# File 'lib/fizzy_api_client/response.rb', line 26

def etag
  headers["etag"]
end

#locationObject



30
31
32
# File 'lib/fizzy_api_client/response.rb', line 30

def location
  headers["location"]
end

#next_page_urlObject



38
39
40
41
42
43
44
# File 'lib/fizzy_api_client/response.rb', line 38

def next_page_url
  link_header = headers["link"]
  return nil unless link_header

  match = link_header.match(/<([^>]+)>;\s*rel="next"/)
  match ? match[1] : nil
end

#no_content?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/fizzy_api_client/response.rb', line 18

def no_content?
  status == 204
end

#not_modified?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/fizzy_api_client/response.rb', line 22

def not_modified?
  status == 304
end

#success?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/fizzy_api_client/response.rb', line 14

def success?
  status >= 200 && status < 300
end