lyrics_finder 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cc26dd72ab5b067f0b65825a0cf201814d78c435
4
- data.tar.gz: 193e8cccb5a6017c1e6a064744bd24ab52763cda
3
+ metadata.gz: 57cc0f1e4ad122a9fde9703f7a422db906bda1b2
4
+ data.tar.gz: ec34c7b3f0598f39dcd1073bfb42d086df8cc629
5
5
  SHA512:
6
- metadata.gz: 979ecf3aa994a3091e79c9821d6fecc227f7286da1a728b6f314db0215971df116ec02bc3c63dc719989ca20f945b79c69953a181eb9b7483a9ff4aa98433071
7
- data.tar.gz: 65747552f08a99b6e3935fdb7bffa1060fd1d99ff492cc7a0b8473d5a3ebba4f30e04f15c0f8ffbb9d852ad0844116a38f06307f31a9eb62fd6a5f65488d1c42
6
+ metadata.gz: 04d13064bd6d959785a314df9fa1943aa6b77c9d22887040addb62014ca7fa82311d3c6d78b7321ace0e24500e495f247d1354a0f8a341ca8f767197ae51b98e
7
+ data.tar.gz: f5c787f0727f686cafaf6611b6208a29d8d72c6ef262e8a2d9e10ac624eb17134e0dbe79bfef04ebdf4e824f29feca7c21419d1a6afc9aeba160b3a21e106200
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # LyricsFinder [![Build Status](https://travis-ci.org/dnlR/lyrics_finder.svg?branch=master)](https://travis-ci.org/dnlR/lyrics_finder) [![Coverage Status](https://img.shields.io/coveralls/dnlR/lyrics_finder.svg)](https://coveralls.io/r/dnlR/lyrics_finder?branch=master) [![endorse](https://api.coderwall.com/dnlr/endorsecount.png)](https://coderwall.com/dnlr)
1
+ # LyricsFinder [![Gem Version](https://badge.fury.io/rb/lyrics_finder.svg)](http://badge.fury.io/rb/lyrics_finder) [![Build Status](https://travis-ci.org/dnlR/lyrics_finder.svg?branch=master)](https://travis-ci.org/dnlR/lyrics_finder) [![Coverage Status](https://img.shields.io/coveralls/dnlR/lyrics_finder.svg)](https://coveralls.io/r/dnlR/lyrics_finder?branch=master) [![endorse](https://api.coderwall.com/dnlr/endorsecount.png)](https://coderwall.com/dnlr)
2
2
 
3
3
  Simple library to search for song lyrics
4
4
 
@@ -18,28 +18,28 @@ Or install it yourself as:
18
18
 
19
19
  ## Hello World!
20
20
 
21
- Create an instance of `LyricsFinder::Fetcher`:
21
+ Create an instance of `Finder`:
22
22
 
23
23
  ```ruby
24
- fetcher = LyricsFinder::Fetcher.new
24
+ finder = Finder.new
25
25
  ```
26
26
 
27
27
  You can specify which websites are you going to get the lyrics from:
28
28
 
29
29
  ```ruby
30
- fetcher = LyricsFinder::Fetcher.new(:song_lyrics, :azlyrics)
30
+ finder = Finder.new(:song_lyrics, :azlyrics)
31
31
  ```
32
32
 
33
33
  You can choose among the following:
34
34
 
35
- - LyricsWikia
36
- - SongLyrics
37
- - AZLyrics
35
+ - LyricsWikia (`:lyrics_wikia`)
36
+ - SongLyrics (`:song_lyrics`)
37
+ - AZLyrics (`:azlyrics`)
38
38
 
39
- And search passing the author and the song title as parameters to `Fetcher#search`:
39
+ And search passing the author and the song title as parameters to `Finder#search`:
40
40
 
41
41
  ```ruby
42
- fetcher.search 'idina menzel', 'let it go'
42
+ finder.search 'idina menzel', 'let it go'
43
43
  ```
44
44
  Which will return and array with all the verses of the song as strings, or `nil` if it cannot found the song in any of the websites.
