Class: Nombaone::NombaObject

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

Examples:

sub = nombaone.subscriptions.retrieve("nbo000000000001sub")
sub.status                 # => "active"
sub.current_period_end     # => "2026-08-05T00:00:00.000Z"
sub.items.first.price_id   # => "nbo000000000001prc"
sub[:latestInvoiceId]      # => "nbo000000000001inv"
sub.request_id             # => "req_…"

Direct Known Subclasses

WebhookEvent

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • values (Hash)

    the parsed wire object (camelCase string keys).

  • request_id (String, nil) (defaults to: nil)
  • response (Nombaone::Internal::Response, nil) (defaults to: nil)
  • request_id: (String, nil) (defaults to: nil)
  • response: (Object) (defaults to: nil)


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.

Parameters:

  • name (Symbol)
  • args (Object)

Returns:

  • (Object)


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_responseNombaone::Internal::Response? (readonly)

Returns the raw HTTP response.

Returns:



29
30
31
# File 'lib/nombaone/object.rb', line 29

def last_response
  @last_response
end

#request_idString? (readonly)

Returns the meta.requestId of the call that produced this.

Returns:

  • (String, nil)

    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.

Parameters:

  • other (Object)

Returns:

  • (Boolean)


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

Parameters:

  • key (String, Symbol)

Returns:

  • (Object, nil)


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

Parameters:

  • keys (Array<String, Symbol, Integer>)

Returns:

  • (Object, nil)


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

#hashInteger

Returns:

  • (Integer)


100
101
102
# File 'lib/nombaone/object.rb', line 100

def hash
  @values.hash
end

#inspectString

Returns:

  • (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.

Parameters:

  • key (String, Symbol)

Returns:

  • (Boolean)

    whether the field is present.



68
69
70
# File 'lib/nombaone/object.rb', line 68

def key?(key)
  !resolve_key(key).nil?
end

#keysArray<String>

Returns the wire field names present on this object.

Returns:

  • (Array<String>)

    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.

Parameters:

  • name (Symbol)
  • include_private (Boolean) (defaults to: false)

Returns:

  • (Boolean)


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

Returns:

  • (Hash)


84
85
86
# File 'lib/nombaone/object.rb', line 84

def to_h
  @values
end