lyricfy 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -24,20 +24,25 @@ Or install it yourself as:
24
24
  You need to create an instance of the <code>Lyricfy::Fetcher</code> class and send the <code>#search</code> message with the artist name and song title respectively. The following example shows the most basic usage.
25
25
 
26
26
  fetcher = Lyricfy::Fetcher.new
27
- lyric = fetcher.search 'Coldplay', 'Viva la vida'
28
- lyric.body.each { |line| puts line }
27
+ song = fetcher.search 'Coldplay', 'Viva la vida'
28
+ puts song.body # prints lyrics separated by '\n'
29
+
30
+ You can also pass a custom separator to the <code>#body</code> method. For example:
31
+
32
+ lyrics.body("<br>") # returns a String with the song lyrics separated by a <br> tag
29
33
 
30
- The <code>Lyricfy::Fetcher#search</code> method returns an OpenStruct object with the following methods:
34
+ The <code>Lyricfy::Fetcher#search</code> method returns a <code>Lyricfy::Song</code> object with the following methods:
31
35
 
32
- - artist
33
- - song
36
+ - title
37
+ - author
38
+ - lines
34
39
  - body
35
40
 
36
- Where artist is the artist name, song is the song title and body is an Array of the lyric paragraphs.
41
+ Where title is the song title, author is the song artist, lines is an Array of the lyric paragraphs and body returns a String with the song lyrics separated with <code>\n</code> by default.
37
42
 
38
43
  ### How does it work?
39
44
 
40
- Under the hood, this library fetch the songs lyrics by scraping some websites called "Providers". The currently supported providers are:
45
+ Under the hood, this library fetch the song lyrics by scraping some websites called "Providers". The currently supported providers are:
41
46
 
