Class: RailsCosmos::Http::Response

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

Overview

RailsCosmos::Http::Response

A wrapper around HTTP responses to provide a consistent interface for handling API responses. This class parses JSON responses and provides helper methods to check response success.

Examples:

Creating a response instance

response = RailsCosmos::Http::Response.new(faraday_response)

Accessing response attributes

response.status # => 200
response.body   # => { "key" => "value" }
response.success? # => true

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Response

Returns a new instance of Response.



25
26
27
28
29
# File 'lib/rails_cosmos/http/response.rb', line 25

def initialize(response)
  @raw_body = response.body.present? ? response.body : {}
  @body = response.body.present? ? JSON.parse(response.body) : {}
  @status = response.status
end

Instance Attribute Details

#bodyHash (readonly)

The parsed response body as a Hash.

Returns:

  • (Hash)

    the current value of body



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

def body
  @body
end

#raw_bodyString? (readonly)

The raw response body as a string, or nil if absent.

Returns:

  • (String, nil)

    the current value of raw_body



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

def raw_body
  @raw_body
end

#statusInteger (readonly)

The HTTP status code.

Returns:

  • (Integer)

    the current value of status



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

def status
  @status
end

Instance Method Details

#success?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/rails_cosmos/http/response.rb', line 31

def success?
  (200..299).include?(status)
end