Class: OsMapRef::InputProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/os_map_ref/input_processor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ InputProcessor

Returns a new instance of InputProcessor.



5
6
7
# File 'lib/os_map_ref/input_processor.rb', line 5

def initialize(input)
  @input = input
end

Instance Attribute Details

#inputObject (readonly)

Returns the value of attribute input.



3
4
5
# File 'lib/os_map_ref/input_processor.rb', line 3

def input
  @input
end

Instance Method Details

#easting_and_northingObject



77
78
79
# File 'lib/os_map_ref/input_processor.rb', line 77

def easting_and_northing
  @easting_and_northing ||= get_easting_and_northing
end

#easting_northing_paramsObject



24
25
26
27
28
29
# File 'lib/os_map_ref/input_processor.rb', line 24

def easting_northing_params
  {
    easting: easting_and_northing[0],
    northing: easting_and_northing[1]
  }
end

#easting_northing_patternObject



86
87
88
# File 'lib/os_map_ref/input_processor.rb', line 86

def easting_northing_pattern
  /(\d{3,6})[\,\s]+(\d{3,6})/
end

#get_easting_and_northingObject



81
82
83
84
# File 'lib/os_map_ref/input_processor.rb', line 81

def get_easting_and_northing
  match = easting_northing_pattern.match input
  (1..2).collect{|n| match[n].ljust(6, '0').to_i}
end

#get_map_reference_elementsObject



43
44
45
46
# File 'lib/os_map_ref/input_processor.rb', line 43

def get_map_reference_elements
  match = map_reference_pattern.match input
  (1..3).collect{|n| match[n]}
end

#grid_lettersObject



48
49
50
# File 'lib/os_map_ref/input_processor.rb', line 48

def grid_letters
   map_reference_elements[0]
end

#map_reference_eastingObject



52
53
54
# File 'lib/os_map_ref/input_processor.rb', line 52

def map_reference_easting
  map_reference_elements[1]
end

#map_reference_elementsObject



39
40
41
# File 'lib/os_map_ref/input_processor.rb', line 39

def map_reference_elements
  @map_reference_elements ||= get_map_reference_elements
end

#map_reference_northingObject



56
57
58
# File 'lib/os_map_ref/input_processor.rb', line 56

def map_reference_northing
  map_reference_elements[2]
end

#map_reference_paramsObject



20
21
22
# File 'lib/os_map_ref/input_processor.rb', line 20

def map_reference_params
  {map_reference: processed_map_reference}
end

#map_reference_patternObject



73
74
75
# File 'lib/os_map_ref/input_processor.rb', line 73

def map_reference_pattern
  /([a-zA-Z]{2})\s*(\d{3,5})\s*(\d{3,6})/
end

#northing_longer_than_easting?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/os_map_ref/input_processor.rb', line 69

def northing_longer_than_easting?
  map_reference_northing.length > map_reference_easting.length
end

#padded_map_reference_eastingObject



60
61
62
# File 'lib/os_map_ref/input_processor.rb', line 60

def padded_map_reference_easting
  map_reference_easting.ljust(5, '0')
end

#padded_map_reference_northingObject



64
65
66
67
# File 'lib/os_map_ref/input_processor.rb', line 64

def padded_map_reference_northing
  target_length = northing_longer_than_easting? ? 6 : 5
  map_reference_northing.ljust(target_length, '0')
end

#paramsObject



9
10
11
12
13
14
15
16
17
18
# File 'lib/os_map_ref/input_processor.rb', line 9

def params
  case input
  when map_reference_pattern
    map_reference_params
  when easting_northing_pattern
    easting_northing_params
  else
    raise OsMapRef::Error, "Unable to process input #{input}"
  end
end

#processed_map_referenceObject



31
32
33
34
35
36
37
# File 'lib/os_map_ref/input_processor.rb', line 31

def processed_map_reference
  [
    grid_letters,
    padded_map_reference_easting,
    padded_map_reference_northing
  ].join(' ')
end