module Brewry

Constants

VERSION

Attributes

search_hash[RW]

Public Class Methods

api_key() click to toggle source
# File lib/brewry.rb, line 25
def self.api_key
  @@api_key
end
api_key=(apikey) click to toggle source
# File lib/brewry.rb, line 29
def self.api_key=(apikey)
  @@api_key = apikey
end
configure() { |self| ... } click to toggle source

Brewry.configure do |config|

config.api_key = 'some_api_key'

end

# File lib/brewry.rb, line 21
def self.configure
  yield self
end
method_missing(meth, *args, &block) click to toggle source

override method_missing in order to create dynamic methods for searches. Ex.: BreweryDB.search_beers(name: 'Corona Light') BreweryDB.search_styles()

Calls superclass method
# File lib/brewry.rb, line 39
def self.method_missing(meth, *args, &block)
  if meth.to_s =~ /^search_(.+)$/
    search_for($1, *args)
  else
    super
  end
end
raw_query(path, options) click to toggle source

returns raw httparty query. See www.brewerydb.com/developers/docs for full range of options

# File lib/brewry.rb, line 58
def self.raw_query(path, options)
  query = build_query(options)
  get(path, query)
end

Protected Class Methods

build_query(search = {}) click to toggle source
# File lib/brewry.rb, line 89
def self.build_query(search = {})
  search_hash = clean_search_hash
  search_hash[:query].merge! search; search_hash
end
clean_search_hash() click to toggle source
# File lib/brewry.rb, line 99
def self.clean_search_hash
  unless @@api_key
    raise MissingApiKey, "You must set a brewerydb api key before you can "\
                         "use this gem. Please see http://www.brewerydb.com"\
                         "/developers/docs for more information."
  end
  { query: { key: @@api_key } }
end
query_error(search) click to toggle source
# File lib/brewry.rb, line 94
def self.query_error(search)
  self.underscore_and_symbolize search
  # TODO: throw some exception or special fail construct
end
return_pretty_results(payload) click to toggle source

underscore_and_symbolize is defined in utils/api_utilities.rb

# File lib/brewry.rb, line 81
def self.return_pretty_results(payload)
  return query_error(payload) if payload['status'] == 'failure'
  data = payload.parsed_response['data']
  data.map do |obj|
    obj = self.underscore_and_symbolize obj
  end if data
end
search_for(path, options = {}) click to toggle source

Used by the method_missing method for dynamic searches

# File lib/brewry.rb, line 74
def self.search_for(path, options = {})
  query   = build_query(options)
  search  = get("/#{path}", query)
  return_pretty_results(search)
end
underscore_and_symbolize(obj, foreignkeys = :id) click to toggle source

TODO: User should be able to change the foreignkeys argument

# File lib/brewry.rb, line 109
def self.underscore_and_symbolize(obj, foreignkeys = :id)
  obj.inject({}) do |hash, (key, val)|
    val = underscore_and_symbolize(val) if val.kind_of? Hash
    if key == 'id' && foreignkeys != :id
      hash.shift
      hash[foreignkeys] = val
    else
      hash[key.underscore.to_sym] = val
    end
    hash
  end
end