Class: Nombaone::NombaObject
- Inherits:
-
Object
- Object
- Nombaone::NombaObject
- Defined in:
- lib/nombaone/object.rb,
sig/nombaone/object.rbs
Overview
A response object from the API. Every field is readable two ways:
customer.amount_in_kobo # snake_case reader, derived from "amountInKobo"
customer[:amount_in_kobo] # or bracket access (String/Symbol, snake or wire)
Nested objects and arrays of objects are wrapped recursively, so
subscription.items.first.price_id just works. metadata (and any
free-form JSON) keeps its keys verbatim. Reach the raw wire Hash any time
with #to_h.
The object also carries the #request_id for the call that produced it and the raw #last_response (status + headers), for support and rate-limit introspection.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#last_response ⇒ Nombaone::Internal::Response?
readonly
The raw HTTP response.
-
#request_id ⇒ String?
readonly
The
meta.requestIdof the call that produced this.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Two objects are equal when their underlying wire Hashes are equal.
-
#[](key) ⇒ Object?
Read a field by snake_case or wire name (String or Symbol).
-
#dig(*keys) ⇒ Object?
Walk nested objects safely:
invoice.dig(:line_items, 0, :amount_in_kobo). - #hash ⇒ Integer
-
#initialize(values, request_id: nil, response: nil) ⇒ NombaObject
constructor
private
A new instance of NombaObject.
- #inspect ⇒ String
-
#key?(key) ⇒ Boolean
(also: #has_key?, #member?)
Whether the field is present.
-
#keys ⇒ Array<String>
The wire field names present on this object.
- #method_missing(name, *args) ⇒ Object private
- #respond_to_missing?(name, include_private = false) ⇒ Boolean private
-
#to_h ⇒ Hash
(also: #to_hash)
The raw wire Hash, exactly as received (camelCase string keys, nested Hashes/Arrays).
Constructor Details
#initialize(values, request_id: nil, response: nil) ⇒ NombaObject
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of NombaObject.
35 36 37 38 39 40 |
# File 'lib/nombaone/object.rb', line 35 def initialize(values, request_id: nil, response: nil) @values = values.is_a?(Hash) ? values : {} @converted = {} @request_id = request_id @last_response = response end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
118 119 120 121 122 123 |
# File 'lib/nombaone/object.rb', line 118 def method_missing(name, *args) wire = args.empty? ? resolve_key(name) : nil return converted(wire) unless wire.nil? super end |
Instance Attribute Details
#last_response ⇒ Nombaone::Internal::Response? (readonly)
Returns the raw HTTP response.
29 30 31 |
# File 'lib/nombaone/object.rb', line 29 def last_response @last_response end |
#request_id ⇒ String? (readonly)
Returns the meta.requestId of the call that produced this.
27 28 29 |
# File 'lib/nombaone/object.rb', line 27 def request_id @request_id end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
Two objects are equal when their underlying wire Hashes are equal.
request_id/response metadata do not participate.
94 95 96 |
# File 'lib/nombaone/object.rb', line 94 def ==(other) other.is_a?(NombaObject) && other.to_h == @values end |
#[](key) ⇒ Object?
Read a field by snake_case or wire name (String or Symbol). Returns nil
when the field is absent — the lenient counterpart to a reader method
(which raises NoMethodError on an unknown field).
48 49 50 51 |
# File 'lib/nombaone/object.rb', line 48 def [](key) wire = resolve_key(key) wire.nil? ? nil : converted(wire) end |
#dig(*keys) ⇒ Object?
Walk nested objects safely: invoice.dig(:line_items, 0, :amount_in_kobo).
57 58 59 60 61 62 63 64 |
# File 'lib/nombaone/object.rb', line 57 def dig(*keys) keys.reduce(self) do |node, key| break nil if node.nil? break nil unless node.is_a?(NombaObject) || node.is_a?(Array) || node.is_a?(Hash) node[key] end end |
#hash ⇒ Integer
100 101 102 |
# File 'lib/nombaone/object.rb', line 100 def hash @values.hash end |
#inspect ⇒ String
105 106 107 |
# File 'lib/nombaone/object.rb', line 105 def inspect "#<#{self.class.name} #{@values.inspect}>" end |
#key?(key) ⇒ Boolean Also known as: has_key?, member?
Returns whether the field is present.
68 69 70 |
# File 'lib/nombaone/object.rb', line 68 def key?(key) !resolve_key(key).nil? end |
#keys ⇒ Array<String>
Returns the wire field names present on this object.
75 76 77 |
# File 'lib/nombaone/object.rb', line 75 def keys @values.keys end |
#respond_to_missing?(name, include_private = false) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
110 111 112 113 114 115 |
# File 'lib/nombaone/object.rb', line 110 def respond_to_missing?(name, include_private = false) name = name.to_s return false if name.end_with?("=", "?", "!") !resolve_key(name).nil? || super end |
#to_h ⇒ Hash Also known as: to_hash
The raw wire Hash, exactly as received (camelCase string keys, nested Hashes/Arrays). Use this to serialize the object back or inspect the untouched payload.
84 85 86 |
# File 'lib/nombaone/object.rb', line 84 def to_h @values end |