45
45
 
@@ -49,8 +49,8 @@ In your ruby apps:
49
49
  ```ruby
50
50
  require 'lyrics_finder'
51
51
 
52
- fetcher = LyricsFinder::Fetcher.new
53
- @song = fetcher.search 'idina menzel', 'let it go'
52
+ finder = Finder.new
53
+ @song = finder.search 'idina menzel', 'let it go'
54
54
  puts @song
55
55
  ```
56
56
 
@@ -62,9 +62,15 @@ LyricsFinder is also available as a command-line tool.
62
62
 
63
63
  ## Changelog
64
64
 
65
+ v 0.0.2
66
+
67
+ - Changed the app interface `LyricsFinder::Fetcher` is now `Finder`.
68
+ - Now using [Contracts](https://github.com/egonSchiele/contracts.ruby).
69
+ - Simplified providers code.
70
+
65
71
  v 0.0.1
66
72
 
67
- - Initial release
73
+ - Initial release.
68
74
 
69
75
  ## Contributing
70
76
 
@@ -72,8 +78,4 @@ v 0.0.1
72
78
  2. Create your feature branch (`git checkout -b my-new-feature`)
73
79
  3. Commit your changes (`git commit -am 'Add some feature'`)
74
80
  4. Push to the branch (`git push origin my-new-feature`)
75
- 5. Create a new Pull Request
76
-
77
- ## Credits
78
-
79
- Inspired by [Lyricfy](https://github.com/javichito/Lyricfy).
81
+ 5. Create a new Pull Request
data/bin/lyricsfinder CHANGED
@@ -1,3 +1,3 @@
1
1
  #!/usr/bin/env ruby
2
2
  require 'lyrics_finder/cli'
3
- LyricsFinder::CLI.start
3
+ CLI.start
@@ -1,14 +1,12 @@
1
1
  require 'thor'
2
2
  require 'lyrics_finder'
3
3
 
4
- module LyricsFinder
5
- class CLI < Thor
6
- desc 'search -a Author -t Song Title', 'Search the lyrics for the specified author and title'
7
- method_option 'author', :aliases => '-a', :type => :string
8
- method_option 'title', :aliases => '-t', :type => :string
9
- def search
10
- fetcher = LyricsFinder::Fetcher.new
11
- puts fetcher.search(options[:author], options[:title])
12
- end
4
+ class CLI < Thor
5
+ desc 'search -a Author -t Song Title', 'Search the lyrics for the specified author and title'
6
+ method_option 'author', :aliases => '-a', :type => :string
7
+ method_option 'title', :aliases => '-t', :type => :string
8
+ def search
9
+ finder = Finder.new
10
+ puts finder.search(options[:author], options[:title])
13
11
  end
14
12
  end
@@ -4,7 +4,6 @@ require 'active_support/core_ext'
4
4
  require 'i18n'
5
5
  I18n.enforce_available_locales = false
6
6
  require 'contracts'
7
- include Contracts
8
7
 
9
8
  require_relative 'version'
10
9
  require_relative 'providers'
@@ -1,19 +1,14 @@
1
- module LyricsFinder::Providers::Azlyrics
2
- def format_url(author, title)
3
- author = I18n.transliterate(author.strip.gsub(" ", ""))
4
- title = I18n.transliterate(title.strip.gsub(" ", ""))
5
- "http://www.azlyrics.com/lyrics/#{author}/#{title}.html"
1
+ module Providers::Azlyrics
2
+ include Contracts
3
+
4
+ Contract Song => String
5
+ def self.format_url(song)
6
+ song.format_attributes_with_separator!("")
7
+ "http://www.azlyrics.com/lyrics/#{song.author}/#{song.title}.html"
6
8
  end
7
- module_function :format_url
8
9
 
9
- def extract_lyric(data)
10
- html = Nokogiri::HTML(data)
11
- lyrics_container = html.css('div:nth-child(7)').first
12
- unless lyrics_container.nil?
13
- elements = lyrics_container.children.to_a
14
- phrases = elements.select { |el| el.text? && el.text != "\n" && !el.blank? }
15
- phrases.map! { |element| element.text.strip }
16
- end
10
+ Contract Tempfile => Or[Array, nil]
11
+ def self.extract_lyric(data)
12
+ Providers.extract_lyrics_at_css_from_data('div:nth-child(7)', data)
17
13
  end
18
- module_function :extract_lyric
19
14
  end
@@ -1,23 +1,14 @@
1
- module LyricsFinder::Providers::LyricsWikia
2
- #Contract lambda { |a, s| !a.blank? && !s.blank? } => String
3
- Contract String, String => String
4
- def format_url(author, title)
5
- author = I18n.transliterate(author.strip.gsub(" ", "_"))
6
- title = I18n.transliterate(title.strip.gsub(" ", "_"))
7
- "http://lyrics.wikia.com/#{author}:#{title}"
1
+ module Providers::LyricsWikia
2
+ include Contracts
3
+
4
+ Contract Song => String
5
+ def self.format_url(song)
6
+ song.format_attributes_with_separator!("_")
7
+ "http://lyrics.wikia.com/#{song.author}:#{song.title}"
8
8
  end
9
- module_function :format_url
10
9
 
11
- #Contract lambda { |x| x.is_a? Tempfile } => Module
12
- Contract lambda { |x| x.is_a? Tempfile } => Array
13
- def extract_lyric(data)
14
- html = Nokogiri::HTML(data)
15
- lyrics_container = html.css('.lyricbox').first
16
- unless lyrics_container.nil?
17
- elements = lyrics_container.children.to_a
18
- phrases = elements.select { |el| el.text? && el.text != "\n" && !el.blank? }
19
- phrases.map! { |element| element.text.strip }
20
- end
10
+ Contract Tempfile => Or[Array, nil]
11
+ def self.extract_lyric(data)
12
+ Providers.extract_lyrics_at_css_from_data('.lyricbox', data)
21
13
  end
22
- module_function :extract_lyric
23
14
  end
@@ -1,19 +1,14 @@
1
- module LyricsFinder::Providers::SongLyrics
2
- def format_url(author, title)
3
- author = I18n.transliterate(author.strip.gsub(" ", "-"))
4
- title = I18n.transliterate(title.strip.gsub(" ", "-"))
5
- "http://www.songlyrics.com/#{author}/#{title}-lyrics/"
1
+ module Providers::SongLyrics
2
+ include Contracts
3
+
4
+ Contract Song => String
5
+ def self.format_url(song)
6
+ song.format_attributes_with_separator!("-")
7
+ "http://www.songlyrics.com/#{song.author}/#{song.title}-lyrics/"
6
8
  end
7
- module_function :format_url
8
9
 
9
- def extract_lyric(data)
10
- html = Nokogiri::HTML(data)
11
- lyrics_container = html.css('#songLyricsDiv').first
12
- unless lyrics_container.nil?
13
- elements = lyrics_container.children.to_a
14
- phrases = elements.select { |el| el.text? && el.text != "\n" && !el.blank? }
15
- phrases.map! { |element| element.text.strip }
16
- end
10
+ Contract Tempfile => Or[Array, nil]
11
+ def self.extract_lyric(data)
12
+ Providers.extract_lyrics_at_css_from_data('#songLyricsDiv', data)
17
13
  end
18
- module_function :extract_lyric
19
14
  end
@@ -1,8 +1,21 @@
1
- module LyricsFinder::Providers
1
+ require_relative 'song'
2
+
3
+ module Providers
4
+ include Contracts
5
+
2
6
  Contract Symbol => Module
3
- def build_klass(provider)
4
- klass = "LyricsFinder::Providers::" + provider.to_s.camelize
7
+ def self.build_klass(provider)
8
+ klass = "Providers::" + provider.to_s.camelize
5
9
  klass.constantize
6
10
  end
7
- module_function :build_klass
11
+
12
+ def self.extract_lyrics_at_css_from_data(css_element, data)
13
+ html = Nokogiri::HTML(data)
14
+ lyrics_container = html.css(css_element).first
15
+ unless lyrics_container.nil?
16
+ elements = lyrics_container.children.to_a
17
+ phrases = elements.select { |el| el.text? && el.text != "\n" && !el.blank? }
18
+ phrases.map! { |element| element.text.strip }
19
+ end
20
+ end
8
21
  end
@@ -0,0 +1,21 @@
1
+ class Song
2
+ attr_accessor :author, :title
3
+
4
+ def initialize(author, title)
5
+ @author = author
6
+ @title = title
7
+ end
8
+
9
+ def self.valid?(val)
10
+ val.is_a?(Song) && !val.author.blank? && !val.title.blank?
11
+ end
12
+
13
+ def self.to_s
14
+ "a valid author and song title please"
15
+ end
16
+
17
+ def format_attributes_with_separator!(separator)
18
+ self.author = I18n.transliterate(@author.strip.gsub(" ", separator))
19
+ self.title = I18n.transliterate(@title.strip.gsub(" ", separator))
20
+ end
21
+ end
@@ -1,3 +1,3 @@
1
1
  module LyricsFinder
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/lyrics_finder.rb CHANGED
@@ -1,53 +1,53 @@
1
1
  require_relative 'lyrics_finder/dependencies'
2
2
 
3
- module LyricsFinder
4
- class Fetcher
5
- PROVIDERS_LIST = [:lyrics_wikia, :song_lyrics, :azlyrics]
6
- UsageError = Class.new(StandardError)
7
- attr_reader :providers
8
-
9
- def initialize(*args)
10
- @providers = filter_providers(args)
11
- end
3
+ class Finder
4
+ include Contracts
5
+ PROVIDERS_LIST = [:lyrics_wikia, :song_lyrics, :azlyrics]
12
6
 
13
- def search(author, title)
14
- validate_song_data(author, title)
15
- song_lyric = catch(:song_lyric) {
16
- @providers.each do |provider|
17
- klass = Providers.build_klass(provider)
18
- url = klass.format_url(author, title)
19
-
20
- data = perform_request(url)
21
- result = klass.extract_lyric(data) if data
22
- throw :song_lyric, result unless result.nil?
23
- end
24
- }
25
- # because if it doesn't find anything returns @providers by default
26
- song_lyric != @providers ? song_lyric : nil
27
- end
28
-
29
- private
7
+ def initialize(*args)
8
+ @providers = filter_providers(args)
9
+ end
30
10
 
31
- def filter_providers(providers)
32
- valid_providers = []
33
- providers.each do |provider|
34
- valid_providers << provider if PROVIDERS_LIST.include?(provider)
35
- end
36
- valid_providers.any? ? valid_providers : PROVIDERS_LIST
37
- end
11
+ def selected_providers
12
+ @providers
13
+ end
14
+
15
+ def search(author, title)
16
+ song = Song.new(author, title)
17
+ perform_search(song)
18
+ end
19
+
20
+ Contract Song => Or[Array, nil]
21
+ def perform_search(song)
22
+ song_lyric = catch(:song_lyric) {
23
+ @providers.each do |provider|
24
+ klass = Providers.build_klass(provider)
25
+ url = klass.format_url(song)
38
26
 
39
- def validate_song_data(author, title)
40
- if author.blank? || title.blank?
41
- fail UsageError, "You must supply a valid author and title"
27
+ data = perform_request(url)
28
+ result = klass.extract_lyric(data) if data
29
+ throw :song_lyric, result unless result.nil?
42
30
  end
31
+ }
32
+ # because if it doesn't find anything returns @providers by default
33
+ song_lyric != @providers ? song_lyric : nil
34
+ end
35
+
36
+ private
37
+
38
+ def filter_providers(providers)
39
+ valid_providers = []
40
+ providers.each do |provider|
41
+ valid_providers << provider if PROVIDERS_LIST.include?(provider)
43
42
  end
43
+ valid_providers.any? ? valid_providers : PROVIDERS_LIST
44
+ end
44
45
 
45
- def perform_request(url)
46
- begin
47
- open(url)
48
- rescue Exception => ex
49
- # puts "ERROR: " + ex.message
50
- end
46
+ def perform_request(url)
47
+ begin
48
+ open(url)
49
+ rescue Exception => ex
50
+ # puts "ERROR: " + ex.message
51
51
  end
52
52
  end
53
53
  end
@@ -22,14 +22,14 @@ Gem::Specification.new do |spec|
22
22
  spec.add_development_dependency 'rake'
23
23
 
24
24
  spec.add_development_dependency 'rspec', '~> 2.14.0'
25
- spec.add_development_dependency 'rspec-nc', '0.0.6'
26
- spec.add_development_dependency 'vcr', '2.9.2'
27
- spec.add_development_dependency 'webmock', '1.18.0'
28
- spec.add_development_dependency 'coveralls', '0.7.0'
25
+ spec.add_development_dependency 'rspec-nc', '~> 0.0.6'
26
+ spec.add_development_dependency 'vcr', '~> 2.9.2'
27
+ spec.add_development_dependency 'webmock', '~> 1.18.0'
28
+ spec.add_development_dependency 'coveralls', '~> 0.7.0'
29
29
 
30
- spec.add_dependency 'nokogiri', '1.6.1'
31
- spec.add_dependency 'activesupport', '4.1.1'
32
- spec.add_dependency 'i18n', '0.6.9'
30
+ spec.add_dependency 'nokogiri', '~> 1.6.1'
31
+ spec.add_dependency 'activesupport', '~> 4.1.1'
32
+ spec.add_dependency 'i18n', '~> 0.6.9'
33
33
  spec.add_dependency 'contracts', '0.4'
34
- spec.add_dependency 'thor', '0.19.1'
34
+ spec.add_dependency 'thor', '~> 0.19.1'
35
35
  end
@@ -1,27 +1,27 @@
1
- describe LyricsFinder::Fetcher do
1
+ describe Finder do
2
2
  describe 'sets @providers properly on initialization' do
3
3
  context 'without specifying providers' do
4
- let(:fetcher) { LyricsFinder::Fetcher.new }
4
+ let(:finder) { Finder.new }
5
5
 
6
6
  it 'sets @providers to default PROVIDERS_LIST' do
7
- expect(fetcher.providers).to eq LyricsFinder::Fetcher::PROVIDERS_LIST
7
+ expect(finder.selected_providers).to eq Finder::PROVIDERS_LIST
8
8
  end
9
9
  end
10
10
 
11
11
  context 'specifying providers' do
12
12
  context 'some providers are invalid' do
13
- let(:fetcher) { LyricsFinder::Fetcher.new(:lyrics_wikia, :bad_songs) }
13
+ let(:finder) { Finder.new(:lyrics_wikia, :bad_songs) }
14
14
 
15
15
  it 'filters invalid providers' do
16
- expect(fetcher.providers).to match_array [:lyrics_wikia]
16
+ expect(finder.selected_providers).to match_array [:lyrics_wikia]
17
17
  end
18
18
  end
19
19
 
20
20
  context 'all providers are invalid' do
21
- let(:fetcher) { LyricsFinder::Fetcher.new(:bad_songs, :invalid_songs) }
21
+ let(:finder) { Finder.new(:bad_songs, :invalid_songs) }
22
22
 
23
23
  it 'sets @providers to default PROVIDERS_LIST' do
24
- expect(fetcher.providers).to eq LyricsFinder::Fetcher::PROVIDERS_LIST
24
+ expect(finder.selected_providers).to eq Finder::PROVIDERS_LIST
25
25
  end
26
26
  end
27
27
  end
@@ -31,9 +31,9 @@ describe LyricsFinder::Fetcher do
31
31
  describe 'using LyricsWikia as the provider' do
32
32
  context 'with a song that can be found' do
33
33
  before :each do
34
- @fetcher = LyricsFinder::Fetcher.new(:lyrics_wikia)
34
+ @finder = Finder.new(:lyrics_wikia)
35
35
  VCR.use_cassette 'LyricsWikia 200 search' do
36
- @song = @fetcher.search("american authors", "best day of my life")
36
+ @song = @finder.search("american authors", "best day of my life")
37
37
  end
38
38
  end
39
39
 
@@ -49,9 +49,9 @@ describe LyricsFinder::Fetcher do
49
49
  # Searching for a song that exist but it's not yet on this website.
50
50
  context 'with a song that cannot be found' do
51
51
  before :each do
52
- @fetcher = LyricsFinder::Fetcher.new(:lyrics_wikia)
52
+ @finder = Finder.new(:lyrics_wikia)
53
53
  VCR.use_cassette 'LyricsWikia Song does not exist search' do
54
- @song = @fetcher.search("arctic monkeys", "do i wanna know")
54
+ @song = @finder.search("arctic monkeys", "do i wanna know")
55
55
  end
56
56
  end
57
57
 
@@ -63,9 +63,9 @@ describe LyricsFinder::Fetcher do
63
63
 
64
64
  describe 'using Azlyrics as the provider' do
65
65
  before :each do
66
- @fetcher = LyricsFinder::Fetcher.new(:azlyrics)
66
+ @finder = Finder.new(:azlyrics)
67
67
  VCR.use_cassette 'Azlyrics 200 search' do
68
- @song = @fetcher.search("american authors", "best day of my life")
68
+ @song = @finder.search("american authors", "best day of my life")
69
69
  end
70
70
  end
71
71
 
@@ -80,9 +80,9 @@ describe LyricsFinder::Fetcher do
80
80
 
81
81
  describe 'using SongLyrics as the provider' do
82
82
  before :each do
83
- @fetcher = LyricsFinder::Fetcher.new(:song_lyrics)
83
+ @finder = Finder.new(:song_lyrics)
84
84
  VCR.use_cassette 'SongLyrics 200 search' do
85
- @song = @fetcher.search("american authors", "best day of my life")
85
+ @song = @finder.search("american authors", "best day of my life")
86
86
  end
87
87
  end
88
88
 
@@ -99,9 +99,9 @@ describe LyricsFinder::Fetcher do
99
99
  # it just redirect to the root url www.lyricsmania.com
100
100
  # describe 'using LyricsMania as the provider' do
101
101
  # before :each do
102
- # @fetcher = LyricsFinder::Fetcher.new(:lyrics_mania)
102
+ # @finder = Finder.new(:lyrics_mania)
103
103
  # VCR.use_cassette 'LyricsMania 200 search' do
104
- # @song = @fetcher.search("american authors", "best day of my life")
104
+ # @song = @finder.search("american authors", "best day of my life")
105
105
  # end
106
106
  # end
107
107
  #
@@ -116,9 +116,9 @@ describe LyricsFinder::Fetcher do
116
116
 
117
117
  describe 'with a song that cannot be found' do
118
118
  before :each do
119
- @fetcher = LyricsFinder::Fetcher.new
119
+ @finder = Finder.new
120
120
  VCR.use_cassette 'Nonexistent Song 404 search' do
121
- @song = @fetcher.search("the foobar band", "rubynation")
121
+ @song = @finder.search("the foobar band", "rubynation")
122
122
  end
123
123
  end
124
124
 
@@ -128,13 +128,12 @@ describe LyricsFinder::Fetcher do
128
128
  end
129
129
 
130
130
  describe 'with invalid parameters' do
131
- let(:fetcher) { LyricsFinder::Fetcher.new }
131
+ let(:finder) { Finder.new }
132
132
 
133
- it 'fails with UsageError' do
133
+ it 'fails with ContractError' do
134
134
  expect{
135
- fetcher.search("", "")
136
- }.to raise_error( LyricsFinder::Fetcher::UsageError,
137
- "You must supply a valid author and title")
135
+ finder.search("","")
136
+ }.to raise_error ContractError
138
137
  end
139
138
  end
140
139
  end # '#search'