Class: Mat::Foodstuff

Inherits:
Object
  • Object
show all
Defined in:
lib/mat/foodstuff.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Foodstuff

Returns a new instance of Foodstuff.



21
22
23
# File 'lib/mat/foodstuff.rb', line 21

def initialize(data)
  @data = data || {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/mat/foodstuff.rb', line 39

def method_missing(m, *args, &block)
  if nutrient_values.respond_to?(:keys) &&
     nutrient_values.keys.include?(m.to_s)
    nutrient_values[m.to_s]
  else
    super
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



19
20
21
# File 'lib/mat/foodstuff.rb', line 19

def data
  @data
end

Class Method Details

.all(query) ⇒ Object



10
11
12
# File 'lib/mat/foodstuff.rb', line 10

def all(query)
  Mat.api.foodstuffs(query).map { |d| new(d) }
end

.find(number, nutrient = nil) ⇒ Object



6
7
8
# File 'lib/mat/foodstuff.rb', line 6

def find(number, nutrient = nil)
  new get_data(number, nutrient)
end

.get_data(number, nutrient = nil) ⇒ Object



14
15
16
# File 'lib/mat/foodstuff.rb', line 14

def get_data(number, nutrient = nil)
  Mat.api.foodstuff(number, nutrient)
end

Instance Method Details

#get_data!Object



48
49
50
51
# File 'lib/mat/foodstuff.rb', line 48

def get_data!
  initialize self.class.get_data(number)
  self
end

#nameObject



25
26
27
# File 'lib/mat/foodstuff.rb', line 25

def name
  data['name'].to_s.strip
end

#numberObject



29
30
31
# File 'lib/mat/foodstuff.rb', line 29

def number
  data['number']
end

#nutrient_valuesObject



33
34
35
36
37
# File 'lib/mat/foodstuff.rb', line 33

def nutrient_values
  get_data! if data['nutrientValues'].nil?

  data['nutrientValues']
end