Class: Noaaish::Extractor

Inherits:
Object
  • Object
show all
Defined in:
lib/noaaish/extractor.rb

Constant Summary collapse

NOAA_FIELDS =

YYYYMMDDHH

{'HR_TIME' => (13...23), # YYYYMMDDHH
'HR'      => (21...23),
'MN'      => (23...25),
'DIR'     => (26...29),
'SPD'     => (30...33),
'GUS'     => (34...37),
'CLG'     => (38...41),
'SKC'     => (42...45),
'L'       => (46...47),
'M'       => (48...49),
'H'       => (50...51),
'VSB'     => (52...56),
'MW1'     => (57...59),
'MW2'     => (60...62),
'MW3'     => (63...65),
'MW4'     => (66...68),
'AW1'     => (69...71),
'AW2'     => (72...74),
'AW3'     => (75...77),
'AW4'     => (78...80),
'W'       => (81...82),
'TEMP'    => (83...87),
'DEWP'    => (88...92),
'SLP'     => (93...99),
'ALT'     => (100...105),
'STP'     => (106...112),
'MAX'     => (113...116),
'MIN'     => (117...120),
'PCP01'   => (121...126),
'PCP06'   => (127...132),
'PCP24'   => (133...138),
'PCPXX'   => (139...144),
'SD'      => (145...147),}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, output = destination, format = :hash) ⇒ Extractor

Returns a new instance of Extractor.



6
7
8
9
10
# File 'lib/noaaish/extractor.rb', line 6

def initialize(input, output=destination, format=:hash)
  @input = input
  @destination = output
  @format = format
end

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format.



12
13
14
# File 'lib/noaaish/extractor.rb', line 12

def format
  @format
end

#inputObject (readonly)

Returns the value of attribute input.



12
13
14
# File 'lib/noaaish/extractor.rb', line 12

def input
  @input
end

Instance Method Details

#callObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/noaaish/extractor.rb', line 14

def call
  # For each line, create a json record.
  hash = input.each_line.map { |line|
    NOAA_FIELDS.inject({}) do |hash, (field, range)|
      hash.merge(field => line[range])
    end
  }

  case format
  when :json
    destination << hash.to_json
    destination
  when :hash
    hash
  end
end

#destinationObject



31
32
33
# File 'lib/noaaish/extractor.rb', line 31

def destination
  @destination ||= Tempfile.new('noaaish-translator')
end