Class: ItunesSearch::Search
- Inherits:
-
Object
- Object
- ItunesSearch::Search
- Defined in:
- lib/itunes-search/search.rb
Instance Attribute Summary collapse
-
#json ⇒ Object
Returns the value of attribute json.
-
#options ⇒ Object
Returns the value of attribute options.
-
#result_hash ⇒ Object
Returns the value of attribute result_hash.
Instance Method Summary collapse
- #fetch ⇒ Object
-
#initialize(*args) ⇒ Search
constructor
A new instance of Search.
- #method_missing(method_name, *args) ⇒ Object
- #original_method_missing ⇒ Object
- #results ⇒ Object
- #to_hash ⇒ Object
Constructor Details
#initialize(*args) ⇒ Search
Returns a new instance of Search.
7 8 9 10 11 12 |
# File 'lib/itunes-search/search.rb', line 7 def initialize(*args) self.={} args.each do |arg| self..merge!(arg) end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/itunes-search/search.rb', line 13 def method_missing(method_name,*args) if args.size == 1 self..merge!({"#{method_name.to_s.gsub("=","")}"=>args.first.to_s}) return self.["#{method_name.to_s.gsub("=","")}"] elsif args.size == 0 if self..keys.include?(method_name.to_s) return self.["#{method_name.to_s.gsub("=","")}"] end end original_method_missing method_name, args end |
Instance Attribute Details
#json ⇒ Object
Returns the value of attribute json.
4 5 6 |
# File 'lib/itunes-search/search.rb', line 4 def json @json end |
#options ⇒ Object
Returns the value of attribute options.
4 5 6 |
# File 'lib/itunes-search/search.rb', line 4 def @options end |
#result_hash ⇒ Object
Returns the value of attribute result_hash.
4 5 6 |
# File 'lib/itunes-search/search.rb', line 4 def result_hash @result_hash end |
Instance Method Details
#fetch ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/itunes-search/search.rb', line 24 def fetch #puts "#{ItunesSearch::ENDPOINT}?#{self.options.to_url_params}" uri = URI.parse("#{ItunesSearch::ENDPOINT}?#{self..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..to_url_params}") end self.json=resp.body end |
#original_method_missing ⇒ Object
5 |
# File 'lib/itunes-search/search.rb', line 5 alias :original_method_missing :method_missing |
#results ⇒ Object
34 35 36 37 38 39 |
# File 'lib/itunes-search/search.rb', line 34 def results ra = [] ra = self.to_hash["results"].collect {|r| ItunesSearch::Result.new(r)} unless self.to_hash["results"].empty? puts ra.inspect return ra end |
#to_hash ⇒ Object
41 42 43 |
# File 'lib/itunes-search/search.rb', line 41 def to_hash self.result_hash ||= JSON.parse(fetch) end |