class Mat::API

Attributes

config[RW]

Public Class Methods

new(config = nil) { |config| ... } click to toggle source
# File lib/mat/api.rb, line 11
def initialize(config = nil)
  @config = config || API::Config.new

  yield(@config) if block_given?
end

Public Instance Methods

foodstuff(number, nutrient = nil) click to toggle source
# 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) click to toggle source
# File lib/mat/api.rb, line 24
def foodstuffs(query = nil)
  query.nil?? get("foodstuff") : get("foodstuff?query=#{URI::encode(query)}")
end
get(path) click to toggle source
# 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) click to toggle source
# File lib/mat/api.rb, line 28
def nutrient(slug)
  get("nutrient/#{slug}")
end
nutrients() click to toggle source
# File lib/mat/api.rb, line 32
def nutrients
  get("nutrient")
end

Private Instance Methods

http_get(path) click to toggle source
# File lib/mat/api.rb, line 43
def http_get(path)
  config.http_client.get(uri(path))
end
load_json(doc) click to toggle source
# File lib/mat/api.rb, line 51
def load_json(doc)
  config.json_parser.load(doc)
end
uri(endpoint = nil) click to toggle source
# File lib/mat/api.rb, line 47
def uri(endpoint = nil)
  URI.parse("#{config.base_url}#{endpoint}")
end