Class: PostCode::UK

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(postcode, latitude, longitude) ⇒ UK

Returns a new instance of UK.



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

def initialize(postcode, latitude, longitude)
  self.postcode = postcode
  self.latitude = latitude
  self.longitude = longitude
end

Instance Attribute Details

#latitudeObject

Returns the value of attribute latitude.



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

def latitude
  @latitude
end

#longitudeObject

Returns the value of attribute longitude.



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

def longitude
  @longitude
end

#postcodeObject

Returns the value of attribute postcode.



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

def postcode
  @postcode
end

Class Method Details

.databaseObject



16
17
18
19
20
21
22
# File 'lib/post_code/uk.rb', line 16

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

.database=(path) ⇒ Object



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

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

.lookup(postcode) ⇒ Object



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

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

  if data.size > 0
    self.new(postcode, data[0], data[1])
  end
end