Class: ScoreFormats::ScoreParser

Inherits:
Object
  • Object
show all
Includes:
LogUtils::Logging
Defined in:
lib/sportdb/formats/score/score_parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(lang:) ⇒ ScoreParser

Returns a new instance of ScoreParser.



83
84
85
86
87
88
89
# File 'lib/sportdb/formats/score/score_parser.rb', line 83

def initialize( lang: )
  @lang    = lang.to_sym   ## note: make sure lang is always a symbol for now (NOT a string)

  ## fallback to english if lang not available
  ##  todo/fix: add/issue warning - why? why not?
  @formats = FORMATS[ @lang ] || FORMATS[ :en ]
end

Instance Method Details

#find!(line) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/sportdb/formats/score/score_parser.rb', line 115

def find!( line )
  ### fix: add and match all-in-one literal first, followed by

  # note: always call after find_dates !!!
  #  scores match date-like patterns!!  e.g. 10-11  or 10:00 etc.
  #   -- note: score might have two digits too

  ### fix: depending on language allow 1:1 or 1-1
  ##   do NOT allow mix and match
  ##  e.g. default to en is  1-1
  ##    de is 1:1 etc.


  # extract score from line
  # and return it
  # note: side effect - removes date from line string

  score = nil
  @formats.each do |format|
    re  = format[0]
    tag = format[1]
    m = re.match( line )
    if m
      score = parse_matchdata( m )
      line.sub!( m[0], tag )
      break
    end
    # no match; continue; try next regex pattern
  end

  score  # note: nil if no match found
end

#parse(line) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/sportdb/formats/score/score_parser.rb', line 92

def parse( line )

   ##########
   ## todo/fix/check: add unicode to regular dash conversion - why? why not?
   ##  e.g. – becomes -  (yes, the letters a different!!!)
   #############

  score = nil
  @formats.each do |format|
    re = format[0]
    m = re.match( line )
    if m
      score = parse_matchdata( m )
      break
    end
    # no match; continue; try next regex pattern
  end

  ## todo/fix - raise ArgumentError - invalid score; no format match found
  score  # note: nil if no match found
end