rmd 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.bundle/config +2 -0
- data/.gitignore +5 -0
- data/.rspec +2 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +22 -0
- data/README.md +57 -0
- data/Rakefile +2 -0
- data/bin/rmd +4 -0
- data/fixtures/vcr_cassettes/song_download.yml +186 -0
- data/lib/rmd.rb +8 -0
- data/lib/rmd/downloader.rb +51 -0
- data/lib/rmd/factory.rb +29 -0
- data/lib/rmd/main.rb +12 -0
- data/lib/rmd/nct/getter/base.rb +31 -0
- data/lib/rmd/nct/getter/key_from_page.rb +50 -0
- data/lib/rmd/nct/getter/key_from_url.rb +44 -0
- data/lib/rmd/nct/playlist.rb +63 -0
- data/lib/rmd/nct/song.rb +36 -0
- data/lib/rmd/processor.rb +44 -0
- data/lib/rmd/song_playlist_adapter.rb +20 -0
- data/lib/rmd/version.rb +3 -0
- data/rmd.gemspec +31 -0
- data/spec/rmd/downloader_spec.rb +33 -0
- data/spec/rmd/factory_spec.rb +31 -0
- data/spec/rmd/nct/getter/key_from_page_spec.rb +140 -0
- data/spec/rmd/nct/getter/key_from_url_spec.rb +113 -0
- data/spec/rmd/nct/playlist_spec.rb +100 -0
- data/spec/rmd/nct/song_spec.rb +57 -0
- data/spec/rmd/processor_spec.rb +62 -0
- data/spec/rmd/song_playlist_adapter_spec.rb +35 -0
- data/spec/spec_helper.rb +102 -0
- metadata +211 -0
@@ -0,0 +1,113 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe RMD::NCT::Getter::KeyFromUrl do
|
4
|
+
let(:url) { 'url' }
|
5
|
+
let(:getter) { described_class.new(url) }
|
6
|
+
|
7
|
+
describe '#fetch' do
|
8
|
+
before do
|
9
|
+
expect(getter).to receive(:key).and_return(key)
|
10
|
+
end
|
11
|
+
|
12
|
+
context 'when key is present' do
|
13
|
+
let(:key) { 'key' }
|
14
|
+
let(:stream_url) { 'stream_url' }
|
15
|
+
let(:response) { {
|
16
|
+
'error_message' => error_message,
|
17
|
+
'data' => { 'stream_url' => stream_url }
|
18
|
+
} }
|
19
|
+
|
20
|
+
before do
|
21
|
+
allow(getter).to receive(:response).and_return(response)
|
22
|
+
end
|
23
|
+
|
24
|
+
context 'when response is success' do
|
25
|
+
let(:error_message) { 'Success' }
|
26
|
+
|
27
|
+
it 'gets the link' do
|
28
|
+
getter.fetch
|
29
|
+
expect(getter.data_link).to eq stream_url
|
30
|
+
expect(getter.errors).to eq nil
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'when response is not success' do
|
35
|
+
let(:error_message) { 'error_message' }
|
36
|
+
let(:new_link) { 'new_link' }
|
37
|
+
|
38
|
+
before do
|
39
|
+
expect(getter).to receive(:new_link).and_return(new_link)
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'returns errors' do
|
43
|
+
getter.fetch
|
44
|
+
expect(getter.data_link).to eq nil
|
45
|
+
expect(getter.errors).to eq 'error_message: new_link'
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
context 'when key is not present' do
|
51
|
+
let(:key) { nil }
|
52
|
+
|
53
|
+
it 'returns errors' do
|
54
|
+
getter.fetch
|
55
|
+
expect(getter.data_link).to eq nil
|
56
|
+
expect(getter.errors).to eq 'The url does not contain the key.'
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe '#response' do
|
62
|
+
let(:page) { double('Page', body: body) }
|
63
|
+
let(:body) { "{\"abc\":\"xyz\"}" }
|
64
|
+
let(:response) { { 'abc' => 'xyz' } }
|
65
|
+
subject { getter.send(:response) }
|
66
|
+
|
67
|
+
before do
|
68
|
+
expect(getter).to receive(:page).and_return(page)
|
69
|
+
end
|
70
|
+
|
71
|
+
it { is_expected.to eq response }
|
72
|
+
end
|
73
|
+
|
74
|
+
describe '#key' do
|
75
|
+
subject { getter.send(:key) }
|
76
|
+
|
77
|
+
context 'when there is no match' do
|
78
|
+
let(:url) { 'http://www.nhaccuatui.com/bai-hat/the-clockmaker-vexare.html' }
|
79
|
+
it { is_expected.to eq nil }
|
80
|
+
end
|
81
|
+
|
82
|
+
context 'when there is a match' do
|
83
|
+
let(:url) { 'http://www.nhaccuatui.com/bai-hat/the-clockmaker-vexare.eorOqCpY7k46.html' }
|
84
|
+
it { is_expected.to eq 'eorOqCpY7k46' }
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
describe '#new_link' do
|
89
|
+
let(:key) { 'key' }
|
90
|
+
subject { getter.send(:new_link) }
|
91
|
+
|
92
|
+
before do
|
93
|
+
expect(getter).to receive(:key).and_return(key)
|
94
|
+
end
|
95
|
+
|
96
|
+
it { is_expected.to eq 'http://www.nhaccuatui.com/download/song/key' }
|
97
|
+
end
|
98
|
+
|
99
|
+
describe '#page' do
|
100
|
+
let(:new_link) { 'new_link' }
|
101
|
+
let(:agent) { instance_double('Mechanize') }
|
102
|
+
let(:page) { double('Page') }
|
103
|
+
subject { getter.send(:page) }
|
104
|
+
|
105
|
+
before do
|
106
|
+
expect(getter).to receive(:new_link).and_return(new_link)
|
107
|
+
expect(getter).to receive(:agent).and_return(agent)
|
108
|
+
expect(agent).to receive(:get).with(new_link).and_return(page)
|
109
|
+
end
|
110
|
+
|
111
|
+
it { is_expected.to eq page }
|
112
|
+
end
|
113
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe RMD::NCT::Playlist do
|
4
|
+
let(:playlist) { described_class.new(link) }
|
5
|
+
let(:link) { 'www.nhaccuatui/bai-hat/playlist.mp3' }
|
6
|
+
|
7
|
+
describe '#fetch' do
|
8
|
+
before do
|
9
|
+
allow(playlist).to receive(:song_elements)
|
10
|
+
.and_return(song_elements)
|
11
|
+
end
|
12
|
+
|
13
|
+
context 'when there are song elements' do
|
14
|
+
let(:element) { double('Element') }
|
15
|
+
let(:errors) { 'errors' }
|
16
|
+
let(:song_link) { 'song_link' }
|
17
|
+
let(:song_elements) { [element, element] }
|
18
|
+
let(:song_1) { instance_double('RMD::NCT::Song', success?: true, fetch: nil, data_link: song_link) }
|
19
|
+
let(:song_2) { instance_double('RMD::NCT::Song', success?: false, fetch: nil, errors: errors) }
|
20
|
+
|
21
|
+
before do
|
22
|
+
allow(element).to receive(:attr).with('href').and_return(link)
|
23
|
+
expect(RMD::NCT::Song).to receive(:new).with(link).and_return(song_1, song_2)
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'fetchs all the song from the playlist' do
|
27
|
+
playlist.fetch
|
28
|
+
expect(playlist.songs).to eq [song_link]
|
29
|
+
expect(playlist.errors).to eq [errors]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'when there are no song elements' do
|
34
|
+
let(:song_elements) { [] }
|
35
|
+
|
36
|
+
it 'does not do anything' do
|
37
|
+
playlist.fetch
|
38
|
+
expect(playlist.songs).to eq []
|
39
|
+
expect(playlist.errors).to eq ['Can not get song lists from this playlist page.']
|
40
|
+
end
|
41
|
+
end
|
42
|
+
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
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe RMD::NCT::Song do
|
4
|
+
let(:song) { described_class.new(link) }
|
5
|
+
let(:link) { 'www.nhaccuatui/bai-hat/song.mp3' }
|
6
|
+
|
7
|
+
describe '#fetch' do
|
8
|
+
let(:data_link) { 'data_link' }
|
9
|
+
let(:errors) { 'errors' }
|
10
|
+
let(:getter_1) { instance_double('RMD::NCT::Getter::KeyFromPage',
|
11
|
+
data_link: data_link,
|
12
|
+
errors: errors) }
|
13
|
+
let(:getter_2) { instance_double('RMD::NCT::Getter::KeyFromPage',
|
14
|
+
data_link: data_link,
|
15
|
+
errors: nil) }
|
16
|
+
let(:getter_3) { instance_double('RMD::NCT::Getter::KeyFromPage',
|
17
|
+
data_link: data_link,
|
18
|
+
errors: errors) }
|
19
|
+
let(:getters) { [getter_1, getter_2, getter_3] }
|
20
|
+
|
21
|
+
it 'fetchs songs' do
|
22
|
+
expect(song).to receive(:getters).and_return(getters)
|
23
|
+
expect(getter_1).to receive(:fetch)
|
24
|
+
expect(getter_2).to receive(:fetch)
|
25
|
+
expect(getter_3).not_to receive(:fetch)
|
26
|
+
song.fetch
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe '#success?' do
|
31
|
+
subject { song.success? }
|
32
|
+
|
33
|
+
before do
|
34
|
+
expect(song).to receive(:data_link).and_return(data_link)
|
35
|
+
end
|
36
|
+
|
37
|
+
context 'when datalink is present' do
|
38
|
+
let(:data_link) { 'data_link' }
|
39
|
+
it { is_expected.to eq true }
|
40
|
+
end
|
41
|
+
|
42
|
+
context 'when datalink is not present' do
|
43
|
+
let(:data_link) { nil }
|
44
|
+
it { is_expected.to eq false }
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe '#getters' do
|
49
|
+
let(:getters) { song.send(:getters) }
|
50
|
+
let(:getter_classes) { getters.map(&:class) }
|
51
|
+
|
52
|
+
it 'contains getter' do
|
53
|
+
expect(getter_classes).to include(RMD::NCT::Getter::KeyFromUrl)
|
54
|
+
expect(getter_classes).to include(RMD::NCT::Getter::KeyFromPage)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe RMD::Processor do
|
4
|
+
let(:processor) { described_class.new(link) }
|
5
|
+
let(:link) { 'www.example/xyz/abc.mp3' }
|
6
|
+
|
7
|
+
describe '.process' do
|
8
|
+
let(:link) { 'www.example/xyz/abc.mp3' }
|
9
|
+
let(:processor) { double('RMD::processor') }
|
10
|
+
|
11
|
+
it 'processes' do
|
12
|
+
expect(described_class).to receive(:new).with(link).and_return(processor)
|
13
|
+
expect(processor).to receive(:process)
|
14
|
+
described_class.process(link)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '#process' do
|
19
|
+
let(:errors) { ['errors'] }
|
20
|
+
let(:songs) { [song] }
|
21
|
+
let(:song) { 'song.mp3' }
|
22
|
+
let(:playlist) { instance_double('RMD::NCT::Playlist', songs: songs,
|
23
|
+
errors: errors,
|
24
|
+
success?: success) }
|
25
|
+
|
26
|
+
before do
|
27
|
+
expect(RMD::Factory).to receive(:build).with(link).and_return(playlist)
|
28
|
+
expect(playlist).to receive(:fetch)
|
29
|
+
end
|
30
|
+
|
31
|
+
context 'when fetching songs is success' do
|
32
|
+
let(:success) { true }
|
33
|
+
|
34
|
+
it 'downloads the songs' do
|
35
|
+
expect(processor).to receive(:download).with(song)
|
36
|
+
expect {
|
37
|
+
processor.process
|
38
|
+
}.to output("\e[0;32;49mStart processing #{link}...\e[0m\n\e[0;31;49merrors\e[0m\n").to_stdout
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context 'when fetching songs is not success' do
|
43
|
+
let(:success) { false }
|
44
|
+
|
45
|
+
it 'does not download the songs' do
|
46
|
+
expect(processor).not_to receive(:download)
|
47
|
+
expect {
|
48
|
+
processor.process
|
49
|
+
}.to output("\e[0;32;49mStart processing #{link}...\e[0m\n\e[0;31;49merrors\e[0m\n").to_stdout
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe '#download' do
|
55
|
+
let(:data_link) { 'data_link' }
|
56
|
+
|
57
|
+
it 'downloads data' do
|
58
|
+
expect(RMD::Downloader).to receive(:download).with(data_link)
|
59
|
+
processor.send(:download, data_link)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe RMD::SongPlaylistAdapter do
|
4
|
+
let(:adapter) { described_class.new(song) }
|
5
|
+
|
6
|
+
describe '#songs' do
|
7
|
+
let(:song) { instance_double('RMD::NCT::Song', data_link: data_link) }
|
8
|
+
subject { adapter.songs }
|
9
|
+
|
10
|
+
context 'when song has the link' do
|
11
|
+
let(:data_link) { 'data_link' }
|
12
|
+
it { is_expected.to eq [data_link] }
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'when song does not have the link' do
|
16
|
+
let(:data_link) { nil }
|
17
|
+
it { is_expected.to eq [] }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe '#errors' do
|
22
|
+
let(:song) { instance_double('RMD::NCT::Song', errors: errors) }
|
23
|
+
subject { adapter.errors }
|
24
|
+
|
25
|
+
context 'when song has the error' do
|
26
|
+
let(:errors) { 'errors' }
|
27
|
+
it { is_expected.to eq [errors] }
|
28
|
+
end
|
29
|
+
|
30
|
+
context 'when song does not have the error' do
|
31
|
+
let(:errors) { nil }
|
32
|
+
it { is_expected.to eq [] }
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause this
|
4
|
+
# file to always be loaded, without a need to explicitly require it in any files.
|
5
|
+
#
|
6
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
7
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
8
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
9
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
10
|
+
# a separate helper file that requires the additional dependencies and performs
|
11
|
+
# the additional setup, and require it from the spec files that actually need it.
|
12
|
+
#
|
13
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
14
|
+
# users commonly want.
|
15
|
+
#
|
16
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
17
|
+
require 'byebug'
|
18
|
+
require 'webmock/rspec'
|
19
|
+
require 'vcr'
|
20
|
+
require 'simplecov'
|
21
|
+
require 'rmd'
|
22
|
+
|
23
|
+
SimpleCov.start
|
24
|
+
|
25
|
+
VCR.configure do |config|
|
26
|
+
config.cassette_library_dir = "fixtures/vcr_cassettes"
|
27
|
+
config.hook_into :webmock
|
28
|
+
end
|
29
|
+
|
30
|
+
RSpec.configure do |config|
|
31
|
+
# rspec-expectations config goes here. You can use an alternate
|
32
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
33
|
+
# assertions if you prefer.
|
34
|
+
config.expect_with :rspec do |expectations|
|
35
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
36
|
+
# and `failure_message` of custom matchers include text for helper methods
|
37
|
+
# defined using `chain`, e.g.:
|
38
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
39
|
+
# # => "be bigger than 2 and smaller than 4"
|
40
|
+
# ...rather than:
|
41
|
+
# # => "be bigger than 2"
|
42
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
43
|
+
end
|
44
|
+
|
45
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
46
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
47
|
+
config.mock_with :rspec do |mocks|
|
48
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
49
|
+
# a real object. This is generally recommended, and will default to
|
50
|
+
# `true` in RSpec 4.
|
51
|
+
mocks.verify_partial_doubles = true
|
52
|
+
end
|
53
|
+
|
54
|
+
# The settings below are suggested to provide a good initial experience
|
55
|
+
# with RSpec, but feel free to customize to your heart's content.
|
56
|
+
=begin
|
57
|
+
# These two settings work together to allow you to limit a spec run
|
58
|
+
# to individual examples or groups you care about by tagging them with
|
59
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
60
|
+
# get run.
|
61
|
+
config.filter_run :focus
|
62
|
+
config.run_all_when_everything_filtered = true
|
63
|
+
|
64
|
+
# Limits the available syntax to the non-monkey patched syntax that is recommended.
|
65
|
+
# For more details, see:
|
66
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
67
|
+
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
68
|
+
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
|
69
|
+
config.disable_monkey_patching!
|
70
|
+
|
71
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
72
|
+
# be too noisy due to issues in dependencies.
|
73
|
+
config.warnings = true
|
74
|
+
|
75
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
76
|
+
# file, and it's useful to allow more verbose output when running an
|
77
|
+
# individual spec file.
|
78
|
+
if config.files_to_run.one?
|
79
|
+
# Use the documentation formatter for detailed output,
|
80
|
+
# unless a formatter has already been configured
|
81
|
+
# (e.g. via a command-line flag).
|
82
|
+
config.default_formatter = 'doc'
|
83
|
+
end
|
84
|
+
|
85
|
+
# Print the 10 slowest examples and example groups at the
|
86
|
+
# end of the spec run, to help surface which specs are running
|
87
|
+
# particularly slow.
|
88
|
+
config.profile_examples = 10
|
89
|
+
|
90
|
+
# Run specs in random order to surface order dependencies. If you find an
|
91
|
+
# order dependency and want to debug it, you can fix the order by providing
|
92
|
+
# the seed, which is printed after each run.
|
93
|
+
# --seed 1234
|
94
|
+
config.order = :random
|
95
|
+
|
96
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
97
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
98
|
+
# test failures related to randomization by passing the same `--seed` value
|
99
|
+
# as the one that triggered the failure.
|
100
|
+
Kernel.srand config.seed
|
101
|
+
=end
|
102
|
+
end
|