Class: PaystackSdk::Response

Inherits:
Object
  • Object
show all
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)

  • Hash-like access (response or response)

  • 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.authorization_url
  puts response.data.reference

  # Or directly from response
  puts response.authorization_url
else
  puts "Error: #{response.error_message}"
end

Examples:

Basic usage


Working with list responses

response = transactions.list

# Iterate through items
response.data.each do |transaction|
  puts transaction.amount
  puts transaction.reference
end

# Access by index
first_transaction = response.data[0] # => same as `response.first`
puts first_transaction.customer.email
```

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Response

Note:

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

Parameters:

  • response (Faraday::Response, Hash, Array)

    The raw API response or data

Raises:



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
    @api_message = extract_api_message(@body)
    @message = @api_message
    @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
      @error_message = @api_message || "Client error"

      # Still raise for authentication issues as these are usually config problems
      raise AuthenticationError.new(@api_message || "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, @api_message)
    else
      @success = false
      @error_message = @api_message || "Unknown error"
    end
  elsif response.is_a?(Response)
    @success = response.success?
    @error_message = response.error_message
    @api_message = response.api_message
    @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

Parameters:

  • method_name (Symbol)

    The attribute name to access

  • args (Array)

    Method arguments (unused)

  • block (Proc)

    Method block (unused)

Returns:

  • (Object, Response)

    The attribute value, wrapped in Response if it’s a complex type



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_messageString? (readonly)

Returns API message from the response.

Returns:

  • (String, nil)

    API message from the response



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

def api_message
  @api_message
end

#error_messageString? (readonly)

Returns Error message if the request failed.

Returns:

  • (String, nil)

    Error message if the request failed



49
50
51
# File 'lib/paystack_sdk/response.rb', line 49

def error_message
  @error_message
end

#messageString? (readonly)

Returns API message from the response, if available.

Returns:

  • (String, nil)

    API message from the response, if available



55
56
57
# File 'lib/paystack_sdk/response.rb', line 55

def message
  @message
end

#raw_dataHash, ... (readonly)

Returns The underlying data.

Returns:

  • (Hash, Array, Object)

    The underlying data



58
59
60
# File 'lib/paystack_sdk/response.rb', line 58

def raw_data
  @raw_data
end

#status_codeInteger (readonly)

Returns The status code of the API response.

Returns:

  • (Integer)

    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

Parameters:

  • key (Object)

    The key or index to access

Returns:

  • (Object, Response)

    The value for the given key or index



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

#countInteger

Returns The number of items.

Returns:

  • (Integer)

    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

#dataResponse

Returns a Response object for the data This enables chained access like response.data.key

Returns:

  • (Response)

    self, to enable chaining



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

Yields:

  • (key, value)

    For hashes, passes each key-value pair

  • (value)

    For arrays, passes each item

Returns:

  • (Response, Enumerator)

    Self for chaining or Enumerator if no block given



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.

Returns:

  • (Boolean)

    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_detailsHash

Get error information if the request failed

Returns:

  • (Hash)

    Hash containing error details, or empty hash if successful



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: @error_message || @api_message,
    raw_response: @body
  }.compact
end

#failed?Boolean

Check if the response failed

Returns:

  • (Boolean)

    true if the API request failed



129
130
131
# File 'lib/paystack_sdk/response.rb', line 129

def failed?
  !@success
end

#firstObject, Response

Returns The first item, wrapped if necessary.

Returns:

  • (Object, Response)

    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

Parameters:

  • key (Symbol, String)

    The key to check

Returns:

  • (Boolean)

    Whether the key exists



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

#lastObject, Response

Returns The last item, wrapped if necessary.

Returns:

  • (Object, Response)

    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

#lengthInteger

Returns The number of items.

Returns:

  • (Integer)

    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_responseHash, Array

Returns the original response body This is useful for debugging or accessing raw data

Returns:

  • (Hash, Array)

    The original response body



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

Parameters:

  • method_name (Symbol)

    The method name

  • include_private (Boolean) (defaults to: false)

    Whether to include private methods

Returns:

  • (Boolean)

    Whether the method is supported



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

#sizeInteger

Returns The number of items.

Returns:

  • (Integer)

    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

Returns:

  • (Boolean)

    true if the API request was successful



122
123
124
# File 'lib/paystack_sdk/response.rb', line 122

def success?
  @success
end