vagalume 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d06acfd5b52be5d23a930c642b0829efe4802ee2
4
- data.tar.gz: e86a3b7e30b579405225cfc5f0a7c8be189abf52
3
+ metadata.gz: cdb570dd97273427b15f1f1038d14673f7e8220b
4
+ data.tar.gz: 4dd491d32fabed72de2253289668845f398babf0
5
5
  SHA512:
6
- metadata.gz: 7ed49516aba72eab580b89eae5cb8ce508dfaee771088f063160e923dd8393c1ebb4f2797058588b8e3cd672dafc2b894fbe9bec242e4631b7abab4dc5d0595e
7
- data.tar.gz: 89807c874b91ac1d6cdb4d8d69cfd32903ed2a8716647b6d730fba14c5349563c3d5aa460f04eb0960b40841221dfcbc86aa625213147ec587c29ee4763d4e2c
6
+ metadata.gz: 20fd8b6211abf6acb9ae361a00bcda7c990bc18cc1816c45e31cf4c64a78fb88516d022baa4f7b6c1ddfcc94306482e409d75894836b54adb8df1f77f3f370c5
7
+ data.tar.gz: 3ea8b96d1a530ff86efde1f8d4f2657da8306bc1b3912f6f10a2a15ca02f3495b6cd107208b9c93ac7218297a88eea0126d57e593e2eeb9c620731a6b06f40a5
@@ -1,8 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- vagalume (0.1.4)
5
- multi_json (~> 1.9.2)
4
+ vagalume (0.1.6)
5
+ multi_json (~> 1.9)
6
6
 
7
7
  GEM
8
8
  remote: http://rubygems.org/
@@ -11,7 +11,7 @@ GEM
11
11
  crack (0.4.2)
12
12
  safe_yaml (~> 1.0.0)
13
13
  diff-lcs (1.2.5)
14
- multi_json (1.9.3)
14
+ multi_json (1.10.1)
15
15
  rspec (2.14.1)
16
16
  rspec-core (~> 2.14.0)
17
17
  rspec-expectations (~> 2.14.0)
@@ -31,7 +31,7 @@ PLATFORMS
31
31
 
32
32
  DEPENDENCIES
33
33
  bundler (~> 1.3)
34
- rspec (~> 2.14.1)
34
+ rspec (~> 2.14)
35
35
  vagalume!
36
- vcr (~> 2.9.0)
37
- webmock (~> 1.17.4)
36
+ vcr (~> 2.9)
37
+ webmock (~> 1.17)
@@ -16,12 +16,13 @@ module Vagalume
16
16
 
17
17
  def find(artist, song)
18
18
  request_url = BASE_URL + "art=#{CGI.escape(artist)}&mus=#{CGI.escape(song)}"
19
- result_json = MultiJson.decode(open(request_url).read)
20
- search_result = Vagalume::SearchResult.fetch(result_json)
19
+ result = MultiJson.decode(open(request_url).read)
20
+ search_result = Vagalume::SearchResult.new(result)
21
21
  end
22
22
 
23
23
  def get_lyric(artist, song, options)
24
24
  search = find(artist, song)
25
- Vagalume::LyricFormatter.format(search, options)
25
+ formatter = Vagalume::LyricFormatter.new
26
+ formatter.format(search, options)
26
27
  end
27
28
  end
@@ -2,12 +2,10 @@ module Vagalume
2
2
  class Artist
3
3
  attr_accessor :id, :name, :url
4
4
 
5
- def self.fetch(artist_json)
6
- artist = Vagalume::Artist.new
7
- artist.id = artist_json["id"]
8
- artist.name = artist_json["name"]
9
- artist.url = artist_json["url"]
10
- artist
5
+ def initialize(artist)
6
+ @id = artist["id"]
7
+ @name = artist["name"]
8
+ @url = artist["url"]
11
9
  end
12
10
  end
13
11
  end
@@ -1,40 +1,43 @@
1
- class Vagalume::LyricFormatter
2
- class << self
1
+ module Vagalume
2
+ class LyricFormatter
3
3
  def format(search, options)
4
- status = search.status
5
- song = search.song
6
- translated_song = search.translations.with_language(Vagalume::Language::PORTUGUESE)
7
- output = "\n\n"
4
+ return "No lyric found" if search.not_found?
8
5
 
9
- return "No lyric found" if status == Vagalume::Status::NOT_FOUND || status == Vagalume::Status::SONG_NOT_FOUND
10
- return "\n\n#{song.name}\n\n#{song.lyric}" unless options[:translation]
11
- return "No translation found" if translated_song.nil?
6
+ original_song = search.song
12
7
 
