Skip to content
This repository has been archived by the owner on Jan 30, 2019. It is now read-only.

Commit

Permalink
TinySong.first
Browse files Browse the repository at this point in the history
  • Loading branch information
Oshuma committed Apr 27, 2010
1 parent 716a542 commit f47bf2c
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 3 deletions.
36 changes: 33 additions & 3 deletions lib/grooveshark/tiny_song.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
require 'cgi'
require 'net/http'
require 'uri'

module Grooveshark
class TinySong
VERSION = '0.0.1'
end
end

VERSION = '0.0.1'
BASE_URL = 'http://tinysong.com/'

class << self

# Returns a URL string with the first match of +search+.
#
# TinySong.first('Bad Brains') # => "http://tinysong.com/2Z5Q"
def first(search, format = 'json')
search = CGI.escape(search)
request = URI.join(BASE_URL, '/a/', search, "?format=#{format}")
sanitize_song_url(Net::HTTP.get(request))
end

private

# Remove any un-needed characters from +url+ (backslashes, quotes, etc.).
# This is needed until the TinySong API is fixed to return a plain string.
def sanitize_song_url(url)
url.gsub!(/["]/, '')
url.gsub!(/\\/, '')
url
end

end # self

end # TinySong
end # Grooveshark
18 changes: 18 additions & 0 deletions spec/grooveshark/tiny_song_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,22 @@
TinySong::VERSION.should_not be_nil
end

it 'should have a BASE_URL' do
TinySong::BASE_URL.should == 'http://tinysong.com/'
end

describe 'first | /a/ -' do
before(:each) do
@search_string = 'Bad Brains'
@search_url = URI.parse('http://tinysong.com/a/Bad+Brains?format=json')
@response = 'http://tinysong.com/gRqG'
end

it 'returns a single URL string' do
Net::HTTP.should_receive(:get).with(@search_url).and_return(@response)
@song_url = TinySong.first(@search_string)
@song_url.should == @response
end
end

end
21 changes: 21 additions & 0 deletions watchr_suite.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
def run_specs
system('rake spec')
end

# Ctrl-\
Signal.trap('QUIT') do
puts "\n--- Running all specs ---\n"
run_specs
end

# Ctrl-C
Signal.trap('INT') { abort("\n") }

run_specs

[
'lib/(.*/)?.*\.rb',
'spec/(.*/)?.*_spec\.rb'
].each do |pattern|
watch(pattern) { |md| run_specs }
end

0 comments on commit f47bf2c

Please sign in to comment.