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

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
# 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
      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.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



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_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



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

#countInteger

Returns The number of items.

Returns:

  • (Integer)

    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

#dataResponse

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

Returns:

  • (Response)

    self, to enable chaining



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

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



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.

Returns:

  • (Boolean)

    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

#firstObject, Response

Returns The first item, wrapped if necessary.

Returns:

  • (Object, Response)

    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

Parameters:

  • key (Symbol, String)

    The key to check

Returns:

  • (Boolean)

    Whether the key exists



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

#lastObject, Response

Returns The last item, wrapped if necessary.

Returns:

  • (Object, Response)

    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

#lengthInteger

Returns The number of items.

Returns:

  • (Integer)

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

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

Returns:

  • (Hash, Array)

    The original response body



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

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



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

#sizeInteger

Returns The number of items.

Returns:

  • (Integer)

    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

Returns:

  • (Boolean)

    true if the API request was successful



119
120
121
# File 'lib/paystack_sdk/response.rb', line 119

def success?
  @success
end