Skip to content

Instantly share code, notes, and snippets.

@radavis
Created November 26, 2014 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save radavis/bf45d5b0e43cd3e5e670 to your computer and use it in GitHub Desktop.
Save radavis/bf45d5b0e43cd3e5e670 to your computer and use it in GitHub Desktop.
require 'httparty'
require 'cgi'
require 'dotenv'
require_relative 'brewery'
Dotenv.load
# example call:
# breweries = BeerMappingProject.new.city_search("Boston")
# breweries is an array of Brewery objects
class BeerMappingProject
include HTTParty
base_uri 'http://beermapping.com/webservice'
def initialize(key=ENV['BEER_MAPPING_API_KEY'])
@key = key
if @key.nil?
raise 'No API key for the BeerMappingProject!'
end
end
# search breweries by name
def name_search(query)
get('locquery', query)
end
# search breweries by city
def city_search(query)
get('loccity', query)
end
# search breweries by state
def state_search(query)
get('locstate', query)
end
# builds the endpoint url
# calls the HTTParty.get method
# returns an array of Brewery objects
def get(service, query)
escaped_query = CGI::escape(query)
api_response = self.class.get("/#{service}/#{@key}/#{escaped_query}")
api_results = api_response["bmp_locations"]["location"]
brewify(api_results)
end
# builds a Brewery based on records return from api call
def brewify(results)
breweries = []
results.each do |attributes|
breweries << Brewery.new(attributes)
end
breweries
end
end
# map hash to object
class Brewery
def initialize(attributes)
@beer_mapping_project_id = attributes["id"]
@name = attributes["name"]
@type = attributes["status"]
@address = attributes["street"]
@city = attributes["city"]
@state = attributes["state"]
@zip = attributes["zip"]
@country = attributes["country"]
@phone_number = attributes["phone"]
@review_url = attributes["reviewlink"]
@map_link = attributes["proxylink"]
end
end
@radavis
Copy link
Author

radavis commented Feb 3, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment