Module: Resource::InstanceMethods
- Defined in:
- lib/simplespotify/resource/instance_methods.rb
Instance Attribute Summary collapse
-
#external_urls ⇒ Object
Returns the value of attribute external_urls.
-
#href ⇒ Object
Returns the value of attribute href.
-
#id ⇒ Object
Returns the value of attribute id.
-
#uri ⇒ Object
Returns the value of attribute uri.
Instance Method Summary collapse
- #fetch!(client = nil) ⇒ Object
- #initialize(data, fetched: false) ⇒ Object
- #link(kind = :api) ⇒ Object
- #to_h ⇒ Object
Instance Attribute Details
#external_urls ⇒ Object
Returns the value of attribute external_urls.
5 6 7 |
# File 'lib/simplespotify/resource/instance_methods.rb', line 5 def external_urls @external_urls end |
#href ⇒ Object
Returns the value of attribute href.
5 6 7 |
# File 'lib/simplespotify/resource/instance_methods.rb', line 5 def href @href end |
#id ⇒ Object
Returns the value of attribute id.
5 6 7 |
# File 'lib/simplespotify/resource/instance_methods.rb', line 5 def id @id end |
#uri ⇒ Object
Returns the value of attribute uri.
5 6 7 |
# File 'lib/simplespotify/resource/instance_methods.rb', line 5 def uri @uri end |
Instance Method Details
#fetch!(client = nil) ⇒ Object
52 53 54 55 56 |
# File 'lib/simplespotify/resource/instance_methods.rb', line 52 def fetch! client=nil client = SimpleSpotify.default_client response = client.get(@href) self.class.new(response.body, fetched: true) end |
#initialize(data, fetched: false) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/simplespotify/resource/instance_methods.rb', line 7 def initialize data, fetched: false @__props = [] @fetched = fetched # puts "-NEW #{self.class.to_s} #{self.class._template.keys.join(',')}" [:id, :uri, :href, :external_urls].each do |k| if data[k] @__props << k instance_variable_set("@#{k}", data[k]) end end self.class._template.each do |k, v| # puts "--EVAL #{self.class}" if v.is_a? Symbol _set(k, data[v]) else key = v[:from] klass = SimpleSpotify::Model.const_get(v[:kind]) if v[:kind] data[key] = v[:default] if v[:default] unless data[key] next end case v[:type] when :real _set(k, (data[v] || v[:default])) when :virtual data[key].each {|k, v| _set(k, v) } when :resource_collection values = data[key] values = SimpleSpotify::Model::Collection.of(klass, values) if v[:paginated] _set(key, values) when :resource _set key, klass.new(data[key]) end end end end |
#link(kind = :api) ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/simplespotify/resource/instance_methods.rb', line 59 def link kind=:api case kind when :uri then @uri when :api then @href when :web then @external_urls && @external_urls[:spotify] when :preview then @preview_url end end |
#to_h ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/simplespotify/resource/instance_methods.rb', line 69 def to_h tpls = self.class._template @__props.map {|v| value = send(v) tpl = tpls[v] if tpl.is_a?(Hash) && tpl[:type] != :virtual value = case tpl[:type] when :resource_collection then value.map(&:to_h) when :resource then value.to_h end end [v, value] }.to_h end |