Class: FoodFishParser::Flat::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/food_fish_parser/flat/parser.rb

Instance Method Summary collapse

Constructor Details

#initializeFoodFishParser::Flat::Parser

Create a new fish detail parser



14
15
# File 'lib/food_fish_parser/flat/parser.rb', line 14

def initialize
end

Instance Method Details

#parse(s, **options) ⇒ Array<Hash>

Parse food fish text into a structured representation.

Parameters:

  • s (String)

    text to parse

Returns:

  • (Array<Hash>)

    structured representation of fish details (maximum one item)



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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