forked from johnnyiller/itunes-search
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
52f48f7
commit 491d523
Showing
7 changed files
with
149 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
require "cgi" | ||
require 'rubygems' | ||
|
||
class Hash | ||
def to_url_params | ||
elements = [] | ||
keys.size.times do |i| | ||
elements << "#{CGI::escape(keys[i])}=#{CGI::escape(values[i])}" | ||
end | ||
elements.join('&') | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module ItunesSearch | ||
|
||
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) | ||
$LOAD_PATH.unshift(File.dirname(__FILE__)) | ||
|
||
# require '*' | ||
require 'hash_extension' | ||
require 'net/http' | ||
require 'uri' | ||
require 'json' | ||
require 'search' | ||
require 'result' | ||
|
||
ENDPOINT = "http://ax.phobos.apple.com.edgesuite.net/WebObjects/MZStoreServices.woa/wa/wsSearch" | ||
|
||
class Base | ||
|
||
|
||
def search(*args) | ||
return Search.new(*args) | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module ItunesSearch | ||
|
||
class Result | ||
attr_accessor :attributes | ||
alias :original_method_missing :method_missing | ||
def initialize(hash) | ||
self.attributes = {} | ||
self.attributes.merge!(hash) | ||
end | ||
|
||
def method_missing(method_name,*args) | ||
if self.attributes.keys.include?(method_name.to_s) | ||
return self.attributes["#{method_name.to_s}"] | ||
else | ||
original_method_missing method_name, args | ||
end | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
module ItunesSearch | ||
|
||
class Search | ||
attr_accessor :options, :result_hash, :json | ||
alias :original_method_missing :method_missing | ||
|
||
def initialize(*args) | ||
self.options={} | ||
args.each do |arg| | ||
self.options.merge!(arg) | ||
end | ||
end | ||
def method_missing(method_name,*args) | ||
if args.size == 1 | ||
self.options.merge!({"#{method_name.to_s.gsub("=","")}"=>args.first.to_s}) | ||
return self.options["#{method_name.to_s.gsub("=","")}"] | ||
elsif args.size == 0 | ||
if self.options.keys.include?(method_name.to_s) | ||
return self.options["#{method_name.to_s.gsub("=","")}"] | ||
end | ||
end | ||
original_method_missing method_name, args | ||
end | ||
def fetch | ||
puts "#{ItunesSearch::ENDPOINT}?#{self.options.to_url_params}" | ||
self.json = Net::HTTP.get(URI.parse("#{ItunesSearch::ENDPOINT}?#{self.options.to_url_params}")).to_s | ||
end | ||
def results | ||
ra = [] | ||
ra = self.to_hash["results"].collect {|r| Result.new(r)} unless self.to_hash["results"].empty? | ||
return ra | ||
end | ||
|
||
def to_hash | ||
self.result_hash ||= JSON.parse(fetch) | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,35 @@ | ||
require 'helper' | ||
require 'itunes-search' | ||
|
||
class TestItunesSearch < Test::Unit::TestCase | ||
should "probably rename this file and start testing for real" do | ||
flunk "hey buddy, you should probably rename this file and start testing for real" | ||
|
||
include ItunesSearch | ||
|
||
context "Search working" do | ||
setup do | ||
@base = Base.new() | ||
end | ||
context "Band exists on itunes" do | ||
setup do | ||
@so = @base.search("term"=>"The Killers") | ||
end | ||
should "have result set" do | ||
assert @so.results.size>0 | ||
end | ||
should "be able to get first url" do | ||
assert !@so.results.first.trackViewUrl!="" | ||
end | ||
should "not be empty" do | ||
assert !@so.results.empty? | ||
end | ||
end | ||
context "Band does not exist on itunes" do | ||
setup do | ||
@so = @base.search("term"=>"dsafjlkdsajflkjdsa") | ||
end | ||
should "have empty result set" do | ||
assert @so.results.empty? | ||
end | ||
end | ||
end | ||
end |