class FDDB::API

Public Class Methods

new(api_key, lang = 'de', format= :json) click to toggle source
# File lib/fddb.rb, line 11
def initialize (api_key, lang = 'de', format= :json)
  @api_key = api_key
  @lang = lang
  @base_url = 'http://fddb.info/api/v8'
  @format  =  format
end

Public Instance Methods

get_item(query) click to toggle source
# File lib/fddb.rb, line 18
def get_item (query)
  response = make_http_request :item, query
  FDDB::Item.new response
end

Private Instance Methods

make_http_request(type, query, *params) click to toggle source

makes an http_request to fddb database @param type {:item or :search} @param query - the query @param params - additional http params

examples: make_http_request :search, ‘beer’, ‘lang’ => ‘en’

# File lib/fddb.rb, line 36
def make_http_request (type, query, *params)
  params = params.reduce(:+)
  params ||= {}
  params['apikey'] ||= @api_key
  params['lang'] ||= @lang

  if type == :item
    uri =  URI("#{@base_url}/item/id_#{query}.xml")
  elsif type == :search
    uri = uri =  URI("#{@base_url}/search/item.xml")
    params['q'] = query
  else
    throw('undefined search type')
  end

  uri.query = URI.encode_www_form params

  Net::HTTP.get uri
end