Module: Chronicle::Spotify::Builders

Included in:
LikeTransformer, ListenTransformer, SavedAlbumTransformer
Defined in:
lib/chronicle/spotify/builders.rb

Instance Method Summary collapse

Instance Method Details

#build_agent(agent) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/chronicle/spotify/builders.rb', line 63

def build_agent(agent)
  Chronicle::Models::Person.new do |r|
    r.name = agent[:display_name]
    r.url = agent[:external_urls][:spotify]
    r.source = 'spotify'
    r.slug = agent[:id]
    r.source_id = agent[:id]
  end
end

#build_album(album) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/chronicle/spotify/builders.rb', line 53

def build_album(album)
  Chronicle::Models::MusicAlbum.new do |r|
    r.name = album[:name]
    r.source = 'spotify'
    r.source_id = album[:id]
    r.image = album[:images].first[:url]
    r.url = album[:external_urls][:spotify]
  end
end

#build_artist(artist) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/chronicle/spotify/builders.rb', line 44

def build_artist(artist)
  Chronicle::Models::MusicGroup.new do |r|
    r.name = artist[:name]
    r.url =  artist[:external_urls][:spotify]
    r.source = 'spotify'
    r.source_id = artist[:id]
  end
end

#build_like(record:, agent:) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/chronicle/spotify/builders.rb', line 16

def build_like(record:, agent:)
  Chronicle::Models::LikeAction.new do |r|
    r.end_time = record[:added_at]

    r.object = if record[:album]
                 build_album(record[:album])
               elsif record[:track]
                 build_track(record[:track])
               end

    r.agent = build_agent(agent)
    r.source = 'spotify'
    r.dedupe_on << %i[type source end_time]
  end
end

#build_listen(record:, agent:) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/chronicle/spotify/builders.rb', line 6

def build_listen(record:, agent:)
  Chronicle::Models::ListenAction.new do |r|
    r.end_time = record[:played_at]
    r.object = build_track(record[:track])
    r.agent = build_agent(agent)
    r.source = 'spotify'
    r.dedupe_on << %i[type source end_time]
  end
end

#build_track(track) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/chronicle/spotify/builders.rb', line 32

def build_track(track)
  Chronicle::Models::MusicRecording.new do |r|
    r.name = track[:name]
    r.in_album = [build_album(track[:album])]
    r.by_artist = track[:artists].map { |artist| build_artist(artist) }
    r.duration = "PT#{track[:duration_ms] / 1000.0}S"
    r.source = 'spotify'
    r.source_id = track[:id]
    r.dedupe_on = [[:url], %i[source_id source type]]
  end
end