Class: SpotifyWebApi::PlayHistoryObject

Inherits:
BaseModel
  • Object
show all
Defined in:
lib/spotify_web_api/models/play_history_object.rb

Overview

PlayHistoryObject Model.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseModel

#to_hash, #to_json

Constructor Details

#initialize(track = SKIP, played_at = SKIP, context = SKIP) ⇒ PlayHistoryObject

Returns a new instance of PlayHistoryObject.



48
49
50
51
52
# File 'lib/spotify_web_api/models/play_history_object.rb', line 48

def initialize(track = SKIP, played_at = SKIP, context = SKIP)
  @track = track unless track == SKIP
  @played_at = played_at unless played_at == SKIP
  @context = context unless context == SKIP
end

Instance Attribute Details

#contextContextObject

The context the track was played from.

Returns:



23
24
25
# File 'lib/spotify_web_api/models/play_history_object.rb', line 23

def context
  @context
end

#played_atDateTime

The date and time the track was played.

Returns:

  • (DateTime)


19
20
21
# File 'lib/spotify_web_api/models/play_history_object.rb', line 19

def played_at
  @played_at
end

#trackTrackObject

The track the user listened to.

Returns:



15
16
17
# File 'lib/spotify_web_api/models/play_history_object.rb', line 15

def track
  @track
end

Class Method Details

.from_hash(hash) ⇒ Object

Creates an instance of the object from a hash.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/spotify_web_api/models/play_history_object.rb', line 55

def self.from_hash(hash)
  return nil unless hash

  # Extract variables from the hash.
  track = TrackObject.from_hash(hash['track']) if hash['track']
  played_at = if hash.key?('played_at')
                (DateTimeHelper.from_rfc3339(hash['played_at']) if hash['played_at'])
              else
                SKIP
              end
  context = ContextObject.from_hash(hash['context']) if hash['context']

  # Create object from extracted values.
  PlayHistoryObject.new(track,
                        played_at,
                        context)
end

.namesObject

A mapping from model property names to API property names.



26
27
28
29
30
31
32
# File 'lib/spotify_web_api/models/play_history_object.rb', line 26

def self.names
  @_hash = {} if @_hash.nil?
  @_hash['track'] = 'track'
  @_hash['played_at'] = 'played_at'
  @_hash['context'] = 'context'
  @_hash
end

.nullablesObject

An array for nullable fields



44
45
46
# File 'lib/spotify_web_api/models/play_history_object.rb', line 44

def self.nullables
  []
end

.optionalsObject

An array for optional fields



35
36
37
38
39
40
41
# File 'lib/spotify_web_api/models/play_history_object.rb', line 35

def self.optionals
  %w[
    track
    played_at
    context
  ]
end

Instance Method Details

#to_custom_played_atObject



73
74
75
# File 'lib/spotify_web_api/models/play_history_object.rb', line 73

def to_custom_played_at
  DateTimeHelper.to_rfc3339(played_at)
end