swr3-nowplaying 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9783529068579b670131facc8f4f5eb4d79975a9
4
- data.tar.gz: f15788d6e83983c2b80092c0ede6f3177ca4b47c
3
+ metadata.gz: f73595e71b4d14aa011032291059b97d024f9962
4
+ data.tar.gz: 569db2c279782ed885ac2b014580e59124758efc
5
5
  SHA512:
6
- metadata.gz: f1c36be7f2e19235e23dcfb20e90d4c17cd43ffcb7fb02a66a521971a11894df8263625fdd0d870b33806856499035877c2bcb41722fa9036f33440d6c967b97
7
- data.tar.gz: 08abfd03f50519d514ece4dfe89b2281939d54e79d84dbd6241836a92a41cf1bbc99485e64469b980cd24462dfc176510fe6b0f3e8838aa915ee2a4bd6f897ab
6
+ metadata.gz: e14886d546be216edcdc2c565db89ee23e299f7cb04627024c5201e5415144d220810b6cc60c9825b8f143bf1cd44d491430a913884015d4ba31ec8ca6fa8b45
7
+ data.tar.gz: 1e0148580f5cd094455193da820394f8af35442801eee418fb162093617bba318d06e5be2ac794ec828ba48830f6cc0a213b1564819efb54c37e0063ca2ae526
data/.travis.yml CHANGED
@@ -2,5 +2,8 @@ language: ruby
2
2
  rvm:
3
3
  - 2.3.3
4
4
  - 2.4.0
5
+ before_install:
6
+ - gem update --system --no-doc
7
+ - gem install bundler
5
8
  bundler_args: --without production
6
9
  script: bundle exec rake test:ci
data/exe/swr3-nowplaying CHANGED
@@ -10,8 +10,8 @@ require 'swr3_now_playing/mapper'
10
10
  json = SWR3::NowPlaying::Loader.load
11
11
 
12
12
  begin
13
- song = SWR3::NowPlaying::Mapper.map(json)
14
- puts "#{Time.new} - #{song}"
13
+ song = SWR3::NowPlaying::SongMapper.map(json)
14
+ puts "#{song.play_date} - #{song}"
15
15
  rescue JSON::ParserError => e
16
16
  warn "Error parsing JSON response: #{e.message}"
17
17
  warn 'Offending document is: '
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+ module SWR3
3
+ module NowPlaying
4
+ class Artist
5
+ attr_reader :name, :link
6
+ alias_method :to_s, :name
7
+
8
+ def initialize(name, link)
9
+ @name = name
10
+ @link = link
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+ module SWR3
3
+ module NowPlaying
4
+ Cover = Struct.new(:small, :thumbnail, :detail, :zoom)
5
+ end
6
+ end
@@ -1,16 +1,39 @@
1
1
  # frozen_string_literal: true
2
2
  require 'json'
3
+ require 'cgi'
3
4
  require 'swr3_now_playing/song'
5
+ require 'swr3_now_playing/artist'
6
+ require 'swr3_now_playing/cover'
4
7
 
5
8
  module SWR3
6
9
  module NowPlaying
10
+ class ArtistMapper
11
+ class << self
12
+ def map(json)
13
+ Artist.new(CGI.unescapeHTML(json['name']), json['link'])
14
+ end
15
+ end
16
+ end
17
+
18
+ class CoverMapper
19
+ class << self
20
+ def map(json)
21
+ Cover.new.tap do |cover|
22
+ %w(small thumbnail detail zoom).each do |size|
23
+ cover[size] = json[size]['src']
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+
7
30
  class MissingNullFixer
8
31
  def fix(stream)
