Class: SWR3::NowPlaying::SongMapper

Inherits:
Object
  • Object
show all
Defined in:
lib/swr3_now_playing/mapper.rb

Class Method Summary collapse

Class Method Details

.map(json) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/swr3_now_playing/mapper.rb', line 38

def map(json)
  fixers = [MissingNullFixer.new]
  stream = json.read

  begin
    j = JSON.parse(stream)
  rescue JSON::ParserError
    if fixers.empty?
      raise
    else
      stream = fixers.pop.fix(stream)
      retry
    end
  end

  artist = ArtistMapper.map(j['frontmod'].first['artist'])
  title = CGI.unescapeHTML(j['frontmod'].first['title'])
  cover = CoverMapper.map(j['frontmod'].first['cover'])

  Song.new(artist, title, cover).tap do |song|
    time_stamp = j['frontmod'].first['playDate']

    if time_stamp
      song.play_date = Time.at(time_stamp / 1000)
    end
  end
end