42
47
  - [Wikia](http://lyrics.wikia.com/Lyrics_Wiki)
43
48
  - [MetroLyrics](http://www.metrolyrics.com/)
@@ -46,7 +51,7 @@ By default this gem will recursively search for the lyric on each of the provide
46
51
 
47
52
  fetcher = Lyricfy::Fetcher.new(:metro_lyrics)
48
53
  lyric = fetcher.search 'Coldplay', 'Viva la vida'
49
- lyric.body.each { |line| puts line }
54
+ puts lyric.body
50
55
 
51
56
  In this case Lyricfy will look for the lyric only on [MetroLyrics](http://www.metrolyrics.com/).
52
57
 
@@ -3,6 +3,7 @@ require "open-uri"
3
3
  require "nokogiri"
4
4
  require_relative "lyricfy/version"
5
5
  require_relative "lyricfy/uri_helper"
6
+ require_relative "lyricfy/song"
6
7
  require_relative "lyricfy/lyric_provider"
7
8
  require_relative "lyricfy/providers/wikia"
8
9
  require_relative "lyricfy/providers/metro_lyrics"
@@ -17,7 +18,7 @@ module Lyricfy
17
18
  metro_lyrics: MetroLyrics
18
19
  }
19
20
 
20
- if !args.empty?
21
+ unless args.empty?
21
22
  passed_providers = {}
22
23
  args.each do |provider|
23
24
  raise Exception unless @providers.has_key?(provider)
@@ -28,16 +29,21 @@ module Lyricfy
28
29
  end
29
30
 
30
31
  def search(artist, song)
31
- result = nil
32
- @providers.each_pair do |provider, klass|
33
- fetcher = klass.new(artist_name: artist, song_name: song)
32
+ key = [artist.downcase, song.downcase]
33
+ @cached_search ||= {}
34
34
 
35
- if lyric_body = fetcher.search
36
- result = OpenStruct.new(artist: artist, song: song, body: lyric_body)
37
- break
35
+ @cached_search.fetch(key) do
36
+ result = nil
37
+ @providers.each_pair do |provider, klass|
38
+ fetcher = klass.new(artist_name: artist, song_name: song)
39
+
40
+ if lyric_body = fetcher.search
41
+ result = Lyricfy::Song.new(song, artist, lyric_body)
42
+ break
43
+ end
38
44
  end
45
+ @cached_search[key] = result
39
46
  end
40
- result
41
47
  end
42
48
  end
43
49
  end
@@ -0,0 +1,15 @@
1
+ module Lyricfy
2
+ class Song
3
+ attr_accessor :title, :author, :lines
4
+
5
+ def initialize(title, author, lines)
6
+ self.title = title
7
+ self.author = author
8
+ self.lines = lines
9
+ end
10
+
11
+ def body(separator = '\n')
12
+ lines.join(separator)
13
+ end
14
+ end
15
+ end
@@ -1,3 +1,3 @@
1
1
  module Lyricfy
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -42,6 +42,17 @@ describe Lyricfy::Fetcher do
42
42
  end
43
43
  end
44
44
  end
45
+
46
+ describe "lyric found" do
47
+ it "should return lyrics" do
48
+ fetcher = Lyricfy::Fetcher.new :metro_lyrics
49
+ VCR.use_cassette('metro_lyrics_200') do
50
+ result = fetcher.search('2pac', 'Life Goes On')
51
+ result.lines.must_be_instance_of Array
52
+ result.body.must_include 'Life as a baller'
53
+ end
54
+ end
55
+ end
45
56
  end
46
57
  end
47
- end
58
+ end
@@ -0,0 +1,31 @@
1
+ require 'test_helper'
2
+
3
+ describe Lyricfy::Song do
4
+ before do
5
+ @song = Lyricfy::Song.new 'Yellow', 'Coldplay', ['Look at the stars', 'Look how they shine for you']
6
+ end
7
+
8
+ [
9
+ :title,
10
+ :author,
11
+ :lines
12
+ ].each do |param|
13
+ it "should respond to #{param}" do
14
+ @song.must_respond_to param
15
+ end
16
+ end
17
+
18
+ describe "#body" do
19
+ describe "with param" do
20
+ it "should use injected separator" do
21
+ @song.body("<br>").must_equal "Look at the stars<br>Look how they shine for you"
22
+ end
23
+ end
24
+
25
+ describe "with no params" do
26
+ it "should use default separator" do
27
+ @song.body.must_equal "Look at the stars\\nLook how they shine for you"
28
+ end
29
+ end
30
+ end
31
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lyricfy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-22 00:00:00.000000000 Z
12
+ date: 2013-05-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
@@ -92,6 +92,7 @@ files:
92
92
  - lib/lyricfy/lyric_provider.rb
93
93
  - lib/lyricfy/providers/metro_lyrics.rb
94
94
  - lib/lyricfy/providers/wikia.rb
95
+ - lib/lyricfy/song.rb
95
96
  - lib/lyricfy/uri_helper.rb
96
97
  - lib/lyricfy/version.rb
97
98
  - lyricfy.gemspec
@@ -102,6 +103,7 @@ files:
102
103
  - test/lyricfy_test.rb
103
104
  - test/providers/metro_lyrics_test.rb
104
105
  - test/providers/wikia_test.rb
106
+ - test/song_test.rb
105
107
  - test/test_helper.rb
106
108
  homepage: https://github.com/javichito/lyricfy
107
109
  licenses: []
@@ -117,7 +119,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
117
119
  version: '0'
118
120
  segments:
119
121
  - 0
120
- hash: 3084054243031445109
122
+ hash: -733643063558142876
121
123
  required_rubygems_version: !ruby/object:Gem::Requirement
122
124
  none: false
123
125
  requirements:
@@ -126,7 +128,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
126
128
  version: '0'
127
129
  segments:
128
130
  - 0
129
- hash: 3084054243031445109
131
+ hash: -733643063558142876
130
132
  requirements: []
131
133
  rubyforge_project:
132
134
  rubygems_version: 1.8.25
@@ -141,4 +143,5 @@ test_files:
141
143
  - test/lyricfy_test.rb
142
144
  - test/providers/metro_lyrics_test.rb
143
145
  - test/providers/wikia_test.rb
146
+ - test/song_test.rb
144
147
  - test/test_helper.rb