Class: Yookassa::Entities::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/yookassa/entities/base.rb

Overview

Base entity class that wraps API response hashes with dynamic attribute access.

Attributes returned by the API are accessible as methods via method_missing. Nested hashes are automatically wrapped into entity instances, and arrays of hashes become arrays of entities.

Examples:

entity = Yookassa::Entities::Base.new(id: "abc", amount: { value: "100", currency: "RUB" })
entity.id              # => "abc"
entity.amount.value    # => "100"
entity[:id]            # => "abc"
entity.to_h            # => { "id" => "abc", "amount" => { "value" => "100", "currency" => "RUB" } }

Direct Known Subclasses

Deal, Payment, Payout, Receipt, Refund, WebhookObj

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Base

Returns a new instance of Base.

Parameters:

  • raw API response data



22
23
24
# File 'lib/yookassa/entities/base.rb', line 22

def initialize(data)
  @attributes = self.class.send(:normalize, data)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object (private)



47
48
49
50
51
52
53
54
# File 'lib/yookassa/entities/base.rb', line 47

def method_missing(method_name, *args)
  key = method_name.to_s
  if @attributes.key?(key)
    wrap_value(@attributes[key])
  else
    super
  end
end

Instance Attribute Details

#attributesHash<String, Object> (readonly)

Returns normalized attribute hash (string keys).

Returns:

  • normalized attribute hash (string keys)



19
20
21
# File 'lib/yookassa/entities/base.rb', line 19

def attributes
  @attributes
end

Instance Method Details

#[](key) ⇒ Object?

Hash-style attribute access.

Parameters:

  • attribute name

Returns:



30
31
32
# File 'lib/yookassa/entities/base.rb', line 30

def [](key)
  @attributes[key.to_s]
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:



41
42
43
# File 'lib/yookassa/entities/base.rb', line 41

def respond_to_missing?(method_name, include_private = false)
  @attributes.key?(method_name.to_s) || super
end

#to_hHash<String, Object>

Returns the raw attribute hash.

Returns:



37
38
39
# File 'lib/yookassa/entities/base.rb', line 37

def to_h
  @attributes
end