rmd 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,18 +14,51 @@ describe RMD::Factory do
14
14
 
15
15
  describe '#build' do
16
16
  let(:factory) { described_class.new(link) }
17
- subject { factory.build }
17
+ subject(:result) { factory.build }
18
18
 
19
19
  context 'when link belongs to nhaccuatui website' do
20
20
  context 'when link is song link' do
21
- let(:link) { 'www.nhaccuatui/bai-hat/song.mp3' }
22
- it { is_expected.to be_a(RMD::SongPlaylistAdapter) }
21
+ let(:link) { 'www.nhaccuatui.com/bai-hat/song.mp3' }
22
+
23
+ it 'returns an adapter' do
24
+ expect(result).to be_a RMD::SongPlaylistAdapter
25
+ expect(result.song).to be_a RMD::NCT::Song
26
+ end
23
27
  end
24
28
 
25
29
  context 'when link is playlist link' do
26
- let(:link) { 'www.nhaccuatui/playlist/bang-kieu' }
30
+ let(:link) { 'www.nhaccuatui.com/playlist/bang-kieu' }
27
31
  it { is_expected.to be_a(RMD::NCT::Playlist) }
28
32
  end
29
33
  end
34
+
35
+ context 'when link belongs to zing website' do
36
+ context 'when link is song link' do
37
+ let(:link) { 'mp3.zing.vn/bai-hat/song.mp3' }
38
+
39
+ it 'returns an adapter' do
40
+ expect(result).to be_a RMD::SongPlaylistAdapter
41
+ expect(result.song).to be_a RMD::Zing::Song
42
+ end
43
+ end
44
+
45
+ context 'when link is playlist link' do
46
+ let(:link) { 'mp3.zing.vn/playlist/bang-kieu' }
47
+ it { is_expected.to be_a(RMD::Zing::Playlist) }
48
+ end
49
+
50
+ context 'when link is playlist link' do
51
+ let(:link) { 'mp3.zing.vn/album/bang-kieu' }
52
+ it { is_expected.to be_a(RMD::Zing::Playlist) }
53
+ end
54
+ end
55
+
56
+ context 'otherwise' do
57
+ let(:link) { '' }
58
+
59
+ it 'raises errors' do
60
+ expect { factory.build }.to raise_error
61
+ end
62
+ end
30
63
  end
31
64
  end
@@ -2,7 +2,10 @@ require 'spec_helper'
2
2
 
3
3
  describe RMD::NCT::Playlist do
4
4
  let(:playlist) { described_class.new(link) }
5
- let(:link) { 'www.nhaccuatui/bai-hat/playlist.mp3' }
5
+ let(:link) { 'www.nhaccuatui.com/bai-hat/playlist.mp3' }
6
+ let(:element_css) { '.item_content .name_song' }
7
+
8
+ it_behaves_like 'RMD::Base::Playlist'
6
9
 
7
10
  describe '#fetch' do
8
11
  before do
@@ -24,7 +27,7 @@ describe RMD::NCT::Playlist do
24
27
  end
25
28
 
26
29
  it 'fetchs all the song from the playlist' do
27
- playlist.fetch
30
+ capture_io { playlist.fetch }
28
31
  expect(playlist.songs).to eq [song_link]
29
32
  expect(playlist.errors).to eq [errors]
30
33
  end
@@ -40,61 +43,4 @@ describe RMD::NCT::Playlist do
40
43
  end
41
44
  end
42
45
  end
43
-
44
- describe '#success?' do
45
- subject { playlist.success? }
46
-
47
- before do
48
- allow(playlist).to receive(:songs).and_return(songs)
49
- end
50
-
51
- context 'when songs is nil' do
52
- let(:songs) { nil }
53
- it { is_expected.to eq false }
54
- end
55
-
56
- context 'when songs is blank' do
57
- let(:songs) { [] }
58
- it { is_expected.to eq false }
59
- end
60
-
61
- context 'otherwise' do
62
- let(:songs) { ['songs'] }
63
- it { is_expected.to eq true }
64
- end
65
- end
66
-
67
- describe '#song_elements' do
68
- let(:page) { double('Page') }
69
- let(:song_element) { double('playlistElement') }
70
- let(:song_elements) { [song_element] }
71
- subject { playlist.send(:song_elements) }
72
-
73
- before do
74
- expect(playlist).to receive(:page).and_return(page)
75
- expect(page).to receive(:search) .with('.item_content .name_song')
76
- .and_return(song_elements)
77
- end
78
-
79
- it { is_expected.to eq song_elements }
80
- end
81
-
82
- describe '#page' do
83
- let(:agent) { instance_double('Mechanize') }
84
- let(:page) { double('Page') }
85
- subject { playlist.send(:page) }
86
-
87
- before do
88
- expect(playlist).to receive(:agent).and_return(agent)
89
- expect(agent).to receive(:get).with(link).and_return(page)
90
- end
91
-
92
- it { is_expected.to eq page }
93
- end
94
-
95
- describe '#agent' do
96
- it 'creates agent' do
97
- expect(playlist.send(:agent)).to be_a(Mechanize)
98
- end
99
- end
100
46
  end
