Skip to content

Instantly share code, notes, and snippets.

@davidsnyder
Created December 13, 2011 19:08
Show Gist options
  • Save davidsnyder/1473403 to your computer and use it in GitHub Desktop.
Save davidsnyder/1473403 to your computer and use it in GitHub Desktop.
OpenMenu API Wrapper

OpenMenu API Wrapper

Check the documentation here http://openmenu.org/api/

require 'open_menu'

apikey = '1219-ab23-139b-de3-c23'
client = OpenMenu::Client.new(apikey)

menu_uuid = '12db-12b3-23bde-23bef'
client.menu(menu_uuid) => HTTParty::Response

Hash parameters passed to a function are just serialized and tossed into the request query string.

Eg. client.location(:city => "Austin", :state => "TX") will resolve to http://openmenu.com/api/v1/location?key=mykey-foo-bar-baz&output=json&city=austin&state=tx

The only dependency is the awesome HTTParty gem.

Gem::Specification.new do |s|
s.name = 'open_menu'
s.version = '0.0.1'
s.platform = Gem::Platform::RUBY
s.author = 'David Snyder'
s.email = 'david@democraticlunch.com'
s.summary = 'OpenMenu API Wrapper'
s.description = 'Wrapper for the OpenMenu API (http://openmenu.org)'
s.files = ['open_menu.rb']
s.require_path = '.'
s.add_dependency('httparty')
end
require 'httparty'
module OpenMenu
class Client
include HTTParty
base_uri 'http://openmenu.com'
attr_accessor :version
# @apikey@ uuid key, request an account on http://openmenu.com
# @format@ :json or :xml, defaults to :json
# @version@ defaults to :v1
def initialize(apikey,version=:v1,format=:json)
raise MissingApikeyError unless apikey
self.version = version
self.class.default_params :key => apikey,:output => format
end
def menu(uuid)
validate_param_types(String,uuid)
self.class.get("/menu/#{uuid}")
end
def restaurant(params)
validate_param_types(Hash,params)
self.class.get("/api/#{version}/restaurant",:query => params)
end
def location(params)
validate_param_types(Hash,params)
self.class.get("/api/#{version}/location",:query => params)
end
def crosswalk(params)
validate_param_types(Hash,params)
self.class.get("/api/#{version}/crosswalk",:query => params)
end
def updates(params)
validate_param_types(Hash,params)
self.class.get("/api/#{version}/updates",:query => params)
end
private
def validate_param_types(klass,params)
raise InvalidParametersError, "Expected #{klass.to_s}, got #{params.class} #{params}" unless params.is_a?(klass)
end
end
class MissingApikeyError < StandardError; end
class InvalidParametersError < StandardError; end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment