class FoodFishParser::Flat::Parser

Public Class Methods

new() click to toggle source

Create a new fish detail parser @return [FoodFishParser::Flat::Parser]

# File lib/food_fish_parser/flat/parser.rb, line 14
def initialize
end

Public Instance Methods

parse(s, **options) click to toggle source

Parse food fish text into a structured representation.

@param s [String] text to parse @return [Array<Hash>] structured representation of fish details (maximum one item)

# File lib/food_fish_parser/flat/parser.rb, line 21
def parse(s, **options)
  names = FishName.find_all(s)
  areas = AreaName.find_all(s) + AreaFao.find_all(s)
  catch_methods = CatchMethod.find_all(s)
  aquac_methods = AquacMethod.find_all(s)

  is_wild = catch_methods.any? || Kind.is_wild?(s)
  is_aquac = aquac_methods.any? || Kind.is_aquac?(s)

  return [] unless names.any? || aquac_methods.any? || catch_methods.any? || areas.any?

  attrs = {
    names: names,
    catch_areas: [],
    catch_methods: catch_methods,
    aquaculture_areas: [],
    aquaculture_methods: aquac_methods
  }

  if is_wild && !is_aquac
    [attrs.merge(catch_areas: areas)]
  elsif !is_wild && is_aquac
    [attrs.merge(aquaculture_areas: areas)]
  elsif areas.any?
    # We have a problem: either there are multiple fish and they're a mix of
    # wild and aquaculture fish, or there is no such indication at all.
    # For now, we return it in a non-standard way (this needs to be tackled).
    [attrs.merge(areas: areas)]
  else
    # just names
    [attrs]
  end
end