@@ -31,7 +31,7 @@ describe RMD::NCT::Song do
31
31
  subject { song.success? }
32
32
 
33
33
  before do
34
- expect(song).to receive(:data_link).and_return(data_link)
34
+ allow(song).to receive(:data_link).and_return(data_link)
35
35
  end
36
36
 
37
37
  context 'when datalink is present' do
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ describe RMD::Zing::Playlist do
4
+ let(:playlist) { described_class.new(link) }
5
+ let(:link) { 'mp3.zing.vn/bai-hat/playlist.mp3' }
6
+ let(:element_css) { '._btnDownload' }
7
+
8
+ it_behaves_like 'RMD::Base::Playlist'
9
+
10
+ describe '#fetch' do
11
+ before do
12
+ allow(playlist).to receive(:song_elements)
13
+ .and_return(song_elements)
14
+ end
15
+
16
+ context 'when there are song elements' do
17
+ let(:element) { double('Element') }
18
+ let(:errors) { 'errors' }
19
+ let(:song_link) { 'song_link' }
20
+ let(:song_elements) { [element] }
21
+
22
+ before do
23
+ expect(element).to receive(:attr).with('href').and_return(link)
24
+ expect(RMD::Zing::Utils::CorrectUrl).to receive(:correct).with(link).and_return(song_link)
25
+ end
26
+
27
+ it 'fetchs all the song from the playlist' do
28
+ capture_io { playlist.fetch }
29
+ expect(playlist.songs).to eq [song_link]
30
+ expect(playlist.errors).to eq []
31
+ end
32
+ end
33
+
34
+ context 'when there are no song elements' do
35
+ let(:song_elements) { [] }
36
+
37
+ it 'does not do anything' do
38
+ playlist.fetch
39
+ expect(playlist.songs).to eq []
40
+ expect(playlist.errors).to eq ['Can not get song lists from this playlist page.']
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,92 @@
1
+ require 'spec_helper'
2
+
3
+ describe RMD::Zing::Song do
4
+ let(:link) { 'mp3.zing.vn/bai-hat/abc.mp3' }
5
+ let(:song) { described_class.new(link) }
6
+
7
+ describe '#fetch' do
8
+ before do
9
+ expect(song).to receive(:new_link)
10
+ .and_return(new_link).at_least(:once)
11
+ end
12
+
13
+ context 'when new link exists' do
14
+ let(:new_link) { 'mp3.zing.vn' }
15
+ let(:data_link) { 'mp3.zing.vn/data/mp3' }
16
+
17
+ before do
18
+ expect(RMD::Zing::Utils::CorrectUrl).to receive(:correct)
19
+ .with(new_link).and_return(data_link)
20
+ end
21
+
22
+ it 'returns correct new link' do
23
+ song.fetch
24
+ expect(song.data_link).to eq data_link
25
+ expect(song.errors).to be_nil
26
+ end
27
+ end
28
+
29
+ context 'when new link does not exist' do
30
+ let(:new_link) { nil }
31
+
32
+ it 'returns error' do
33
+ song.fetch
34
+ expect(song.data_link).to eq nil
35
+ expect(song.errors).to eq 'Can not get song from this link!'
36
+ end
37
+ end
38
+ end
39
+
40
+ describe '#new_link' do
41
+ let(:page) { double('Page') }
42
+ let(:elements) { [element] }
43
+ let(:element) { double('Element', text: text) }
44
+ subject { song.send(:new_link) }
45
+
46
+ before do
47
+ expect(song).to receive(:page).and_return(page)
48
+ expect(page).to receive(:search).with('.detail-function script').and_return(elements)
49
+ end
50
+
51
+ context 'when match data exists' do
52
+ let(:text) { 'abc http://mp3.zing.vn/download/song/abc/xyz' }
53
+ it { is_expected.to eq 'http://mp3.zing.vn/download/song/abc/xyz' }
54
+ end
55
+
56
+ context 'when match data is not exists' do
57
+ let(:text) { 'abc http://mp3.zing.vn' }
58
+ it { is_expected.to eq nil }
59
+ end
60
+ end
61
+
62
+ describe '#page' do
63
+ let(:agent) { instance_double('Mechanize') }
64
+ let(:page) { double('Page') }
65
+ subject { song.send(:page) }
66
+
67
+ before do
68
+ expect(song).to receive(:agent).and_return(agent)
69
+ expect(agent).to receive(:get).with(link).and_return(page)
70
+ end
71
+
72
+ it { is_expected.to eq page }
73
+ end
74
+
75
+ describe '#success?' do
76
+ subject { song.success? }
77
+
78
+ before do
79
+ allow(song).to receive(:data_link).and_return(data_link)
80
+ end
81
+
82
+ context 'when datalink is present' do
83
+ let(:data_link) { 'data_link' }
84
+ it { is_expected.to eq true }
85
+ end
86
+
87
+ context 'when datalink is not present' do
88
+ let(:data_link) { nil }
89
+ it { is_expected.to eq false }
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe RMD::Zing::Utils::CorrectUrl do
4
+ describe '#correct' do
5
+ let(:util) { described_class.new(url) }
6
+
7
+ context 'when url is normal' do
8
+ let(:url) { 'http://www.nhaccuatui.com/download/song/tFBwsJKDGQX9' }
9
+
10
+ it 'returns that url' do
11
+ VCR.use_cassette('utils correct_url correct') do
12
+ expect(util.correct).to eq url
13
+ end
14
+ end
15
+ end
16
+
17
+ context 'when url is a redirect' do
18
+ let(:url) { 'http://mp3.zing.vn/download/song/Bird-TV-Size-Yuya-Matsushita/ZHJntZGsBidFQQQTLDxyDHkG' }
19
+ let(:redirect_url) { 'http://dl2.mp3.zdn.vn/fsdd1131lwwjA/74548e9982bf8c91fdbf7939139a5bb0/54ba2490/2011/06/05/d/f/dfed7ed9a9ed91387af6062db606e72f.mp3?filename=Bird%20TV%20Size%20-%20Yuya%20Matsushita.mp3' }
20
+
21
+ it 'returns that url' do
22
+ VCR.use_cassette('utils correct_url redirect') do
23
+ expect(util.correct).to eq redirect_url
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
data/spec/spec_helper.rb CHANGED
@@ -18,8 +18,11 @@ require 'byebug'
18
18
  require 'webmock/rspec'
