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 +4 -4
- data/.travis.yml +3 -0
- data/exe/swr3-nowplaying +2 -2
- data/lib/swr3_now_playing/artist.rb +14 -0
- data/lib/swr3_now_playing/cover.rb +6 -0
- data/lib/swr3_now_playing/mapper.rb +34 -4
- data/lib/swr3_now_playing/song.rb +5 -8
- data/lib/swr3_now_playing/version.rb +1 -1
- data/test/helper.rb +1 -0
- data/test/integration/test_all.rb +7 -3
- data/test/unit/test_mapper.rb +50 -4
- data/test/unit/test_song.rb +14 -0
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f73595e71b4d14aa011032291059b97d024f9962
|
4
|
+
data.tar.gz: 569db2c279782ed885ac2b014580e59124758efc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e14886d546be216edcdc2c565db89ee23e299f7cb04627024c5201e5415144d220810b6cc60c9825b8f143bf1cd44d491430a913884015d4ba31ec8ca6fa8b45
|
7
|
+
data.tar.gz: 1e0148580f5cd094455193da820394f8af35442801eee418fb162093617bba318d06e5be2ac794ec828ba48830f6cc0a213b1564819efb54c37e0063ca2ae526
|
data/.travis.yml
CHANGED
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::
|
14
|
-
puts "#{
|
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: '
|
@@ -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
|
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']
|
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
|
-
|
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
|
-
|
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
|
-
"#{
|
15
|
+
"#{artist.name}: #{title}"
|
19
16
|
end
|
20
17
|
end
|
21
18
|
end
|
data/test/helper.rb
CHANGED
@@ -21,14 +21,18 @@ class IntegrationTest < MiniTest::Test
|
|
21
21
|
|
22
22
|
def setup
|
23
23
|
@song = mocked('setup') do
|
24
|
-
|
24
|
+
SongMapper.map(Loader.load)
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
|
-
def
|
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
|
data/test/unit/test_mapper.rb
CHANGED
@@ -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::
|
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::
|
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::
|
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' 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'n'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.
|
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-
|
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
|
-
|
304
|
+
- test/unit/test_song.rb
|