Class: D4H::API::Model

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/d4h/api/model.rb

Overview

Public: Base class for all D4H API response models.

Wraps a JSON response hash in an OpenStruct so that attributes are accessible via dot-notation. Nested hashes and arrays are recursively converted, allowing deep attribute traversal.

Subclasses (e.g. Member, Event, Equipment) are thin wrappers that exist primarily for type identification via kind_of?.

Examples

model = D4H::API::Model.new({"id" => 1, "name" => "Alice"})
model.id    # => 1
model.name  # => "Alice"

# Nested data
model = D4H::API::Model.new({"brand" => {"title" => "Petzl"}})
model.brand.title  # => "Petzl"

# Raw JSON hash
model.to_json  # => {"id" => 1, "name" => "Alice"}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Model

Public: Initialize a Model from a JSON response hash.

attributes - A Hash of key/value pairs from the API response.



35
36
37
38
# File 'lib/d4h/api/model.rb', line 35

def initialize(attributes)
  super(to_ostruct(attributes))
  @to_json = attributes
end

Instance Attribute Details

#to_jsonObject (readonly)

Public: Returns the original JSON hash that was used to build this model.



30
31
32
# File 'lib/d4h/api/model.rb', line 30

def to_json
  @to_json
end

Instance Method Details

#to_ostruct(obj) ⇒ Object

Internal: Recursively convert a parsed JSON object into OpenStructs.

obj - A Hash, Array, or scalar value from the parsed JSON.

Returns an OpenStruct (for Hash), Array of converted values, or the original scalar.



46
47
48
49
50
51
52
53
54
# File 'lib/d4h/api/model.rb', line 46

def to_ostruct(obj)
  if obj.is_a?(Hash)
    OpenStruct.new(obj.map { |key, val| [key, to_ostruct(val)] }.to_h)
  elsif obj.is_a?(Array)
    obj.map { |o| to_ostruct(o) }
  else
    obj
  end
end