Created
July 8, 2014 23:18
-
-
Save radavis/9674943e41c959b429c7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative 'helper' | |
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'httparty' | |
require 'cgi' | |
require 'dotenv' | |
require_relative 'brewery' | |
Dotenv.load |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment