Class: PaystackSdk::Response
- Inherits:
-
Object
- Object
- PaystackSdk::Response
- Defined in:
- lib/paystack_sdk/response.rb
Overview
The Response class provides a wrapper around Paystack API responses. It offers convenient access to response data through dot notation and supports both direct attribute access and hash/array-like operations.
The Response class handles API responses that use string keys (as returned by the Paystack API) and provides seamless access through both string and symbol notation.
Features:
-
Dynamic attribute access via dot notation (response.data.attribute)
-
Array-like access for list responses (response)
-
Iteration support (response.each)
-
Automatic handling of nested data structures
-
Consistent handling of string-keyed API responses
“‘ruby
response = PaystackSdk::Response.new(api_response)
# Check if request was successful
if response.success?
# Access data using dot notation
puts response.data.
puts response.data.reference
# Or directly from response
puts response.
else
puts "Error: #{response.error_message}"
end
Instance Attribute Summary collapse
-
#api_message ⇒ String?
readonly
API message from the response.
-
#error_message ⇒ String?
readonly
Error message if the request failed.
-
#message ⇒ String?
readonly
API message from the response, if available.
-
#raw_data ⇒ Hash, ...
readonly
The underlying data.
-
#status_code ⇒ Integer
readonly
The status code of the API response.
Instance Method Summary collapse
-
#[](key) ⇒ Object, Response
Access data via hash/array notation.
-
#count ⇒ Integer
The number of items.
-
#data ⇒ Response
Returns a Response object for the data This enables chained access like response.data.key.
-
#each {|key, value| ... } ⇒ Response, Enumerator
Iterate through hash entries or array items.
-
#empty? ⇒ Boolean
Whether the collection is empty.
-
#error_details ⇒ Hash
Get error information if the request failed.
-
#failed? ⇒ Boolean
Check if the response failed.
-
#first ⇒ Object, Response
The first item, wrapped if necessary.
-
#initialize(response) ⇒ Response
constructor
Initializes a new Response object.
-
#key?(key) ⇒ Boolean
Check if key exists in hash.
-
#last ⇒ Object, Response
The last item, wrapped if necessary.
-
#length ⇒ Integer
The number of items.
-
#method_missing(method_name, *args, &block) ⇒ Object, Response
Access hash values via methods (dot notation) Allows accessing data attributes directly: response.attribute_name.
-
#original_response ⇒ Hash, Array
Returns the original response body This is useful for debugging or accessing raw data.
-
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Check if the object responds to a method.
-
#size ⇒ Integer
The number of items.
-
#success? ⇒ Boolean
Check if the response was successful.
Constructor Details
#initialize(response) ⇒ Response
API-level errors (4xx except 401) are returned as unsuccessful Response objects rather than raised as exceptions. Use response.success? to check for errors.
Initializes a new Response object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/paystack_sdk/response.rb', line 71 def initialize(response) if response.is_a?(Faraday::Response) @status_code = response.status @body = response.body = (@body) = @raw_data = extract_data_from_body(@body) case @status_code when 200..299 @success = true when 400..499 # Client errors - return unsuccessful response for user to handle @success = false = || "Client error" # Still raise for authentication issues as these are usually config problems raise AuthenticationError.new( || "Authentication failed") if @status_code == 401 when 429 # Rate limiting - raise as users need to implement retry logic retry_after = response.headers["Retry-After"] raise RateLimitError.new(retry_after || 30) when 500..599 # Server errors - raise as these indicate Paystack infrastructure issues raise ServerError.new(@status_code, ) else @success = false = || "Unknown error" end elsif response.is_a?(Response) @success = response.success? = response. = response. @raw_data = response.raw_data else @success = true @raw_data = response end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object, Response
Access hash values via methods (dot notation) Allows accessing data attributes directly: response.attribute_name
161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/paystack_sdk/response.rb', line 161 def method_missing(method_name, *args, &block) if @raw_data.is_a?(Hash) && (@raw_data.key?(method_name) || @raw_data.key?(method_name.to_s)) value = @raw_data[method_name] || @raw_data[method_name.to_s] wrap_value(value) elsif @raw_data.is_a?(Array) && @raw_data.respond_to?(method_name) result = @raw_data.send(method_name, *args, &block) wrap_value(result) else super end end |
Instance Attribute Details
#api_message ⇒ String? (readonly)
Returns API message from the response.
52 53 54 |
# File 'lib/paystack_sdk/response.rb', line 52 def end |
#error_message ⇒ String? (readonly)
Returns Error message if the request failed.
49 50 51 |
# File 'lib/paystack_sdk/response.rb', line 49 def end |
#message ⇒ String? (readonly)
Returns API message from the response, if available.
55 56 57 |
# File 'lib/paystack_sdk/response.rb', line 55 def end |
#raw_data ⇒ Hash, ... (readonly)
Returns The underlying data.
58 59 60 |
# File 'lib/paystack_sdk/response.rb', line 58 def raw_data @raw_data end |
#status_code ⇒ Integer (readonly)
Returns The status code of the API response.
61 62 63 |
# File 'lib/paystack_sdk/response.rb', line 61 def status_code @status_code end |
Instance Method Details
#[](key) ⇒ Object, Response
Access data via hash/array notation
188 189 190 191 192 193 194 195 196 197 |
# File 'lib/paystack_sdk/response.rb', line 188 def [](key) return nil unless @raw_data if @raw_data.is_a?(Hash) value = @raw_data[key.is_a?(String) ? key.to_sym : key] wrap_value(value) elsif @raw_data.is_a?(Array) && key.is_a?(Integer) wrap_value(@raw_data[key]) end end |
#count ⇒ Integer
Returns The number of items.
232 233 234 235 236 |
# File 'lib/paystack_sdk/response.rb', line 232 i[size length count empty?].each do |method_name| define_method(method_name) do @raw_data.send(method_name) if @raw_data.respond_to?(method_name) end end |
#data ⇒ Response
Returns a Response object for the data This enables chained access like response.data.key
115 116 117 |
# File 'lib/paystack_sdk/response.rb', line 115 def data self end |
#each {|key, value| ... } ⇒ Response, Enumerator
Iterate through hash entries or array items
212 213 214 215 216 217 218 219 220 221 |
# File 'lib/paystack_sdk/response.rb', line 212 def each return enum_for(:each) unless block_given? if @raw_data.is_a?(Hash) @raw_data.each { |k, v| yield k, wrap_value(v) } elsif @raw_data.is_a?(Array) @raw_data.each { |item| yield wrap_value(item) } end self end |
#empty? ⇒ Boolean
Returns Whether the collection is empty.
232 233 234 235 236 |
# File 'lib/paystack_sdk/response.rb', line 232 i[size length count empty?].each do |method_name| define_method(method_name) do @raw_data.send(method_name) if @raw_data.respond_to?(method_name) end end |
#error_details ⇒ Hash
Get error information if the request failed
136 137 138 139 140 141 142 143 144 |
# File 'lib/paystack_sdk/response.rb', line 136 def error_details return {} if success? { status_code: @status_code, message: || , raw_response: @body }.compact end |
#failed? ⇒ Boolean
Check if the response failed
129 130 131 |
# File 'lib/paystack_sdk/response.rb', line 129 def failed? !@success end |
#first ⇒ Object, Response
Returns The first item, wrapped if necessary.
243 244 245 246 247 248 249 |
# File 'lib/paystack_sdk/response.rb', line 243 i[first last].each do |method_name| define_method(method_name) do return nil unless @raw_data.is_a?(Array) wrap_value(@raw_data.send(method_name)) end end |
#key?(key) ⇒ Boolean
Check if key exists in hash
203 204 205 |
# File 'lib/paystack_sdk/response.rb', line 203 def key?(key) @raw_data.is_a?(Hash) && @raw_data.key?(key.is_a?(String) ? key.to_sym : key) end |
#last ⇒ Object, Response
Returns The last item, wrapped if necessary.
243 244 245 246 247 248 249 |
# File 'lib/paystack_sdk/response.rb', line 243 i[first last].each do |method_name| define_method(method_name) do return nil unless @raw_data.is_a?(Array) wrap_value(@raw_data.send(method_name)) end end |
#length ⇒ Integer
Returns The number of items.
232 233 234 235 236 |
# File 'lib/paystack_sdk/response.rb', line 232 i[size length count empty?].each do |method_name| define_method(method_name) do @raw_data.send(method_name) if @raw_data.respond_to?(method_name) end end |
#original_response ⇒ Hash, Array
Returns the original response body This is useful for debugging or accessing raw data
150 151 152 |
# File 'lib/paystack_sdk/response.rb', line 150 def original_response @body end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Check if the object responds to a method
178 179 180 181 182 |
# File 'lib/paystack_sdk/response.rb', line 178 def respond_to_missing?(method_name, include_private = false) (@raw_data.is_a?(Hash) && (@raw_data.key?(method_name) || @raw_data.key?(method_name.to_s))) || (@raw_data.is_a?(Array) && @raw_data.respond_to?(method_name)) || super end |
#size ⇒ Integer
Returns The number of items.
232 233 234 235 236 |
# File 'lib/paystack_sdk/response.rb', line 232 i[size length count empty?].each do |method_name| define_method(method_name) do @raw_data.send(method_name) if @raw_data.respond_to?(method_name) end end |
#success? ⇒ Boolean
Check if the response was successful
122 123 124 |
# File 'lib/paystack_sdk/response.rb', line 122 def success? @success end |