Class: Mat::API

Inherits:
Object
  • Object
show all
Defined in:
lib/mat/api.rb,
lib/mat/api/config.rb

Defined Under Namespace

Classes: Config

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = nil) {|@config| ... } ⇒ API

Returns a new instance of API.

Yields:



11
12
13
14
15
# File 'lib/mat/api.rb', line 11

def initialize(config = nil)
  @config = config || API::Config.new

  yield(@config) if block_given?
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



9
10
11
# File 'lib/mat/api.rb', line 9

def config
  @config
end

Instance Method Details

#foodstuff(number, nutrient = nil) ⇒ Object



17
18
19
20
21
22
# File 'lib/mat/api.rb', line 17

def foodstuff(number, nutrient = nil)
  endpoint  = "foodstuff/#{number}"
  endpoint += "?nutrient=#{nutrient}" if nutrient

  get(endpoint)
end

#foodstuffs(query = nil) ⇒ Object



24
25
26
# File 'lib/mat/api.rb', line 24

def foodstuffs(query = nil)
  query.nil?? get("foodstuff") : get("foodstuff?query=#{URI::encode(query)}")
end

#get(path) ⇒ Object



36
37
38
39
# File 'lib/mat/api.rb', line 36

def get(path)
  response = http_get(path)
  load_json(response.body) if response && response.body
end

#nutrient(slug) ⇒ Object



28
29
30
# File 'lib/mat/api.rb', line 28

def nutrient(slug)
  get("nutrient/#{slug}")
end

#nutrientsObject



32
33
34
# File 'lib/mat/api.rb', line 32

def nutrients
  get("nutrient")
end