13
- bigger_line = bigger_line(song.lyric)
14
- lyric_array = [song.name, ""]
15
- translation_array = [translated_song.name, ""]
16
-
17
- lyric_array += song.lyric.split("\n")
18
- translation_array += translated_song.lyric.split("\n")
19
-
20
- lyric_array.each_with_index do |lyric_line, index|
21
- output += lyric_line + separator(bigger_line, lyric_line) + translation_array[index].to_s + "\n"
8
+ if options[:translation]
9
+ translated_song = search.translations.with_language(Vagalume::Language::PORTUGUESE)
10
+ return "No translation found" if translated_song.nil?
11
+ return formatted_song_with_transaction(original_song, translated_song)
22
12
  end
23
- output
13
+
14
+ "\n\n#{original_song.name}\n\n#{original_song.lyric}"
24
15
  end
25
16
 
26
17
  private
27
18
 
28
- def separator(bigger_line, lyric_line)
29
- " " * (bigger_line - lyric_line.size) + " | "
30
- end
19
+ def formatted_song_with_transaction(song, translated_song)
20
+ original_lyrics_lines = [song.name, ""] + song.lyric.split("\n")
21
+ translated_lyrics_lines = [translated_song.name, ""] + translated_song.lyric.split("\n")
22
+
23
+ biggest_line_size = biggest_line_size(song.lyric)
24
+ formatted_output = "\n\n"
31
25
 
32
- def bigger_line(lyric)
33
- bigger = 0
34
- lyric.each_line do |line|
35
- bigger = line.size if line.size > bigger
26
+ original_lyrics_lines.each_with_index do |original_line, index|
27
+ separator = find_separator(biggest_line_size, original_line)
28
+ translated_line = translated_lyrics_lines[index]
29
+ formatted_output += original_line + separator + translated_line.to_s + "\n"
36
30
  end
37
- bigger
31
+
32
+ formatted_output
33
+ end
34
+
35
+ def find_separator(biggest_line_size, lyric_line)
36
+ " " * (biggest_line_size - lyric_line.size) + " | "
37
+ end
38
+
39
+ def biggest_line_size(lyric)
40
+ lyric.each_line.max { |a, b| a.length <=> b.length }.size
38
41
  end
39
42
  end
40
43
  end
@@ -2,24 +2,25 @@ module Vagalume
2
2
  class SearchResult
3
3
  attr_accessor :status, :artist, :song, :translations
4
4
 
5
- def initialize
5
+ def initialize(result)
6
6
  @translations = []
7
- end
7
+ @status = result["type"]
8
+ return if not_found?
8
9
 
9
- def self.fetch(result_json)
10
- result = Vagalume::SearchResult.new
11
- result.status = result_json["type"]
12
- return result if result.status == Vagalume::Status::NOT_FOUND || result.status == Vagalume::Status::SONG_NOT_FOUND
13
- song = result_json["mus"].first
14
- artist = result_json["art"]
10
+ song = result["mus"].first
11
+ artist = result["art"]
15
12
  translations = song["translate"] || []
16
- result.song = Vagalume::Song.fetch(song)
17
- result.artist = Vagalume::Artist.fetch(artist)
13
+ @song = Vagalume::Song.new(song)
14
+ @artist = Vagalume::Artist.new(artist)
18
15
 
19
16
  translations.each do |translation|
20
- result.translations << Vagalume::Song.fetch(translation)
17
+ @translations << Vagalume::Song.new(translation)
21
18
  end
22
- result
19
+ end
20
+
21
+ def not_found?
22
+ @status == Vagalume::Status::NOT_FOUND ||
23
+ @status == Vagalume::Status::SONG_NOT_FOUND
23
24
  end
24
25
  end
25
26
  end
@@ -2,18 +2,17 @@ module Vagalume
2
2
  class Song
3
3
  attr_accessor :id, :name, :language, :url, :lyric
4
4
 
5
- def self.fetch(song_json)
6
- song = Vagalume::Song.new
7
- song.id = song_json["id"]
8
- song.language = song_json["lang"]
9
- song.url = song_json["url"]
10
- song.lyric = song_json["text"]
11
- song.name = song_json["name"]
12
- if song.translation?
13
- song.name = get_name_from_lyric(song.lyric)
14
- song.lyric = remove_title(song.lyric)
5
+ def initialize(song)
6
+ @id = song["id"]
7
+ @language = song["lang"]
8
+ @url = song["url"]
9
+ @lyric = song["text"]
10
+ @name = song["name"]
11
+
12
+ if translation?
13
+ @name = get_name_from_lyric(@lyric)
14
+ @lyric = remove_title(@lyric)
15
15
  end
16
- song
17
16
  end
18
17
 
19
18
  def translation?
@@ -22,20 +21,28 @@ module Vagalume
22
21
 
