Module: Radio5::Client::Tracks::Parser

Extended by:
Utils
Defined in:
lib/radio5/client/tracks.rb

Constant Summary

Constants included from Radio5::Constants

Radio5::Constants::ASSET_HOST, Radio5::Constants::DECADES, Radio5::Constants::IMAGE_SIZES, Radio5::Constants::MAX_PAGE_SIZE, Radio5::Constants::MOODS, Radio5::Constants::MOODS_MAPPING, Radio5::Constants::USER_TRACK_STATUSES, Radio5::Constants::USER_TRACK_STATUSES_MAPPING

Class Method Summary collapse

Methods included from Utils

create_asset_url, normalize_string, parse_asset_url, parse_image_urls, parse_json, parse_time_string, parse_unix_timestamp, stringify_moods, stringify_user_track_status, symbolize_mood, symbolize_user_track_status

Class Method Details

.track_audio(json, format) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/radio5/client/tracks.rb', line 98

def self.track_audio(json, format)
  url = json.fetch(:links).fetch(format)
  url.gsub!(/#t=\d*,\d+/, "") # remove play time limit

  expires_at_unix = Integer(url[/(?<=expires=)\d+/])
  expires_at = parse_unix_timestamp(expires_at_unix)

  {
    url: url,
    expires_at: expires_at
  }
end

.track_info(json) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/radio5/client/tracks.rb', line 63

def self.track_info(json)
  created_node = json[:created]
  created_at = created_node && parse_time_string(created_node.fetch(:date))
  created_by = created_node ? created_node.fetch(:user_id) : json.fetch(:profile_id)

  cover_node = json[:image] || json[:cover]
  cover_url = parse_image_urls(cover_node, entity: :track)

  audio = {
    mpeg: track_audio(json, :mpeg),
    ogg:  track_audio(json, :ogg)
  }

  {
    id:          json.fetch(:_id),
    uuid:        json.fetch(:uuid),
    artist:      normalize_string(json.fetch(:artist)),
    title:       normalize_string(json.fetch(:title)),
    album:       normalize_string(json[:album]),
    year:        normalize_string(json.fetch(:year)),
    label:       normalize_string(json[:label]),
    songwriter:  normalize_string(json[:songwriter]),
    length:      json.fetch(:length),
    info:        normalize_string(json[:info]),
    cover_url:   cover_url,
    audio:       audio,
    decade:      json.fetch(:decade),
    mood:        symbolize_mood(json.fetch(:mood)),
    country:     json.fetch(:country),
    like_count:  json.fetch(:likes),
    created_at:  created_at,
    created_by:  created_by
  }
end