musix_match 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +1 -1
- data/lib/musix_match.rb +7 -1
- data/lib/musix_match/api/base.rb +4 -1
- data/lib/musix_match/instant_lyrics.rb +45 -0
- data/spec/api_base_spec.rb +47 -0
- data/spec/musix_match_spec.rb +11 -0
- metadata +7 -6
- data/spec/base_model_spec.rb +0 -13
data/README.rdoc
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
| | _ _ | |
|
5
5
|
| | | | | | | |
|
6
6
|
| | _ _ | | | | _ _ | |
|
7
|
-
| || || || | MusixMatch is wrapper for the musixmatch.com API's.
|
7
|
+
| || || || | MusixMatch is a wrapper for the musixmatch.com API's. | || || || |
|
8
8
|
| || || || | With this library you can search for lyrics and tracks | || || || |
|
9
9
|
| | || || | using the http://musixmatch.com service. | | || || |
|
10
10
|
| | | |
|
data/lib/musix_match.rb
CHANGED
@@ -19,7 +19,8 @@ musix_match_path = File.dirname(__FILE__)
|
|
19
19
|
'lyrics_find_result',
|
20
20
|
'track_find_result',
|
21
21
|
'api/finder',
|
22
|
-
'models/track'
|
22
|
+
'models/track',
|
23
|
+
'instant_lyrics'].each do |lib|
|
23
24
|
require musix_match_path + '/musix_match/' + lib
|
24
25
|
end
|
25
26
|
|
@@ -39,4 +40,9 @@ module MusixMatch
|
|
39
40
|
def self.search_track(*args)
|
40
41
|
Models::Track.search(*args)
|
41
42
|
end
|
43
|
+
|
44
|
+
def self.i_m_feeling_lucky(q)
|
45
|
+
result = MusixMatch::InstantLyrics::Search.search(q)
|
46
|
+
result.found? ? result.lyrics.lyrics_body : 'Lyrics not found'
|
47
|
+
end
|
42
48
|
end
|
data/lib/musix_match/api/base.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
module MusixMatch
|
2
2
|
module API
|
3
|
+
class AuthenticationFailedException < Exception; end
|
3
4
|
class APIKeyNotSpecifiedException < Exception; end
|
4
5
|
|
5
6
|
class Base
|
@@ -24,12 +25,14 @@ module MusixMatch
|
|
24
25
|
def self.get(method, params={})
|
25
26
|
raise APIKeyNotSpecifiedException.new('You must specify the API key. MusixMatch::API::Base.api_key = "YOUR_API_KEY"') if api_key.nil?
|
26
27
|
response = HTTParty.get(url_for(method, params))
|
27
|
-
case response.parsed_response
|
28
|
+
parsed_response = case response.parsed_response
|
28
29
|
when Hash
|
29
30
|
response.parsed_response
|
30
31
|
when String
|
31
32
|
JSON.parse(response.parsed_response)
|
32
33
|
end
|
34
|
+
raise AuthenticationFailedException.new('Authentication failed, probably because of a bad API key') if parsed_response['message']['header']['status_code'] == 401
|
35
|
+
parsed_response
|
33
36
|
end
|
34
37
|
|
35
38
|
def api_key
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module MusixMatch
|
2
|
+
module InstantLyrics
|
3
|
+
class Result
|
4
|
+
attr_reader :lyrics
|
5
|
+
|
6
|
+
def initialize(lyrics)
|
7
|
+
@lyrics = lyrics
|
8
|
+
end
|
9
|
+
|
10
|
+
def found?
|
11
|
+
@lyrics.is_a?(MusixMatch::Models::Lyrics)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class Search
|
16
|
+
def initialize(q)
|
17
|
+
@q = q
|
18
|
+
end
|
19
|
+
|
20
|
+
def start
|
21
|
+
lyrics_id = search_lyrics
|
22
|
+
lyrics = find_lyrics(lyrics_id) if lyrics_id
|
23
|
+
Result.new(lyrics)
|
24
|
+
end
|
25
|
+
|
26
|
+
def search_lyrics
|
27
|
+
result = MusixMatch.search_lyrics(:q => @q)
|
28
|
+
if result.status_code == 200 && lyrics = result.lyrics_list.first
|
29
|
+
lyrics.lyrics_id
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def find_lyrics(lyrics_id)
|
34
|
+
result = MusixMatch.get_lyrics(lyrics_id)
|
35
|
+
if result.status_code == 200 && lyrics = result.lyrics
|
36
|
+
lyrics
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.search(q)
|
41
|
+
s = Search.new(q).start
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'API::Base' do
|
4
|
+
it "should always use JSON as format" do
|
5
|
+
expected_url = MusixMatch::API::Base::API_URL + '/lyrics.get?apikey=&format=json'
|
6
|
+
MusixMatch::API::Base.url_for('lyrics.get', :format => 'xml').should == expected_url
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should use JSON as format even if the format key is a string" do
|
10
|
+
expected_url = MusixMatch::API::Base::API_URL + '/lyrics.get?apikey=&format=json'
|
11
|
+
MusixMatch::API::Base.url_for('lyrics.get', 'format' => 'xml').should == expected_url
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should raise an exception if api_key is not set" do
|
15
|
+
lambda do
|
16
|
+
MusixMatch::API::Base.get('http://localhost')
|
17
|
+
end.should raise_error(MusixMatch::API::APIKeyNotSpecifiedException)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should call get on HTTParty" do
|
21
|
+
MusixMatch::API::Base.api_key = 'API_KEY'
|
22
|
+
method = 'lyrics.search'
|
23
|
+
params = {:q_artist => 'artist name'}
|
24
|
+
url = MusixMatch::API::Base.url_for(method, params)
|
25
|
+
response = mock(HTTParty::Response)
|
26
|
+
parsed_response = {'message' => {'header' => {'status_code' => 200}}}
|
27
|
+
response.should_receive(:parsed_response).twice.and_return(parsed_response)
|
28
|
+
HTTParty.should_receive(:get).with(url).and_return(response)
|
29
|
+
result = MusixMatch::API::Base.get(method, params)
|
30
|
+
result.should == parsed_response
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should raise an error" do
|
34
|
+
lambda do
|
35
|
+
MusixMatch::API::Base.api_key = 'API_KEY'
|
36
|
+
method = 'lyrics.search'
|
37
|
+
params = {:q_artist => 'artist name'}
|
38
|
+
url = MusixMatch::API::Base.url_for(method, params)
|
39
|
+
response = mock(HTTParty::Response)
|
40
|
+
parsed_response = {'message' => {'header' => {'status_code' => 401}}}
|
41
|
+
response.should_receive(:parsed_response).twice.and_return(parsed_response)
|
42
|
+
HTTParty.should_receive(:get).with(url).and_return(response)
|
43
|
+
result = MusixMatch::API::Base.get(method, params)
|
44
|
+
result.should == parsed_response
|
45
|
+
end.should raise_error(MusixMatch::API::AuthenticationFailedException)
|
46
|
+
end
|
47
|
+
end
|
data/spec/musix_match_spec.rb
CHANGED
@@ -24,4 +24,15 @@ describe 'MusixMatch' do
|
|
24
24
|
MusixMatch::Models::Track.should_receive(:search).with(params)
|
25
25
|
MusixMatch.search_track(params)
|
26
26
|
end
|
27
|
+
|
28
|
+
it "should call search on InstantLyrics::Search" do
|
29
|
+
q = 'artist name track name'
|
30
|
+
result = mock(MusixMatch::InstantLyrics::Result)
|
31
|
+
lyrics = mock(MusixMatch::Models::Lyrics)
|
32
|
+
lyrics.should_receive(:lyrics_body).and_return('lyrics body')
|
33
|
+
result.should_receive(:found?).and_return(true)
|
34
|
+
result.should_receive(:lyrics).and_return(lyrics)
|
35
|
+
MusixMatch::InstantLyrics::Search.should_receive(:search).with(q).and_return(result)
|
36
|
+
MusixMatch.i_m_feeling_lucky(q)
|
37
|
+
end
|
27
38
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: musix_match
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 29
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 3
|
10
|
+
version: 0.1.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Andrea Franz
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-09-
|
18
|
+
date: 2010-09-26 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -65,6 +65,7 @@ files:
|
|
65
65
|
- lib/musix_match/api/base.rb
|
66
66
|
- lib/musix_match/api/finder.rb
|
67
67
|
- lib/musix_match/api/search.rb
|
68
|
+
- lib/musix_match/instant_lyrics.rb
|
68
69
|
- lib/musix_match/lyrics_find_result.rb
|
69
70
|
- lib/musix_match/lyrics_search_result.rb
|
70
71
|
- lib/musix_match/models/lyrics.rb
|
@@ -72,7 +73,7 @@ files:
|
|
72
73
|
- lib/musix_match/models/track.rb
|
73
74
|
- lib/musix_match/track_find_result.rb
|
74
75
|
- lib/musix_match/track_search_result.rb
|
75
|
-
- spec/
|
76
|
+
- spec/api_base_spec.rb
|
76
77
|
- spec/finder_spec.rb
|
77
78
|
- spec/lyrics_spec.rb
|
78
79
|
- spec/musix_match_spec.rb
|
@@ -115,7 +116,7 @@ signing_key:
|
|
115
116
|
specification_version: 3
|
116
117
|
summary: API wrapper for musixmatch.com API's
|
117
118
|
test_files:
|
118
|
-
- spec/
|
119
|
+
- spec/api_base_spec.rb
|
119
120
|
- spec/finder_spec.rb
|
120
121
|
- spec/lyrics_spec.rb
|
121
122
|
- spec/musix_match_spec.rb
|
data/spec/base_model_spec.rb
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe 'Base Model' do
|
4
|
-
it "should always use JSON as format" do
|
5
|
-
expected_url = MusixMatch::API::Base::API_URL + '/lyrics.get?apikey=&format=json'
|
6
|
-
MusixMatch::API::Base.url_for('lyrics.get', :format => 'xml').should == expected_url
|
7
|
-
end
|
8
|
-
|
9
|
-
it "should use JSON as format even if the format key is a string" do
|
10
|
-
expected_url = MusixMatch::API::Base::API_URL + '/lyrics.get?apikey=&format=json'
|
11
|
-
MusixMatch::API::Base.url_for('lyrics.get', 'format' => 'xml').should == expected_url
|
12
|
-
end
|
13
|
-
end
|