23
22
  private
24
23
 
25
- def self.get_name_from_lyric(lyric)
24
+ def get_name_from_lyric(lyric)
26
25
  title = lyric.lines.first
27
26
  title.gsub(/\[|\]|\n/, "")
28
27
  end
29
28
 
30
- # This is necessary because on translations, the song title comes with the
31
- # lyric text, so we need to remove the first two lines, in order to
32
- # have a consistent API with the song object
33
- def self.remove_title(lyric)
34
- lyric_lines_array = lyric.lines.to_a
35
- lyric_lines_array.shift(2)
36
- lyric = ""
37
- lyric_lines_array.each { |line| lyric << line }
38
- lyric
29
+ # This is necessary because, for translations, the song title comes with the
30
+ # lyric text. Also, the format is not always the same, sometimes it's just one line
31
+ # for the title, sometimes it's the title followed by some empty lines.
32
+ # Here we remove the first line and any following empty lines, so we have
33
+ # a consident API.
34
+ def remove_title(lyric)
35
+ lines = lyric.lines
36
+ lines.shift
37
+ lines = remove_empty_lines_from_beginning(lines)
38
+ lines.join
39
+ end
40
+
41
+ def remove_empty_lines_from_beginning(lines)
42
+ return lines unless lines.first.empty? || lines.first == "\n"
43
+
44
+ lines.shift
45
+ remove_empty_lines_from_beginning(lines)
39
46
  end
40
47
  end
41
48
  end
@@ -3,7 +3,7 @@
3
3
  require "spec_helper"
4
4
 
5
5
  describe Array do
6
- it "return song with selected language" do
6
+ it "returns song with selected language" do
7
7
  right_song = double "Song"
8
8
  right_song.stub(:language).and_return(Vagalume::Language::PORTUGUESE)
9
9
 
@@ -4,52 +4,48 @@ require "spec_helper"
4
4
 
5
5
  describe Vagalume do
6
6
 
7
- before(:each) do
7
+ before do
8
8
  VCR.use_cassette('vagalume') do
9
9
  @result = Vagalume.find("Metallica", "The Unforgiven")
10
10
  end
11
11
  end
12
12
 
13
- it "should bring song" do
14
- song = @result.song
15
- artist = @result.artist
16
- lyric = File.read 'spec/assets/unforgiven_lyric.txt'
17
-
13
+ it "returns parsed result" do
18
14
  @result.status.should == Vagalume::Status::EXACT
19
15
 
20
- song.id.should == "3ade68b6g470deda3"
21
- song.name.should == "The Unforgiven"
22
- song.language.should == Vagalume::Language::ENGLISH
23
- song.url.should == "http://www.vagalume.com.br/metallica/the-unforgiven.html"
24
- song.lyric.should == lyric.chop!
16
+ @result.song.id.should == "3ade68b6g470deda3"
17
+ @result.song.name.should == "The Unforgiven"
18
+ @result.song.language.should == Vagalume::Language::ENGLISH
19
+ @result.song.url.should == "http://www.vagalume.com.br/metallica/the-unforgiven.html"
20
+ @result.song.lyric.should == File.read('spec/assets/unforgiven_lyric.txt').chop
25
21
 
26
- artist.id.should == "3ade68b5g7257eda3"
27
- artist.name.should == "Metallica"
28
- artist.url.should == "http://www.vagalume.com.br/metallica/"
22
+ @result.artist.id.should == "3ade68b5g7257eda3"
23
+ @result.artist.name.should == "Metallica"
24
+ @result.artist.url.should == "http://www.vagalume.com.br/metallica/"
29
25
  end
30
26
 
31
- it "should return result with song not found status" do
27
+ it "returns result with song not found status" do
32
28
  VCR.use_cassette('vagalume_song_not_found') do
33
29
  result = Vagalume.find("Metallica", "Oops")
34
30
  result.status.should == Vagalume::Status::SONG_NOT_FOUND
35
31
  end
36
32
  end
37
33
 
38
- it "should return result with not found status" do
34
+ it "returns result with not found status" do
39
35
  VCR.use_cassette('vagalume_not_found') do
40
36
  result = Vagalume.find("Oops", "Oops")
41
37
  result.status.should == Vagalume::Status::NOT_FOUND
42
38
  end
43
39
  end
44
40
 
45
- it "should have translations" do
41
+ it "has translations" do
46
42
  @result.translations.should_not be_empty
47
43
  pt_translation = @result.translations.with_language(Vagalume::Language::PORTUGUESE)
48
44
  lyric = File.read 'spec/assets/unforgiven_lyric_translation_pt.txt'
49
45
  pt_translation.lyric.should == lyric.chop!
50
46
  end
