class DietaryDsl::Food

Parte de la clase que interactúa con la base de datos

Parte de la clase que presenta sus datos

Attributes

http[RW]
name[R]
nombre[R]
raw[R]
values[R]

Public Class Methods

build_attributes(b, attributes) click to toggle source
# File lib/dietary_dsl/bedca_api/food/orm.rb, line 37
def self.build_attributes(b, attributes)
  attributes
    .reject { |(key)| key == 'foodvalue' }
    .each do |(key)|
      b.atribute name: key
    end
end
find(id) click to toggle source
# File lib/dietary_dsl/bedca_api/food/orm.rb, line 86
def self.find(id)
  find_by(id: id)
end
find_by(data) click to toggle source
# File lib/dietary_dsl/bedca_api/food/orm.rb, line 90
def self.find_by(data)
  find_by_and_relation(data, RELATIONS[:equal])
end
find_by_and_relation(data, relation) { |b| ... } click to toggle source
# File lib/dietary_dsl/bedca_api/food/orm.rb, line 65
def self.find_by_and_relation(data, relation)
  xml = query do |b|
    data.each do |(key), value|
      where(b, key, relation, value)
    end
    yield b if block_given?
    order(b, :nombre)
  end

  data = sanitize_response request xml
  return nil if data.nil?

  Food.new(data)
end
find_by_like(data) click to toggle source
# File lib/dietary_dsl/bedca_api/food/orm.rb, line 94
def self.find_by_like(data)
  find_by_and_relation(data, RELATIONS[:like]) do |b|
    data.each do |(key), value|
      where(b, key, RELATIONS[:begin_with], value[0])
    end
  end
end
new(values) click to toggle source
# File lib/dietary_dsl/bedca_api/food/orm.rb, line 102
def initialize(values)
  @data = rehash(values)
  @groups = @data[:foodvalue]
            .group_by { |value| value['cg_descripcion'] }
            .each_with_object({}) do |(key, vals), hash|
              hash[CATEGORIES[key]] = FoodValues.new(vals.first['cg_descripcion'], vals)
            end

  @data.delete(:foodvalue)
  @data[:groups] = @groups

  @general_values = download_general_values
end
order(b, field, direction = 'ASC') click to toggle source
# File lib/dietary_dsl/bedca_api/food/orm.rb, line 80
def self.order(b, field, direction = 'ASC')
  b.order(ordtype: direction) do
    b.atribute3(name: ATTRIBUTES.key(field))
  end
end
query(attributes = ATTRIBUTES) { |b| ... } click to toggle source
# File lib/dietary_dsl/bedca_api/food/orm.rb, line 45
def self.query(attributes = ATTRIBUTES)
  Builder::XmlMarkup.new.foodquery do |b|
    b.type level: 2
    b.selection do
      build_attributes(b, attributes)
    end
    yield b
  end
end
request(xml) click to toggle source
# File lib/dietary_dsl/bedca_api/food/orm.rb, line 25
def self.request(xml)
  response = @http.request_post URL, XML_HEADER + xml, 'Content-Type' => 'text/xml'
  body = response.read_body
  return Hash.from_xml body
rescue StandardError => e
  raise "El servidor ha devuelto un error: #{e.message}"
end
sanitize_response(response) click to toggle source
# File lib/dietary_dsl/bedca_api/food/orm.rb, line 33
def self.sanitize_response(response)
  response.dig 'foodresponse', 'food'
end
where(b, field, relation, value) click to toggle source
# File lib/dietary_dsl/bedca_api/food/orm.rb, line 55
def self.where(b, field, relation, value)
  b.condition do
    b.cond1 do
      b.atribute1(name: ATTRIBUTES.key(field))
    end
    b.relation(type: relation)
    b.cond3(value)
  end
end

Public Instance Methods

[](key) click to toggle source
# File lib/dietary_dsl/bedca_api/food/orm.rb, line 116
def [](key)
  return @data[key].delete(',') if %i[nombre name].include?(key)
  @data[key]
end
download_general_values() click to toggle source
# File lib/dietary_dsl/bedca_api/food/orm.rb, line 121
def download_general_values
  body = (Food.http.request_get "http://www.bedca.net/cocoon/svg/response#{self[:id]}langes.svg").read_body
  parse_svg body
end
kcal() click to toggle source
# File lib/dietary_dsl/bedca_api/food/others.rb, line 15
def kcal
  @data[:groups][:proximales][:energia_total][:cantidad]
    .gsub(/.+kJ/, '')
    .to_f * 0.238846
end
parse_svg(svg) click to toggle source
# File lib/dietary_dsl/bedca_api/food/orm.rb, line 126
def parse_svg(svg)
  (svg.scan SVG_REGEX).each_with_object({}) do |value, hash|
    hash[COMPONENTS[value.last]] = value.first.delete(' ')
    hash
  end
end
to_s() click to toggle source
# File lib/dietary_dsl/bedca_api/food/others.rb, line 21
    def to_s
      tables = @data[:groups]
               .map do |(_key, group)|
                 group.to_table
               end
               .join("\n")

      <<~TO_MARKDOWN
        # #{self[:nombre]}

        > #{self[:scientific_name]}

        ## Distribución de la Energía total

        Proteínas: #{@general_values[:proteina_total]}
        Grasas: #{@general_values[:grasa_total]}
        Carbohidratos: #{@general_values[:carbohidratos]}
        Alcohol: #{@general_values[:alcohol]}

        ## Información de composición (por 100 g de porción comestible)

        #{tables}
      TO_MARKDOWN
    end