Class: ItunesSearch::Search

Inherits:
Object
  • Object
show all
Defined in:
lib/itunes-search/search.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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.options={}
  args.each do |arg|
    self.options.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.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

Instance Attribute Details

#jsonObject

Returns the value of attribute json.



4
5
6
# File 'lib/itunes-search/search.rb', line 4

def json
  @json
end

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/itunes-search/search.rb', line 4

def options
  @options
end

#result_hashObject

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

#fetchObject



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.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

#original_method_missingObject



5
# File 'lib/itunes-search/search.rb', line 5

alias :original_method_missing :method_missing

#resultsObject



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_hashObject



41
42
43
# File 'lib/itunes-search/search.rb', line 41

def to_hash
  self.result_hash ||= JSON.parse(fetch)
end