51
47
 
52
- it "should get right song name for translations" do
48
+ it "gets the right song name for translations" do
53
49
  translated_song = @result.translations.with_language(Vagalume::Language::PORTUGUESE)
54
50
  translated_song.name.should == "Os Imperdoáveis"
55
51
  end
@@ -4,33 +4,33 @@ require "spec_helper"
4
4
 
5
5
  describe Vagalume::LyricFormatter do
6
6
  context "there is a translation available" do
7
- before(:each) do
7
+ before do
8
8
  VCR.use_cassette('vagalume') do
9
9
  @search = Vagalume.find("Metallica", "The Unforgiven")
10
10
  end
11
11
  end
12
12
 
13
- it "should format song without translation option" do
13
+ it "formats song without translation option" do
14
14
  options = {:translation => false}
15
- formatted_lyric = Vagalume::LyricFormatter.format(@search, options)
15
+ formatted_lyric = subject.format(@search, options)
16
16
  lyric = File.read 'spec/assets/lyric_formatter/unforgiven_lyric.txt'
17
17
  formatted_lyric.should == lyric
18
18
  end
19
19
 
20
- it "should format song with translation" do
20
+ it "formats song with translation" do
21
21
  options = {:translation => true}
22
- formatted_lyric = Vagalume::LyricFormatter.format(@search, options)
22
+ formatted_lyric = subject.format(@search, options)
23
23
  lyric = File.read 'spec/assets/lyric_formatter/unforgiven_lyric_with_translation.txt'
24
24
  formatted_lyric.should == lyric
25
25
  end
26
26
  end
27
27
 
28
28
  context "there is no translation available" do
29
- it "should inform user there is no translation available" do
29
+ it "informs that there is no translation available" do
30
30
  VCR.use_cassette('vagalume_with_no_translation_available') do
31
31
  @search = Vagalume.find("Bruce Springsteen", "Ain't good enough for You")
32
32
  options = {:translation => true}
33
- output = Vagalume::LyricFormatter.format(@search, options)
33
+ output = subject.format(@search, options)
34
34
  output.should == "No translation found"
35
35
  end
36
36
  end
@@ -0,0 +1,17 @@
1
+ #encoding: utf-8
2
+
3
+ require "spec_helper"
4
+
5
+ describe Vagalume::SearchResult do
6
+ it 'knows the song was not found when the status is NOT_FOUND' do
7
+ result = { "type" => Vagalume::Status::NOT_FOUND }
8
+ search_result = Vagalume::SearchResult.new(result)
9
+ search_result.not_found?.should be_true
10
+ end
11
+
12
+ it 'knows the song was not found when the status is SONG_NOT_FOUND' do
13
+ result = { "type" => Vagalume::Status::SONG_NOT_FOUND }
14
+ search_result = Vagalume::SearchResult.new(result)
15
+ search_result.not_found?.should be_true
16
+ end
17
+ end
@@ -0,0 +1,24 @@
1
+ #encoding: utf-8
2
+
3
+ require "spec_helper"
4
+
5
+ describe Vagalume::Song do
6
+ let(:translation_lyric) do
7
+ "title
8
+
9
+
10
+ Lyric"
11
+ end
12
+
13
+ it "removes the title from the lyrics for translations" do
14
+ params = { "name" => nil, "text" => translation_lyric }
15
+ song = Vagalume::Song.new(params)
16
+ song.lyric.strip.should == "Lyric"
17
+ end
18
+
19
+ it "doesn't change the lyrics when it's not a translation" do
20
+ params = { "name" => "Foo", "text" => translation_lyric }
21
+ song = Vagalume::Song.new(params)
22
+ song.lyric.should == translation_lyric
23
+ end
24
+ end
@@ -5,7 +5,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "vagalume"
7
7
  spec.executables = ["vagalume"]
8
- spec.version = "0.1.5"
8
+ spec.version = "0.1.6"
9
9
  spec.authors = ["Brian Thomas Storti"]
10
10
  spec.email = ["btstorti@gmail.com"]
11
11
  spec.description = "Ruby interface for the Vagalume API"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vagalume
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Thomas Storti
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-28 00:00:00.000000000 Z
11
+ date: 2015-03-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_json
@@ -115,6 +115,8 @@ files:
115
115
  - spec/fixtures/vcr_cassettes/vagalume_song_not_found.yml
116
116
  - spec/fixtures/vcr_cassettes/vagalume_with_no_translation_available.yml
117
117
  - spec/lyric_formatter_spec.rb
118
+ - spec/search_result_spec.rb
119
+ - spec/song_spec.rb
118
120
  - spec/spec_helper.rb
119
121
  - vagalume.gemspec
120
122
  - vagalume.jpg