Class: Sports::Country

Inherits:
Object
  • Object
show all
Defined in:
lib/sportdb/search/structs_world.rb

Class Method Summary collapse

Class Method Details

._searchObject

use service/api or such - why? why not?



15
16
17
# File 'lib/sportdb/search/structs_world.rb', line 15

def self._search #### use service/api or such - why? why not?
    SportDb::Import.world.countries
end

.find(q) ⇒ Object Also known as: []

find by code (first) or name (second)



30
31
32
# File 'lib/sportdb/search/structs_world.rb', line 30

def self.find( q )   ## find by code (first) or name (second)
    _search.find_by_name_or_code( q )
end

.find_by(code: nil, name: nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/sportdb/search/structs_world.rb', line 19

def self.find_by( code: nil, name: nil )
    ## todo/fix upstream - change to find_by( code:, name:, ) too - why? why not?
    if code && name.nil?
      _search.find_by_code( code )
    elsif name && code.nil?
      _search.find_by_name( name )
    else
      raise ArgumentError, "CountrySearch#find_by - one (and only one arg) required - code: or name:"
    end
end

.parse_heading(line) ⇒ Object Also known as: heading

split/parse country line

split on bullet e.g.
 split into name and code with regex - make code optional

Examples:
  Österreich • Austria (at)
  Österreich • Austria
  Austria
  Deutschland (de) • Germany

 todo/check: support more formats - why? why not?
     e.g.  Austria, AUT  (e.g. with comma - why? why not?)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/sportdb/search/structs_world.rb', line 48

def self.parse_heading( line )
  values = line.split( '' )   ## use/support multi-lingual separator
  country = nil
  values.each do |value|
     value = value.strip
     ## check for trailing country code e.g. (at), (eng), etc
     ##   allow code 1 to 5 for now - northern cyprus(fifa) with 5 letters?.
     ##     add/allow  gb-eng, gb-wal (official iso2!!), in the future too - why? why not?
     if value =~ /[ ]+\((?<code>[A-Za-z]{1,5})\)$/  ## e.g. Austria (at)
       code =  $~[:code]
       name = value[0...(value.size-code.size-2)].strip  ## note: add -2 for brackets
       candidates = [ find_by( code: code ), find_by( name: name ) ]
       if candidates[0].nil?
         puts "** !!! ERROR Country.parse_heading - unknown code >#{code}< in line: #{line}"
         pp line
         exit 1
       end
       if candidates[1].nil?
         puts "** !!! ERROR Country.parse_heading - unknown name >#{code}< in line: #{line}"
         pp line
         exit 1
       end
       if candidates[0] != candidates[1]
         puts "** !!! ERROR Country.parse_heading - name and code do NOT match the same country:"
         pp line
         pp candidates
         exit 1
       end
       if country && country != candidates[0]
         puts "** !!! ERROR Country.parse_heading - names do NOT match the same country:"
         pp line
         pp country
         pp candidates
         exit 1
       end
       country = candidates[0]
     else
       ## just assume value is name or code
       candidate = find( value )
       if candidate.nil?
         puts "** !!! ERROR Country.parse_heading - unknown name or code >#{value}< in line: #{line}"
         pp line
         exit 1
       end
       if country && country != candidate
         puts "** !!! ERROR Country.parse_heading - names do NOT match the same country:"
         pp line
         pp country
         pp candidate
         exit 1
       end
       country = candidate
     end
  end
  country
end