Module: Punchfork::Base

Included in:
Punchfork
Defined in:
lib/punchfork/base.rb

Instance Method Summary collapse

Instance Method Details

#search(*args) ⇒ Object

Returns a Punchfork::Search object. Accepts any number of arguments that will be used as ingredients in your search as well as an options hash. Example: Punchfork.search(:tomatoes, :garlic, protocol:‘https’)



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/punchfork/base.rb', line 6

def search(*args)
  url = search_url(*args)
  results = JSON.load(Net::HTTP.get_response(url).body)
  p ['results for: ', url.to_s, results] if self.debug
  Punchfork::Search.new(results)
rescue Exception => e
  p url
  p results
  p e
  raise e
end

#search_url(*args) ⇒ Object

used by #search to query punchfork.com. Returns a URI object.



19
20
21
22
23
24
25
26
27
28
# File 'lib/punchfork/base.rb', line 19

def search_url(*args)
  options = args.extract_options!.stringify_keys!
  protocol = (options.delete('protocol') || self.protocol).to_s
  
  uri = search_uri
  uri.scheme = protocol
  uri.query = ["key=#{api_key}", "q=#{URI.escape(args.join(','))}", *options.map{ |k,v| "#{k}=#{v}" }].join('&')
  p uri.to_s if self.debug
  return uri
end