Class: Chicago::ETL::Transformations::UkPostCode

Inherits:
Object
  • Object
show all
Defined in:
lib/chicago/etl/transformations/uk_post_code.rb

Overview

Cleans and reformats UK-based postcodes.

Transformations are based on observed errors in data entry, so shift key slips (i.e. typing ‘!’ where ‘1’ was meant) are corrected, as are use of numbers where letters were intended i.e. (0X -> OX for Oxfordshire postcodes).

Leaves BFPO postcodes alone.

Instance Method Summary collapse

Constructor Details

#initialize(column_name, &filter_block) ⇒ UkPostCode

Creates a new post code transformation.

Parameters:

  • Symbol

    column_name - the name of the column containing the post code.

  • Proc

    filter_block - an optional block, which takes a row. If the block returns false, the transformation will not be run. This can be useful to only run the transformation on UK addresses, based a country field in the row for example.



24
25
26
27
# File 'lib/chicago/etl/transformations/uk_post_code.rb', line 24

def initialize(column_name, &filter_block)
  @column_name = column_name
  @filter_block = filter_block
end

Instance Method Details

#call(row, errors = []) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/chicago/etl/transformations/uk_post_code.rb', line 29

def call(row, errors=[])
  return [row, errors] if @filter_block && !@filter_block.call(row)

  row[@column_name] = UkPostCodeField.new.
    normalize(row[@column_name])[:post_code]

  [row, errors]
end