9
32
  stream.gsub(/{ "detail": ,/, '{ "detail": {},')
10
33
  end
11
34
  end
12
35
 
13
- class Mapper
36
+ class SongMapper
14
37
  class << self
15
38
  def map(json)
16
39
  fixers = [MissingNullFixer.new]
@@ -27,10 +50,17 @@ module SWR3
27
50
  end
28
51
  end
29
52
 
30
- artist = j['frontmod'].first['artist']['name']
31
- title = j['frontmod'].first['title']
53
+ artist = ArtistMapper.map(j['frontmod'].first['artist'])
54
+ title = CGI.unescapeHTML(j['frontmod'].first['title'])
55
+ cover = CoverMapper.map(j['frontmod'].first['cover'])
56
+
57
+ Song.new(artist, title, cover).tap do |song|
58
+ time_stamp = j['frontmod'].first['playDate']
32
59
 
33
- Song.new(artist, title)
60
+ if time_stamp
61
+ song.play_date = Time.at(time_stamp / 1000)
62
+ end
63
+ end
34
64
  end
35
65
  end
36
66
  end
@@ -2,20 +2,17 @@
2
2
  module SWR3
3
3
  module NowPlaying
4
4
  class Song
5
- attr_reader :artist, :title
5
+ attr_reader :artist, :title, :cover
6
+ attr_accessor :play_date
6
7
 
7
- def initialize(artist, title)
8
+ def initialize(artist, title, cover=nil)
8
9
  @artist = artist
9
10
  @title = title
10
- end
11
-
12
- def ==(other)
13
- return false if other.nil?
14
- self.artist == other.artist && self.title == other.title
11
+ @cover = cover
15
12
  end
16
13
 
17
14
  def to_s
18
- "#{@artist}: #{@title}"
15
+ "#{artist.name}: #{title}"
19
16
  end
20
17
  end
21
18
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
  module SWR3
3
3
  module NowPlaying
4
- VERSION = '0.0.2'
4
+ VERSION = '0.0.3'
5
5
  end
6
6
  end
data/test/helper.rb CHANGED
@@ -1,2 +1,3 @@
1
1
  # frozen_string_literal: true
2
2
  require 'minitest/autorun'
3
+ require 'minitest/mock'
@@ -21,14 +21,18 @@ class IntegrationTest < MiniTest::Test
21
21
 
22
22
  def setup
23
23
  @song = mocked('setup') do
24
- Mapper.map(Loader.load)
24
+ SongMapper.map(Loader.load)
25
25
  end
26
26
  end
27
27
 
28
- def test_song
28
+ def test_song_title
29
29
  refute_nil(@song)
30
- assert_equal('Billy Talent', @song.artist)
31
30
  assert_equal('Red flag', @song.title)
32
31
  assert_equal('Billy Talent: Red flag', @song.to_s)
33
32
  end
33
+
34
+ def test_artist
35
+ refute_nil(@song)
36
+ assert_equal('Billy Talent', @song.artist.name)
37
+ end
34
38
  end
@@ -1,22 +1,68 @@
1
1
  # frozen_string_literal: true
2
2
  require 'helper'
3
-
4
3
  require 'swr3_now_playing/mapper'
5
4
 
6
5
  class MapperTest < MiniTest::Test
7
6
  def test_good
8
- result = SWR3::NowPlaying::Mapper.map(File.new('test/fixtures/good.json'))
7
+ result = SWR3::NowPlaying::SongMapper.map(File.new('test/fixtures/good.json'))
8
+ refute_nil(result)
9
+ end
10
+
11
+ def test_title
12
+ result = SWR3::NowPlaying::SongMapper.map(File.new('test/fixtures/good.json'))
13
+ assert_equal('Breathing underwater', result.title)
14
+ end
15
+
16
+ def test_artist
17
+ result = SWR3::NowPlaying::SongMapper.map(File.new('test/fixtures/good.json'))
18
+ refute_nil(result)
19
+ refute_nil(result.artist)
20
+ artist = result.artist
21
+ assert_equal('Sandé, Emeli', artist.name)
22
+ assert_equal('http://www.swr3.de/musik/poplexikon/-/id=927882/did=1199388/cf24yn/index.html', artist.link)
23
+ end
24
+
25
+ def test_cover
26
+ result = SWR3::NowPlaying::SongMapper.map(File.new('test/fixtures/good.json'))
9
27
  refute_nil(result)
28
+ refute_nil(result.cover)
29
+
30
+ cover = result.cover
31
+ assert_equal('http://www.swr3.de/-/id=4244326/property=small/width=100/height=100/122v5mf/index.jpg', cover.small)
32
+ assert_equal('http://www.swr3.de/-/id=4244326/property=thumbnail/width=160/height=160/bv9ur/index.jpg', cover.thumbnail)
33
+ assert_equal('http://www.swr3.de/-/id=4244326/property=detail/width=200/height=200/f69z8d/index.jpg', cover.detail)
34
+ assert_equal('http://www.swr3.de/-/id=4244326/property=zoom/width=500/height=500/1ugkxk9/index.jpg', cover.zoom)
10
35
  end
11
36
 
12
37
  def test_fixable
13
- result = SWR3::NowPlaying::Mapper.map(File.new('test/fixtures/fixable.json'))
38
+ result = SWR3::NowPlaying::SongMapper.map(File.new('test/fixtures/fixable.json'))
14
39
  refute_nil(result)
15
40
  end
16
41
 
17
42
  def test_unfixable
18
43
  assert_raises(JSON::ParserError) {
19
- SWR3::NowPlaying::Mapper.map(File.new('test/fixtures/unfixable.json'))
44
+ SWR3::NowPlaying::SongMapper.map(File.new('test/fixtures/unfixable.json'))
20
45
  }
21
46
  end
47
+
48
+ def test_play_date
49
+ result = SWR3::NowPlaying::SongMapper.map(File.new('test/fixtures/good.json'))
50
+ assert_equal(Time.at(1483110825), result.play_date)
51
+ end
52
+
53
+ def test_html_entities_title
54
+ fixture = JSON.parse(File.new('test/fixtures/good.json').read)
55
+ fixture['frontmod'].first['title'] = 'and nothin&#39; on'
56
+ io = StringIO.new(JSON.dump(fixture))
57
+ result = SWR3::NowPlaying::SongMapper.map(io)
58
+ assert_equal("and nothin' on", result.title)
59
+ end
60
+
61
+ def test_html_entities_artist_name
62
+ fixture = JSON.parse(File.new('test/fixtures/good.json').read)
63
+ fixture['frontmod'].first['artist']['name'] = 'Rag&#39;n&#39;bone Man'
64
+ io = StringIO.new(JSON.dump(fixture))
65
+ result = SWR3::NowPlaying::SongMapper.map(io)
66
+ assert_equal("Rag'n'bone Man", result.artist.name)
67
+ end
22
68
  end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+ require 'helper'
3
+ require 'swr3_now_playing/song'
4
+
5
+ class SongTest < MiniTest::Test
6
+ def test_to_s
7
+ artist = Minitest::Mock.new
8
+ artist.expect(:name, 'foo')
9
+
10
+ result = SWR3::NowPlaying::Song.new(artist, 'bar')
11
+ refute_nil(result)
12
+ assert_equal('foo: bar', result.to_s)
13
+ end
14
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: swr3-nowplaying
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicholas E. Rabenau
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-01-06 00:00:00.000000000 Z
11
+ date: 2017-01-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -253,6 +253,8 @@ files:
253
253
  - Rakefile
254
254
  - exe/swr3-nowplaying
255
255
  - lib/swr3_now_playing.rb
256
+ - lib/swr3_now_playing/artist.rb
257
+ - lib/swr3_now_playing/cover.rb
256
258
  - lib/swr3_now_playing/loader.rb
257
259
  - lib/swr3_now_playing/mapper.rb
258
260
  - lib/swr3_now_playing/song.rb
@@ -266,6 +268,7 @@ files:
266
268
  - test/integration/test_all.rb
267
269
  - test/unit/test_loader.rb
268
270
  - test/unit/test_mapper.rb
271
+ - test/unit/test_song.rb
269
272
  homepage: http://github.com/nerab/swr3-nowplaying
270
273
  licenses: []
271
274
  metadata: {}
@@ -298,4 +301,4 @@ test_files:
298
301
  - test/integration/test_all.rb
299
302
  - test/unit/test_loader.rb
300
303
  - test/unit/test_mapper.rb
301
- has_rdoc:
304
+ - test/unit/test_song.rb