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.}"
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.
-
#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
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 |
# File 'lib/paystack_sdk/response.rb', line 71 def initialize(response) if response.is_a?(Faraday::Response) @status_code = response.status @body = response.body @api_message = (@body) @message = @api_message @raw_data = extract_data_from_body(@body) case @status_code when 200..299 @success = true when 400 handle_400_response when 401 raise AuthenticationError.new when 404 handle_404_response when 429 retry_after = response.headers["Retry-After"] raise RateLimitError.new(retry_after || 30) when 500..599 raise ServerError.new(@status_code, @api_message) else @success = false raise APIError.new(@api_message || "Paystack API Error") end elsif response.is_a?(Response) @success = response.success? @error_message = response. @api_message = 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
138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/paystack_sdk/response.rb', line 138 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 @api_message end |
#error_message ⇒ String? (readonly)
Returns Error message if the request failed.
49 50 51 |
# File 'lib/paystack_sdk/response.rb', line 49 def @error_message end |
#message ⇒ String? (readonly)
Returns API message from the response, if available.
55 56 57 |
# File 'lib/paystack_sdk/response.rb', line 55 def @message 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
165 166 167 168 169 170 171 172 173 174 |
# File 'lib/paystack_sdk/response.rb', line 165 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.
209 210 211 212 213 |
# File 'lib/paystack_sdk/response.rb', line 209 [: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
112 113 114 |
# File 'lib/paystack_sdk/response.rb', line 112 def data self end |
#each {|key, value| ... } ⇒ Response, Enumerator
Iterate through hash entries or array items
189 190 191 192 193 194 195 196 197 198 |
# File 'lib/paystack_sdk/response.rb', line 189 def each(&block) 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.
209 210 211 212 213 |
# File 'lib/paystack_sdk/response.rb', line 209 [: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 |
#first ⇒ Object, Response
Returns The first item, wrapped if necessary.
220 221 222 223 224 225 |
# File 'lib/paystack_sdk/response.rb', line 220 [: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
180 181 182 |
# File 'lib/paystack_sdk/response.rb', line 180 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.
220 221 222 223 224 225 |
# File 'lib/paystack_sdk/response.rb', line 220 [: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.
209 210 211 212 213 |
# File 'lib/paystack_sdk/response.rb', line 209 [: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
127 128 129 |
# File 'lib/paystack_sdk/response.rb', line 127 def original_response @body end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Check if the object responds to a method
155 156 157 158 159 |
# File 'lib/paystack_sdk/response.rb', line 155 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.
209 210 211 212 213 |
# File 'lib/paystack_sdk/response.rb', line 209 [: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
119 120 121 |
# File 'lib/paystack_sdk/response.rb', line 119 def success? @success end |