Class: Audd::Model
- Inherits:
-
Object
- Object
- Audd::Model
- Defined in:
- lib/audd/models/model.rb
Overview
Base for the response objects. Subclasses declare the fields their OpenAPI
schema documents; every instance also keeps the untouched raw hash,
because those schemas are all additionalProperties: true — Apple, Spotify
and friends ship new keys without warning and AudD passes them through
verbatim. Reach unmapped keys with result["some_new_key"].
Direct Known Subclasses
AppleMusicMetadata, DeezerMetadata, ErrorResponse, MusicBrainzEntry, RecognitionResult, RecognizeSuccessResponse, SpotifyMetadata
Instance Attribute Summary collapse
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
Class Method Summary collapse
-
.field(name, key = name.to_s, &cast) ⇒ Object
Declares a reader backed by
keyin the raw payload. - .fields ⇒ Object
-
.from_json(raw) ⇒ Object
Anything that isn't an object (
null, mostly) is absence, not an error.
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #[](key) ⇒ Object
- #hash ⇒ Object
-
#initialize(raw) ⇒ Model
constructor
A new instance of Model.
-
#inspect ⇒ Object
Only the fields that are actually present — these payloads are wide and mostly empty, and a full dump buries the useful bits.
- #to_h ⇒ Object
Constructor Details
#initialize(raw) ⇒ Model
Returns a new instance of Model.
30 31 32 33 34 35 36 37 |
# File 'lib/audd/models/model.rb', line 30 def initialize(raw) @raw = raw self.class.fields.each do |name, (key, cast)| value = raw[key] value = cast.call(value) if cast && !value.nil? instance_variable_set(:"@#{name}", value) end end |
Instance Attribute Details
#raw ⇒ Object (readonly)
Returns the value of attribute raw.
28 29 30 |
# File 'lib/audd/models/model.rb', line 28 def raw @raw end |
Class Method Details
.field(name, key = name.to_s, &cast) ⇒ Object
Declares a reader backed by key in the raw payload. An optional block
casts the raw value (used to nest sub-models).
13 14 15 16 |
# File 'lib/audd/models/model.rb', line 13 def field(name, key = name.to_s, &cast) fields[name] = [key, cast] attr_reader(name) end |
.fields ⇒ Object
18 19 20 |
# File 'lib/audd/models/model.rb', line 18 def fields @fields ||= superclass.respond_to?(:fields) ? superclass.fields.dup : {} end |
.from_json(raw) ⇒ Object
Anything that isn't an object (null, mostly) is absence, not an error.
23 24 25 |
# File 'lib/audd/models/model.rb', line 23 def from_json(raw) new(raw) if raw.is_a?(Hash) end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
47 48 49 |
# File 'lib/audd/models/model.rb', line 47 def ==(other) other.instance_of?(self.class) && other.raw == raw end |
#[](key) ⇒ Object
39 40 41 |
# File 'lib/audd/models/model.rb', line 39 def [](key) raw[key.to_s] end |
#hash ⇒ Object
52 53 54 |
# File 'lib/audd/models/model.rb', line 52 def hash [self.class, raw].hash end |
#inspect ⇒ Object
Only the fields that are actually present — these payloads are wide and mostly empty, and a full dump buries the useful bits.
58 59 60 61 62 63 64 |
# File 'lib/audd/models/model.rb', line 58 def inspect populated = self.class.fields.keys.filter_map do |name| value = public_send(name) "#{name}=#{value.inspect}" unless value.nil? end "#<#{self.class.name} #{populated.join(" ")}>" end |
#to_h ⇒ Object
43 44 45 |
# File 'lib/audd/models/model.rb', line 43 def to_h raw end |