19
19
  require 'vcr'
20
20
  require 'simplecov'
21
+ require 'coveralls'
21
22
  require 'rmd'
23
+ Dir[__dir__ + '/support/**/*.rb'].each { |f| require f }
22
24
 
25
+ Coveralls.wear!
23
26
  SimpleCov.start
24
27
 
25
28
  VCR.configure do |config|
@@ -100,3 +103,22 @@ RSpec.configure do |config|
100
103
  Kernel.srand config.seed
101
104
  =end
102
105
  end
106
+
107
+ def capture_io(&block)
108
+ original_stdout = $stdout
109
+ original_progressbar_stdout = ProgressBar::Output::DEFAULT_OUTPUT_STREAM
110
+ $stdout = fake = StringIO.new
111
+ redef_without_warning(ProgressBar::Output, 'DEFAULT_OUTPUT_STREAM', $stdout)
112
+ begin
113
+ yield
114
+ ensure
115
+ redef_without_warning(ProgressBar::Output, 'DEFAULT_OUTPUT_STREAM', original_progressbar_stdout)
116
+ $stdout = original_stdout
117
+ end
118
+ fake.string
119
+ end
120
+
121
+ def redef_without_warning(klass, const, value)
122
+ klass.send(:remove_const, const) if klass.const_defined?(const)
123
+ klass.const_set(const, value)
124
+ end
@@ -0,0 +1,9 @@
1
+ shared_examples 'download' do
2
+ it 'downloads' do
3
+ VCR.use_cassette(scenario) do
4
+ capture_io { run_method }
5
+ expect(File.exists?(file_path)).to eq true
6
+ end
7
+ File.delete(file_path)
8
+ end
9
+ end
@@ -0,0 +1,58 @@
1
+ shared_examples 'RMD::Base::Playlist' do
2
+ describe '#success?' do
3
+ subject { playlist.success? }
4
+
5
+ before do
6
+ allow(playlist).to receive(:songs).and_return(songs)
7
+ end
8
+
9
+ context 'when songs is nil' do
10
+ let(:songs) { nil }
11
+ it { is_expected.to eq false }
12
+ end
13
+
14
+ context 'when songs is blank' do
15
+ let(:songs) { [] }
16
+ it { is_expected.to eq false }
17
+ end
18
+
19
+ context 'otherwise' do
20
+ let(:songs) { ['songs'] }
21
+ it { is_expected.to eq true }
22
+ end
23
+ end
24
+
25
+ describe '#song_elements' do
26
+ let(:page) { double('Page') }
27
+ let(:song_element) { double('playlistElement') }
28
+ let(:song_elements) { [song_element] }
29
+ subject { playlist.send(:song_elements) }
30
+
31
+ before do
32
+ expect(playlist).to receive(:page).and_return(page)
33
+ expect(page).to receive(:search) .with(element_css)
34
+ .and_return(song_elements)
35
+ end
36
+
37
+ it { is_expected.to eq song_elements }
38
+ end
39
+
40
+ describe '#page' do
41
+ let(:agent) { instance_double('Mechanize') }
42
+ let(:page) { double('Page') }
43
+ subject { playlist.send(:page) }
44
+
45
+ before do
46
+ expect(playlist).to receive(:agent).and_return(agent)
47
+ expect(agent).to receive(:get).with(link).and_return(page)
48
+ end
49
+
50
+ it { is_expected.to eq page }
51
+ end
52
+
53
+ describe '#agent' do
54
+ it 'creates agent' do
55
+ expect(playlist.send(:agent)).to be_a(Mechanize)
56
+ end
57
+ end
58
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rmd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hieu Nguyen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-13 00:00:00.000000000 Z
11
+ date: 2015-01-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '1.6'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '10.1'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '10.1'
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: rspec
85
99
  requirement: !ruby/object:Gem::Requirement
