Class: PostCode::UK

Inherits:
Object
  • Object
show all
Defined in:
lib/postcode/uk.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(outward, inward, latitude, longitude) ⇒ UK

Returns a new instance of UK.



5
6
7
8
9
10
# File 'lib/postcode/uk.rb', line 5

def initialize(outward, inward, latitude, longitude)
  self.outward = outward
  self.inward = inward
  self.latitude = latitude
  self.longitude = longitude
end

Instance Attribute Details

#inwardObject

Returns the value of attribute inward.



3
4
5
# File 'lib/postcode/uk.rb', line 3

def inward
  @inward
end

#latitudeObject

Returns the value of attribute latitude.



3
4
5
# File 'lib/postcode/uk.rb', line 3

def latitude
  @latitude
end

#longitudeObject

Returns the value of attribute longitude.



3
4
5
# File 'lib/postcode/uk.rb', line 3

def longitude
  @longitude
end

#outwardObject

Returns the value of attribute outward.



3
4
5
# File 'lib/postcode/uk.rb', line 3

def outward
  @outward
end

Class Method Details

.databaseObject



25
26
27
28
29
30
31
# File 'lib/postcode/uk.rb', line 25

def database
  if !@database
    self.database = File.dirname(__FILE__) + "/../../databases/database.uk.txt"
  end
  
  @database
end

.database=(path) ⇒ Object



21
22
23
# File 'lib/postcode/uk.rb', line 21

def database=(path)
  @database = FasterCSV.read(path, :col_sep => " ")
end

.lookup(postcode) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/postcode/uk.rb', line 33

def lookup(postcode)
  postcode.gsub!(/\s+/, "").downcase!
  
  data = database.select {|row| row if "#{row[2]}#{row[3]}".downcase == postcode }.first

  if data
    self.new(data[2], data[3], data[0], data[1])
  end
end

Instance Method Details

#to_sObject



16
17
18
# File 'lib/postcode/uk.rb', line 16

def to_s
  unit
end

#unitObject



12
13
14
# File 'lib/postcode/uk.rb', line 12

def unit
  "#{outward} #{inward}"
end