rapgenius 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -11,4 +11,8 @@ __v0.0.2__ (17th August 2013)
11
11
  __v0.0.3__ (22nd August 2013, *contributed by [tsigo](https://github.com/tsigo)*)
12
12
 
13
13
  * Improves implementation of HTTParty
14
- * Reorganises specs to use VCR
14
+ * Reorganises specs to use VCR
15
+
16
+ __v0.1.0__ (29th August 2013, *contributed by [tsigo](https://github.com/tsigo)*)
17
+
18
+ * Adds support for searching for songs with `RapGenius::Song.search("Song, artist name or other query")`
data/README.md CHANGED
@@ -15,7 +15,7 @@ with a nice bit of screen scraping with [Nokogiri](https://github.com/sparklemot
15
15
  Install the gem, and you're ready to go. Simply add the following to your
16
16
  Gemfile:
17
17
 
18
- `gem "rapgenius", "~> 0.0.2"`
18
+ `gem "rapgenius", "~> 0.1.0"`
19
19
 
20
20
  ## Usage
21
21
 
@@ -77,14 +77,45 @@ annotation == annotations2
77
77
  # => true
78
78
  ```
79
79
 
80
+ You can search for songs by artist and/or title.
81
+
82
+ ```ruby
83
+ results = RapGenius::Song.search("Big Sean Control")
84
+ # => [#<RapGenius::Song:0x007fbe4b9195e0
85
+ # @artist="Big Sean (Ft. Jay Electronica & Kendrick Lamar)",
86
+ # @title="Control",
87
+ # @url="http://rapgenius.com/Big-sean-control-lyrics">,
88
+ # #<RapGenius::Song:0x007fbe4b920f70
89
+ # @artist="Big Sean (Ft. Jay Electronica & Kendrick Lamar)",
90
+ # @title="Control (French Version)",
91
+ # @url="http://rapgenius.com/Big-sean-control-french-version-lyrics">,
92
+ # #<RapGenius::Song:0x007fbe4b920958
93
+ # @artist="Big Sean (Ft. Crobar, Jay Electronica & Kendrick Lamar)",
94
+ # @title="Control (Remix) [Kendrick Diss]",
95
+ # @url="http://rapgenius.com/Big-sean-control-remix-kendrick-diss-lyrics">,
96
+ # #<RapGenius::Song:0x007fbe4b920250
97
+ # @artist=
98
+ # "Sa-roc (Ft. Big Sean - No I.D., Jay Electronica, Kendrick Lamar & Sa-roc)",
99
+ # @title="CONTROL",
100
+ # @url="http://rapgenius.com/Sa-roc-control-lyrics">,
101
+ # #<RapGenius::Song:0x007fbe4b91ff30
102
+ # @artist="C3",
103
+ # @title=
104
+ # "Control ( Disses Kendrick Lamar , Jay-Z, Tyler The Creator, Big Sean, Meek Mill & More )",
105
+ # @url=
106
+ # "http://rapgenius.com/C3-control-disses-kendrick-lamar-jay-z-tyler-the-creator-big-sean-meek-mill-and-more-lyrics">]
107
+
108
+ results[0].description
109
+ # => "The non-album cut from Sean that basically blew up the Internet due to a world-beating verse by Kendrick Lamar...
110
+ ```
111
+
80
112
  ## Contributing
81
113
 
82
- There are a few things I'd love to see added to this gem:
114
+ After the last few contributions, there's one core thing I'd like to add to the gem:
83
115
 
84
- * __Searching__ - having to know the path to a particular track's lyrics isn't super intuitive
85
- * __Support for *\*Genius*__ - RapG enius also have other sites on subdomains like [News Genius](http://news.rapgenius.com) and [Poetry Genius](http://poetry.rapgenius.com). These could very easily be supported, since theyre identical in terms of markup.
116
+ * __Support for *\*Genius*__ - Rap Genius also have other sites on subdomains like [News Genius](http://news.rapgenius.com) and [Poetry Genius](http://poetry.rapgenius.com). These could very easily be supported, since theyre identical in terms of markup.
86
117
 
87
- This gem is open source, so feel free to add anything you want, then make a pull request. A few quick tips:
118
+ If you'd like to contribute anything else, go ahead or better still, make an issue and we can talk it over and spec it out! A few quick tips:
88
119
 
89
120
  * Don't update the version numbers before your pull request - I'll sort that part out for you!
90
121
  * Make sure you write specs, then run them with `$ bundle exec rake`
@@ -92,4 +123,6 @@ This gem is open source, so feel free to add anything you want, then make a pull
92
123
 
93
124
  ## Get in touch
94
125
 
95
- Any questions, thoughts or comments? Email me at <me@timrogers.co.uk>.
126
+ [timrogers](https://github.com/timrogers) and [tsigo](https://github.com/tsigo) are the gem's primary contributors.
127
+
128
+ Any questions, thoughts or comments? Email me at <me+rapgenius@timrogers.co.uk> or create an issue.
@@ -24,6 +24,31 @@ module RapGenius
24
24
  parser NokogiriParser
25
25
  base_uri 'http://rapgenius.com'
26
26
  headers 'User-Agent' => "rapgenius.rb v#{RapGenius::VERSION}"
27
+
28
+ # Perform a search
29
+ #
30
+ # query - Query string
31
+ #
32
+ # Note: Currently only supports Song searching
33
+ #
34
+ # Returns a non-parsed (i.e., plaintext) response body
35
+ def self.search(query)
36
+ response = get("/search/quick", query: {q: query}, headers: default_search_headers)
37
+ response.body
38
+ end
39
+
40
+ private
41
+
42
+ # Default headers for a search request
43
+ #
44
+ # Tells RapGenius we only want Javascript so we get plaintext results back.
45
+ def self.default_search_headers
46
+ {
47
+ 'X-Requested-With' => 'XMLHttpRequest',
48
+ 'Referer' => base_uri,
49
+ 'Accept' => 'application/x-javascript,text/javascript'
50
+ }
51
+ end
27
52
  end
28
53
 
29
54
  BASE_URL = Client.base_uri + "/".freeze
@@ -32,7 +57,7 @@ module RapGenius
32
57
 
33
58
  def url=(url)
34
59
  unless url =~ /^https?:\/\//
35
- @url = BASE_URL + url
60
+ @url = BASE_URL + url.gsub(/^\//, '')
36
61
  else
37
62
  @url = url
38
63
  end
@@ -7,16 +7,35 @@ module RapGenius
7
7
  self.new(path)
8
8
  end
9
9
 
10
- def initialize(path)
10
+ # Search for a song
11
+ #
12
+ # query - Song to search for
13
+ #
14
+ # Returns an Array of Song objects.
15
+ def self.search(query)
16
+ results = Client.search(query)
17
+
18
+ results.split("\n").map do |song|
19
+ info, link, id = song.split('|')
20
+ artist, title = info.force_encoding('UTF-8').split(' – ')
21
+
22
+ new(link, artist: artist, title: title)
23
+ end
24
+ end
25
+
26
+ def initialize(path, kwargs = {})
11
27
  self.url = path
28
+
29
+ @artist = kwargs.delete(:artist)
30
+ @title = kwargs.delete(:title)
12
31
  end
13
32
 
14
33
  def artist
15
- document.css('.song_title a').text
34
+ @artist ||= document.css('.song_title a').text
16
35
  end
17
36
 
18
37
  def title
19
- document.css('.edit_song_description i').text
38
+ @title ||= document.css('.edit_song_description i').text
20
39
  end
21
40
 
22
41
  def description
@@ -1,3 +1,3 @@
1
1
  module RapGenius
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.0"
3
3
  end
Binary file
data/rapgenius.gemspec CHANGED
@@ -6,9 +6,9 @@ Gem::Specification.new do |s|
6
6
  s.name = "rapgenius"
7
7
  s.version = RapGenius::VERSION
8
8
  s.platform = Gem::Platform::RUBY
9
- s.authors = ["Tim Rogers"]
10
- s.email = ["me@timrogers.co.uk"]
11
- s.homepage = "http://timrogers.co.uk"
9
+ s.authors = ["Tim Rogers", "Robert Speicher"]
10
+ s.email = ["me+rapgenius@timrogers.co.uk", "rspeicher@gmail.com"]
11
+ s.homepage = "https://github.com/timrogers/rapgenius"
12
12
  s.summary = %q{A gem for accessing lyrics and explanations on RapGenius.com}
13
13
  s.description = %q{Up until until now, to quote RapGenius themselves,
14
14
  "working at Rap Genius is the API". With this magical screen-scraping gem,
@@ -13,7 +13,7 @@ module RapGenius
13
13
 
14
14
  describe "#lyric" do
15
15
  it "should have the correct lyric" do
16
- annotation.lyric.should eq "You gon' get this rain like it's May weather,"
16
+ annotation.lyric.should match "You gon' get this rain like it's May weather"
17
17
  end
18
18
  end
19
19
 
@@ -42,5 +42,22 @@ module RapGenius
42
42
  i.url.should eq 'http://rapgenius.com/foobar'
43
43
  end
44
44
  end
45
+
46
+ describe '.search', vcr: {cassette_name: 'song-search-big-sean-control'} do
47
+ let(:results) { described_class.search('Big Sean Control') }
48
+
49
+ it "returns an Array of Songs" do
50
+ results.should be_an Array
51
+ results.first.should be_a Song
52
+ end
53
+
54
+ describe 'assigned attributes' do
55
+ subject { results.first }
56
+
57
+ its(:url) { should eq "http://rapgenius.com/Big-sean-control-lyrics" }
58
+ its(:artist) { should eq "Big Sean (Ft. Jay Electronica & Kendrick Lamar)" }
59
+ its(:title) { should eq "Control" }
60
+ end
61
+ end
45
62
  end
46
63
  end
metadata CHANGED
@@ -1,15 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rapgenius
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Tim Rogers
9
+ - Robert Speicher
9
10
  autorequire:
10
11
  bindir: bin
11
12
  cert_chain: []
12
- date: 2013-08-22 00:00:00.000000000 Z
13
+ date: 2013-08-29 00:00:00.000000000 Z
13
14
  dependencies:
14
15
  - !ruby/object:Gem::Dependency
15
16
  name: nokogiri
@@ -111,7 +112,8 @@ description: ! "Up until until now, to quote RapGenius themselves,\n \"workin
111
112
  at Rap Genius is the API\". With this magical screen-scraping gem,\n you can
112
113
  access the wealth of data on the internet Talmud in Ruby."
113
114
  email:
114
- - me@timrogers.co.uk
115
+ - me+rapgenius@timrogers.co.uk
116
+ - rspeicher@gmail.com
115
117
  executables: []
116
118
  extensions: []
117
119
  extra_rdoc_files: []
@@ -130,13 +132,14 @@ files:
130
132
  - lib/rapgenius/version.rb
131
133
  - pkg/rapgenius-0.0.1.gem
132
134
  - pkg/rapgenius-0.0.2.gem
135
+ - pkg/rapgenius-0.0.3.gem
133
136
  - rapgenius.gemspec
134
137
  - spec/rapgenius/annotation_spec.rb
135
138
  - spec/rapgenius/scraper_spec.rb
136
139
  - spec/rapgenius/song_spec.rb
137
140
  - spec/spec_helper.rb
138
141
  - spec/support/vcr.rb
139
- homepage: http://timrogers.co.uk
142
+ homepage: https://github.com/timrogers/rapgenius
140
143
  licenses: []
141
144
  post_install_message:
142
145
  rdoc_options: []