Class: Posty

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(database = nil) ⇒ Posty

Returns a new instance of Posty.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/posty.rb', line 21

def initialize database = nil
  if database.nil?
    database = Posty.gem_database
    unless File.exist? database
      raise IOError, "No database found in gem. Have you run posty-init -g (possibly with sudo)?"
    end
  end

  unless File.exist? database
    raise IOError, "No database found at #{database}. Have you run posty-init -d '#{database}'?"
  end

  @db = SQLite3::Database.new(database)
  @db.results_as_hash = true
end

Class Method Details

.gem_databaseObject



42
43
44
# File 'lib/posty.rb', line 42

def self.gem_database
  "#{File.dirname(__FILE__)}/posty/codepointopen.sqlite3"
end

.postcode(code, database = nil) ⇒ Object



46
47
48
# File 'lib/posty.rb', line 46

def self.postcode code, database = nil
  Posty.new(database).postcode(code)
end

Instance Method Details

#postcode(code) ⇒ Object



37
38
39
40
# File 'lib/posty.rb', line 37

def postcode code
  code = code.gsub(/[^\w\d]/,'').upcase
  PostCode.new(@db.get_first_row('SELECT * FROM codepoint WHERE postcode = ?', code))
end