Permalink
Browse files

add hacky lookup support

  • Loading branch information...
kylebragger committed May 4, 2012
1 parent 5846c99 commit 0d11dd5d8829c89a1029bff444e19bb0ede15cd8
Showing with 51 additions and 0 deletions.
  1. +1 −0 lib/itunes-search.rb
  2. +5 −0 lib/itunes-search/base.rb
  3. +45 −0 lib/itunes-search/lookup.rb
View
@@ -19,4 +19,5 @@ def to_url_params
directory = File.expand_path(File.dirname(__FILE__))
require File.join(directory,"itunes-search", "base")
require File.join(directory,"itunes-search","search")
+require File.join(directory,"itunes-search","lookup")
require File.join(directory,"itunes-search","result")
@@ -1,11 +1,16 @@
module ItunesSearch
ENDPOINT = "http://ax.phobos.apple.com.edgesuite.net/WebObjects/MZStoreServices.woa/wa/wsSearch"
+ LOOKUP_ENDPOINT = "http://itunes.apple.com/lookup"
class Base
def search(*args)
return ItunesSearch::Search.new(*args)
end
+
+ def lookup(*args)
+ return ItunesSearch::Lookup.new(*args)
+ end
end
end
@@ -0,0 +1,45 @@
+module ItunesSearch
+
+ class Lookup
+ 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}"
+ uri = URI.parse("#{ItunesSearch::LOOKUP_ENDPOINT}?#{self.options.to_url_params}")
+ resp = Net::HTTP.start(uri.host,uri.port) do |http|
+ http.open_timeout=5
+ http.read_timeout=5
+ http.get("#{uri.path}?#{self.options.to_url_params}")
+ end
+ self.json=resp.body
+ end
+ def results
+ ra = []
+ ra = self.to_hash["results"].collect {|r| ItunesSearch::Result.new(r)} unless self.to_hash["results"].empty?
+ return ra
+ end
+
+ def to_hash
+ self.result_hash ||= JSON.parse(fetch)
+ end
+
+ end
+end

0 comments on commit 0d11dd5

Please sign in to comment.