Sha256: 2fafd1c5307b97c6976266cc6d80f267bfc09a0514b98f3f49809efb318f2584

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

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}"
      uri = URI.parse("#{ItunesSearch::ENDPOINT}?#{self.options.to_url_params}")
      http = Net::HTTP.new(uri.host,uri.port)
      http.open_timeout=5
      http.read_timeout=5
      req = Net::HTTP::Get.new(uri.path)
      resp = http.start do |http|
        http.request(req)
      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?
      puts ra.inspect
      return ra
    end
    
    def to_hash
      self.result_hash ||= JSON.parse(fetch)
    end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
itunes-search-0.2.3 lib/itunes-search/search.rb