Class: SWR3::NowPlaying::Mapper

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

Class Method Summary collapse

Class Method Details

.map(json) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/swr3_now_playing/mapper.rb', line 15

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 = j['frontmod'].first['artist']['name']
  title = j['frontmod'].first['title']

  Song.new(artist, title)
end