Skip to content

Commit

Permalink
major changes
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyiller committed Apr 16, 2010
1 parent 52f48f7 commit 491d523
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 5 deletions.
23 changes: 22 additions & 1 deletion README.rdoc
@@ -1,6 +1,27 @@
= itunes-search

Description goes here.
This was created for use on musicxray.com if you think you can do better send your resume to jeff at musicxray.com

== Installation

gem install itunes-search

== Usage

base = Itunes::Base.new

search_object = base.search("term"=>"The Killers")

# get an array of the search_objects

results = search_object.results

results.each do |result|
puts result.trackViewUrl
end

puts result.attributes


== Note on Patches/Pull Requests

Expand Down
4 changes: 2 additions & 2 deletions Rakefile
Expand Up @@ -6,11 +6,11 @@ begin
Jeweler::Tasks.new do |gem|
gem.name = "itunes-search"
gem.summary = %Q{Itunes Search}
gem.description = %Q{Pretty simple interface for the itunes search api}
gem.description = %Q{Pretty simple interface for the itunes search api will return results as array of results objects and offer reasonable accessor methods variables}
gem.email = "jeff.durand@gmail.com"
gem.homepage = "http://github.com/johnnyiller/itunes-search"
gem.authors = ["jeff durand"]
gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
gem.add_development_dependency "shoulda", ">= 0"
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
end
Jeweler::GemcutterTasks.new
Expand Down
12 changes: 12 additions & 0 deletions lib/hash_extension.rb
@@ -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
24 changes: 24 additions & 0 deletions lib/itunes-search.rb
@@ -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
20 changes: 20 additions & 0 deletions lib/result.rb
@@ -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
39 changes: 39 additions & 0 deletions lib/search.rb
@@ -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
32 changes: 30 additions & 2 deletions test/test_itunes-search.rb
@@ -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

0 comments on commit 491d523

Please sign in to comment.