class FDDB::Item

Attributes

item[R]

Public Class Methods

init(xml_string) click to toggle source
# File lib/fddb/item.rb, line 8
def self.init (xml_string)
  element = Item.new xml_string
  return nil if element.item['error']
  element
end
new(xml_string) click to toggle source
# File lib/fddb/item.rb, line 48
def initialize (xml_string)
  @item = check_item xml_string
end

Public Instance Methods

check_item(xml_string) click to toggle source
# File lib/fddb/item.rb, line 14
def check_item (xml_string)
  return xml_string if xml_string.class == Hash # if item is already a hash

  begin
    item = parse xml_string
    throw "Error - #{item['error']}" unless item['error'].nil?

    response = item['result']['item'] if xml_string.class == String
  rescue Exception => e
    puts e
    response = item
  end
  response
end
get_ingredients(query = nil) click to toggle source

returns the ingredients to an given element available filters are (:minerals, :vitamines, :general)

# File lib/fddb/item.rb, line 32
def get_ingredients (query = nil)
  return @item['data'] if query.nil?

  case query
    when :minerals
      search_ingredient 'm'
    when :vitamins
      search_ingredient 'v'
    when :general
      search_ingredient '(?!(a|l)).'
    else
      nil
  end
end

Private Instance Methods

search_ingredient(search) click to toggle source
# File lib/fddb/item.rb, line 52
def search_ingredient (search)
  get_ingredients.select {| key, value | key.to_s.match(/^#{search}/)}
end