class Tankard::Api::Search

Access for the /search route on brewerydb

@see www.brewerydb.com/developers/docs-endpoint/search_index @author Matthew Shafer

Attributes

http_client[R]
http_request_parameters[R]

Public Class Methods

new(request, options = {}) click to toggle source

Initializes a new object

@param request [Tankard::Request] @param options [Hash] @return [Tankard::Api::Search]

# File lib/tankard/api/search.rb, line 20
def initialize(request, options = {})
  @http_client = request
  @http_request_parameters = Hashie::Mash.new(options)
end

Public Instance Methods

each(&block) click to toggle source

Calls the given block once for each result

@yieldparam [Hash] hash containing individual beer information @raise [Tankard::Error::MissingParameter] when the id is not set @raise [Tankard::Error::ApiKeyUnauthorized] when an api key is not valid @raise [Tankard::Error::InvalidResponse] when no data is returned fron the api @raise [Tankard::Error::HttpError] when a status other than 200 or 401 is returned @raise [Tankard::Error::LoadError] when multi json is unable to decode json

Calls superclass method Tankard::Api::Utils::PageFinders#each
# File lib/tankard/api/search.rb, line 33
def each(&block)
  raise_if_required_options_not_set
  super(&block)
end
geo_point(latitude, longitude) click to toggle source

search for berweries around a specific point. sets the endpoint to geo/point and passes lat/lng as parameters

@param latitude [Float] @param longitude [Float] @return [self] returns itself

# File lib/tankard/api/search.rb, line 93
def geo_point(latitude, longitude)
  @http_request_parameters.lat = latitude
  @http_request_parameters.lng = longitude
  @http_request_parameters.endpoint = 'geo/point'
  self
end
page(number) click to toggle source

Page number to request

@param number [Integer] @return [self] returns itself

# File lib/tankard/api/search.rb, line 51
def page(number)
  @http_request_parameters.p = number
  self
end
params(options = {}) click to toggle source

Additional parameters to send with the request

@param options [Hash] @return [self] returns itself

# File lib/tankard/api/search.rb, line 60
def params(options = {})
  options.each_pair do |key, value|
    @http_request_parameters[key] = value
  end
  self
end
query(search_query) click to toggle source

Query to search for

@param search_query [String] @return [self] returns itself

# File lib/tankard/api/search.rb, line 42
def query(search_query)
  @http_request_parameters.q = search_query
  self
end
type(search_type) click to toggle source

Type of search to perform

@param search_type [String] @return [self] returns itself

# File lib/tankard/api/search.rb, line 71
def type(search_type)
  @http_request_parameters.type = search_type
  self
end
upc(upc_code) click to toggle source

search for a specific upc. sets the endpoint to upc and passes upc_code as a parameter

@param upc_code [Integer] @return [self] returns itself

# File lib/tankard/api/search.rb, line 81
def upc(upc_code)
  @http_request_parameters.code = upc_code
  @http_request_parameters.endpoint = 'upc'
  self
end

Private Instance Methods

http_request_uri() click to toggle source
# File lib/tankard/api/search.rb, line 105
def http_request_uri
  @request_endpoint = "/#{@http_request_parameters.delete(:endpoint)}" if @http_request_parameters.endpoint?
  endpoint = 'search'
  endpoint += @request_endpoint if @request_endpoint
  endpoint
end
raise_if_required_options_not_set() click to toggle source
# File lib/tankard/api/search.rb, line 112
def raise_if_required_options_not_set
  if @http_request_parameters.endpoint.nil?
    fail Tankard::Error::MissingParameter, 'No search query set' unless @http_request_parameters.q?
  elsif @http_request_parameters.endpoint == 'upc'
    fail Tankard::Error::MissingParameter, 'missing parameter: code' unless @http_request_parameters.code?
  elsif @http_request_parameters.endpoint == 'geo/point'
    fail Tankard::Error::MissingParameter, 'missing Parameters: lat, lng' unless @http_request_parameters.lat? && @http_request_parameters.lng?
  end
end