@@ -147,13 +161,22 @@ files:
147
161
  - ".bundle/config"
148
162
  - ".gitignore"
149
163
  - ".rspec"
164
+ - ".travis.yml"
150
165
  - Gemfile
151
166
  - LICENSE.txt
152
167
  - README.md
153
168
  - Rakefile
154
169
  - bin/rmd
170
+ - fixtures/vcr_cassettes/nhaccuatui_playlist.yml
171
+ - fixtures/vcr_cassettes/nhaccuatui_song.yml
155
172
  - fixtures/vcr_cassettes/song_download.yml
173
+ - fixtures/vcr_cassettes/utils_correct_url_correct.yml
174
+ - fixtures/vcr_cassettes/utils_correct_url_redirect.yml
175
+ - fixtures/vcr_cassettes/zing_playlist.yml
176
+ - fixtures/vcr_cassettes/zing_song.yml
156
177
  - lib/rmd.rb
178
+ - lib/rmd/base/playlist.rb
179
+ - lib/rmd/base/song.rb
157
180
  - lib/rmd/downloader.rb
158
181
  - lib/rmd/factory.rb
159
182
  - lib/rmd/main.rb
@@ -165,7 +188,11 @@ files:
165
188
  - lib/rmd/processor.rb
166
189
  - lib/rmd/song_playlist_adapter.rb
167
190
  - lib/rmd/version.rb
191
+ - lib/rmd/zing/playlist.rb
192
+ - lib/rmd/zing/song.rb
193
+ - lib/rmd/zing/utils/correct_url.rb
168
194
  - rmd.gemspec
195
+ - spec/acceptants/main_spec.rb
169
196
  - spec/rmd/downloader_spec.rb
170
197
  - spec/rmd/factory_spec.rb
171
198
  - spec/rmd/nct/getter/key_from_page_spec.rb
@@ -174,7 +201,12 @@ files:
174
201
  - spec/rmd/nct/song_spec.rb
175
202
  - spec/rmd/processor_spec.rb
176
203
  - spec/rmd/song_playlist_adapter_spec.rb
204
+ - spec/rmd/zing/playlist_spec.rb
205
+ - spec/rmd/zing/song_spec.rb
206
+ - spec/rmd/zing/utils/correct_url_spec.rb
177
207
  - spec/spec_helper.rb
208
+ - spec/support/download_share_examples.rb
209
+ - spec/support/playlist_share_examples.rb
178
210
  homepage: https://github.com/hieuk09/rmd
179
211
  licenses:
180
212
  - MIT
@@ -200,6 +232,7 @@ signing_key:
200
232
  specification_version: 4
201
233
  summary: 'RMD: Ruby Music Dowloader'
202
234
  test_files:
235
+ - spec/acceptants/main_spec.rb
203
236
  - spec/rmd/downloader_spec.rb
204
237
  - spec/rmd/factory_spec.rb
205
238
  - spec/rmd/nct/getter/key_from_page_spec.rb
@@ -208,4 +241,9 @@ test_files:
208
241
  - spec/rmd/nct/song_spec.rb
209
242
  - spec/rmd/processor_spec.rb
210
243
  - spec/rmd/song_playlist_adapter_spec.rb
244
+ - spec/rmd/zing/playlist_spec.rb
245
+ - spec/rmd/zing/song_spec.rb
246
+ - spec/rmd/zing/utils/correct_url_spec.rb
211
247
  - spec/spec_helper.rb
248
+ - spec/support/download_share_examples.rb
249
+ - spec/support/playlist_share_examples.rb