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
# File 'lib/punchfork/base.rb', line 6

def search(*args)
  Punchfork::Search.new(JSON.load(Net::HTTP.get_response(search_url(*args)).body))
end

#search_url(*args) ⇒ Object

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



11
12
13
14
15
16
17
18
19
# File 'lib/punchfork/base.rb', line 11

def search_url(*args)
  options = args.extract_options!.stringify_keys!
  protocol = (options['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('&')
  return uri
end