Class: Ziptz
- Inherits:
-
Object
- Object
- Ziptz
- Defined in:
- lib/ziptz.rb,
lib/ziptz/version.rb
Defined Under Namespace
Classes: ZipNotFound
Constant Summary collapse
- VERSION =
'6.2.0'
Instance Method Summary collapse
- #close ⇒ Object
- #db ⇒ Object
-
#initialize(path = nil, db: nil) ⇒ Ziptz
constructor
A new instance of Ziptz.
- #inspect ⇒ Object
- #time_zone_name(zip) ⇒ Object
- #time_zone_offset(zip) ⇒ Object
- #time_zone_uses_dst?(zip) ⇒ Boolean
- #zips(tz_name) ⇒ Object
Constructor Details
#initialize(path = nil, db: nil) ⇒ Ziptz
Returns a new instance of Ziptz.
9 10 11 12 13 |
# File 'lib/ziptz.rb', line 9 def initialize(path = nil, db: nil) @db_path = path || db raise ArgumentError, 'Database path is required' if @db_path.nil? || @db_path.empty? raise ArgumentError, 'Database file does not exist' unless File.exist?(@db_path) end |
Instance Method Details
#close ⇒ Object
24 25 26 27 |
# File 'lib/ziptz.rb', line 24 def close Thread.current[:ziptz_db]&.delete(@db_path)&.close nil end |
#db ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/ziptz.rb', line 15 def db connections = Thread.current[:ziptz_db] ||= {} connections[@db_path] ||= begin db = SQLite3::Database.open(@db_path) db.results_as_hash = true db end end |
#inspect ⇒ Object
51 52 53 |
# File 'lib/ziptz.rb', line 51 def inspect "#<#{self.class}:#{object_id}>" end |
#time_zone_name(zip) ⇒ Object
29 30 31 |
# File 'lib/ziptz.rb', line 29 def time_zone_name(zip) time_zone_info(zip)['time_zone'] end |
#time_zone_offset(zip) ⇒ Object
33 34 35 |
# File 'lib/ziptz.rb', line 33 def time_zone_offset(zip) time_zone_info(zip)['offset'] end |
#time_zone_uses_dst?(zip) ⇒ Boolean
37 38 39 |
# File 'lib/ziptz.rb', line 37 def time_zone_uses_dst?(zip) time_zone_info(zip)['observes_dst'] == 1 end |
#zips(tz_name) ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/ziptz.rb', line 41 def zips(tz_name) sql = <<~SQL select zip_code from zip_codes where time_zone = ? order by zip_code SQL db.execute(sql, tz_name).map { |row| row['zip_code'] } end |