Class: FDDB::Item

Inherits:
Object
  • Object
show all
Includes:
Parser
Defined in:
lib/fddb/item.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Parser

#parse

Instance Attribute Details

#itemObject (readonly)

Returns the value of attribute item.



5
6
7
# File 'lib/fddb/item.rb', line 5

def item
  @item
end

Class Method Details

.init(xml_string) ⇒ Object



8
9
10
11
12
# 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

Instance Method Details

#check_item(xml_string) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 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) ⇒ Object

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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 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