Class: Brapi::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/brapi/model.rb

Direct Known Subclasses

Brapi::Models::AvailableListResponse, Brapi::Models::BalanceSheetEntry, Brapi::Models::CashDividend, Brapi::Models::CashflowEntry, Brapi::Models::DividendsData, Brapi::Models::FinancialDataEntry, Brapi::Models::HistoricalDataPrice, Brapi::Models::IncomeStatementEntry, Brapi::Models::KeyStatisticsEntry, Brapi::Models::Pagination, Brapi::Models::Quote, Brapi::Models::QuoteListItem, Brapi::Models::QuoteListResponse, Brapi::Models::QuoteRetrieveResponse, Brapi::Models::StockDividend, Brapi::Models::Subscription, Brapi::Models::SummaryProfile, Brapi::Models::V2::Crypto, Brapi::Models::V2::CryptoListAvailableResponse, Brapi::Models::V2::CryptoRetrieveResponse, Brapi::Models::V2::Currency, Brapi::Models::V2::CurrencyListAvailableResponse, Brapi::Models::V2::CurrencyRetrieveResponse, Brapi::Models::V2::Fii, Brapi::Models::V2::FiiDividend, Brapi::Models::V2::FiiDividendsResponse, Brapi::Models::V2::FiiHistoricalResponse, Brapi::Models::V2::FiiHistory, Brapi::Models::V2::FiiIndicatorsResponse, Brapi::Models::V2::FiiListResponse, Brapi::Models::V2::InflationEntry, Brapi::Models::V2::InflationListAvailableResponse, Brapi::Models::V2::InflationRetrieveResponse, Brapi::Models::V2::MacroListAvailableResponse, Brapi::Models::V2::MacroObservation, Brapi::Models::V2::MacroResult, Brapi::Models::V2::MacroRetrieveResponse, Brapi::Models::V2::MacroSeries, Brapi::Models::V2::PrimeRateEntry, Brapi::Models::V2::PrimeRateListAvailableResponse, Brapi::Models::V2::PrimeRateRetrieveResponse, Brapi::Models::V2::TreasuryBond, Brapi::Models::V2::TreasuryIndicatorsResponse, Brapi::Models::V2::TreasuryListResponse, Brapi::Models::V2::TreasuryRateInfo, Brapi::Models::ValueAddedEntry

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Model

Returns a new instance of Model.



35
36
37
38
39
40
41
42
43
44
# File 'lib/brapi/model.rb', line 35

def initialize(hash = {})
  hash ||= {}
  hash = hash.transform_keys(&:to_s) if hash.respond_to?(:transform_keys)
  @raw = hash
  self.class.attribute_specs.each do |name, spec|
    value = hash[spec[:json_key]]
    value = hash[name.to_s] if value.nil?
    instance_variable_set("@#{name}", coerce(value, spec[:type]))
  end
end

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



66
67
68
# File 'lib/brapi/model.rb', line 66

def raw
  @raw
end

Class Method Details

.attribute(name, type: nil, json_key: nil) ⇒ Object



9
10
11
12
13
# File 'lib/brapi/model.rb', line 9

def attribute(name, type: nil, json_key: nil)
  attr_reader name

  attribute_specs[name] = { type: type, json_key: json_key&.to_s || camelize(name.to_s) }
end

.attribute_specsObject



15
16
17
18
19
20
# File 'lib/brapi/model.rb', line 15

def attribute_specs
  @attribute_specs ||= begin
    parent = superclass.respond_to?(:attribute_specs) ? superclass.attribute_specs : {}
    parent.dup
  end
end

.camelize(snake) ⇒ Object



29
30
31
32
# File 'lib/brapi/model.rb', line 29

def camelize(snake)
  head, *rest = snake.split("_")
  ([head] + rest.map(&:capitalize)).join
end

.from_h(value) ⇒ Object



22
23
24
25
26
27
# File 'lib/brapi/model.rb', line 22

def from_h(value)
  return nil if value.nil?
  return value if value.is_a?(self)

  new(value)
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



57
58
59
# File 'lib/brapi/model.rb', line 57

def ==(other)
  other.is_a?(self.class) && other.to_h == to_h
end

#[](name) ⇒ Object



53
54
55
# File 'lib/brapi/model.rb', line 53

def [](name)
  public_send(name) if respond_to?(name)
end

#hashObject



62
63
64
# File 'lib/brapi/model.rb', line 62

def hash
  to_h.hash
end

#to_hObject



46
47
48
49
50
51
# File 'lib/brapi/model.rb', line 46

def to_h
  self.class.attribute_specs.each_with_object({}) do |(name, _), h|
    value = public_send(name)
    h[name] = serialize(value)
  end
end