class RateBeer::Client

Constants

DEFAULT_OPTIONS
SORT_OPTIONS

Public Class Methods

new(options = {}) click to toggle source
# File lib/rate_beer/client.rb, line 23
def initialize(options = {})
  merged_options = RateBeer.options.merge(options)

  Configuration::VALID_CONFIG_KEYS.each do |key|
    send("#{key}=", merged_options[key])
  end
end

Public Instance Methods

beer_info_by_id(beer_id) click to toggle source
# File lib/rate_beer/client.rb, line 31
def beer_info_by_id(beer_id)
  response = self.class.get("/bff.asp?bd=#{beer_id}&k=#{api_key}&vg=1")
  parsed_response(response)
end
beer_info_by_name(beer_name) click to toggle source
# File lib/rate_beer/client.rb, line 36
def beer_info_by_name(beer_name)
  beer_name = URI.escape(beer_name)
  response = self.class.get("/bff.asp?bn=#{beer_name}&k=#{api_key}&vg=1&rc=1")
  parsed_response(response)
end
beer_reviews(beer_id, options = {}) click to toggle source
# File lib/rate_beer/client.rb, line 42
def beer_reviews(beer_id, options = {})
  options = merge_with_default_options(options)
  response = self.class.get("/gr.asp?bid=#{beer_id}&s=#{options[:sort_by]}&p=#{options[:page]}&k=#{api_key}")
  parsed_response(response)
end

Private Instance Methods

hash_with_transformed_keys(hash) click to toggle source
# File lib/rate_beer/client.rb, line 64
def hash_with_transformed_keys(hash)
  hash.inject({}){|memo,(k,v)| memo[k.underscore] = v; memo}
end
merge_with_default_options(options) click to toggle source
# File lib/rate_beer/client.rb, line 50
def merge_with_default_options(options)
  DEFAULT_OPTIONS.merge(options)
end
parsed_response(response) click to toggle source
# File lib/rate_beer/client.rb, line 54
def parsed_response(response)
  parsed_array = JSON.parse(response.body)
  parsed_array.each_with_index do |hash, index|
    hash = hash_with_transformed_keys(hash)
    hash = transformed_to_mash(hash)
    parsed_array[index] = hash
  end
  parsed_array
end
transformed_to_mash(hash) click to toggle source
# File lib/rate_beer/client.rb, line 68
def transformed_to_mash(hash)
  Hashie::Mash.new(hash)
end