zip_meta 0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,21 @@
1
+ class CreateZipMetaTable < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :zip_meta, :force => true do |t|
4
+ t.string :zip
5
+ t.string :city
6
+ t.string :state
7
+ t.float :latitude
8
+ t.float :longitude
9
+ t.integer :timezone
10
+ end
11
+ add_index :zip_meta, :zip
12
+ add_index :zip_meta, :timezone
13
+
14
+ # finally, load the data...
15
+ ZipMeta::MigrationUtil.load_zipcode_data
16
+ end
17
+
18
+ def self.down
19
+ drop_table :zip_meta
20
+ end
21
+ end
@@ -0,0 +1,19 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ # Generates a timestamped migration file containing the CreateZipMetaTable
5
+ # migration, which creates the "zip_meta" table and populates it with data from
6
+ # "zipcodes.csv", and drops this migration file into the user's "app/db/migrations" directory.
7
+ class ZipMetaMigrationGenerator < Rails::Generators::Base
8
+ include Rails::Generators::Migration
9
+
10
+ source_root File.expand_path("../templates", __FILE__)
11
+
12
+ def self.next_migration_number(path)
13
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
14
+ end
15
+
16
+ def create_migration_file
17
+ migration_template "create_zip_meta_table.rb", "db/migrate/create_zip_meta_table.rb"
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module ZipMeta
2
+ require File.expand_path('../zip_meta/railtie', __FILE__) if defined?(Rails)
3
+ end
@@ -0,0 +1,36 @@
1
+ require File.expand_path('../../core_ext/string', __FILE__)
2
+
3
+ module ZipMeta
4
+ # Contains a static "load_zipcode_data" method that parses
5
+ # the "zipcodes.csv" file and loads each row into
6
+ # the "zip_meta" database table.
7
+ class MigrationUtil
8
+ # Method to parse the "zipcodes.csv" file and load each row into
9
+ # the "zip_meta" database table.
10
+ def self.load_zipcode_data
11
+ require 'csv'
12
+
13
+ csv_path = File.expand_path('../../data/zipcode.csv', __FILE__)
14
+ csv_data = CSV.open(csv_path, 'r')
15
+ rows = []
16
+ csv_data.each do |row|
17
+ rows << row
18
+ end
19
+ keys = rows[0]
20
+ rows.delete_at(0)
21
+
22
+ connection = ActiveRecord::Base.connection
23
+
24
+ len = rows.length
25
+ rows.each_with_index do |row, index|
26
+ vals = "('#{row[0]}', '#{row[1].sql_escape_single_quotes}', '#{row[2].sql_escape_single_quotes}', #{row[3]}, #{row[4]}, #{row[5]})"
27
+
28
+ query = "INSERT INTO zip_meta(#{keys[0]}, #{keys[1]}, #{keys[2]}, #{keys[3]}, #{keys[4]}, #{keys[5]}) " + \
29
+ "VALUES #{vals}"
30
+ connection.execute(query)
31
+
32
+ puts "adding zip meta info row #{index} of #{len}\n"
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,21 @@
1
+ require 'rails'
2
+ require File.expand_path('../../core_ext/string', __FILE__)
3
+ require File.expand_path('../util', __FILE__)
4
+ require File.expand_path('../migration_util', __FILE__)
5
+
6
+ module ZipMeta
7
+ # The ZipMeta railtie implements the "configure_rails_initialization" callback
8
+ # and adds a "zip_meta" instance method ActionController::Base and ActionView::Base
9
+ class Railtie < Rails::Railtie
10
+ railtie_name :zip_meta
11
+
12
+ # Initialization callback. Adds the "zip_meta" method we've defined in the Util module
13
+ # to ActionController::Base and ActionView::Base, so that it can be available
14
+ # from within all controllers and views.
15
+ initializer "zip_meta.configure_rails_initialization" do
16
+ ActionController::Base.send(:include, ZipMeta::Util)
17
+ ActionView::Base.send(:include, ZipMeta::Util)
18
+ ActiveRecord::Base.send(:include, ZipMeta::Util)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,22 @@
1
+ require 'active_record'
2
+
3
+ module ZipMeta
4
+ # Contains implementation of the "zip_meta" method, which queries the
5
+ # zip_meta table for a record corresponding to the zipcode passed in.
6
+ module Util
7
+ # Implementation of the "zip_meta" method, which queries the
8
+ # zip_meta table for a record corresponding to the zipcode passed in.
9
+ def zip_meta(zipcode)
10
+ connection = ::ActiveRecord::Base.connection
11
+ result_set = connection.select_all("SELECT * FROM zip_meta WHERE zip=#{connection.quote(zipcode)}")
12
+ if result_set.count == 0
13
+ nil
14
+ else
15
+ zip_meta_item = result_set[0]
16
+ zip_meta_item.delete("id")
17
+ zip_meta_item.symbolize_keys!
18
+ zip_meta_item
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ module ZipMeta
2
+ VERSION = "0.5"
3
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zip_meta
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.5'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - nzaillian
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.1.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 3.1.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '2'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '2'
46
+ - !ruby/object:Gem::Dependency
47
+ name: sqlite3
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: A gem that uses data formerly available from CivicSpace Labs (included
63
+ with this project in CSV format in the lib/data directory) in conjunction with ActiveRecord
64
+ to let you add fast zip code meta info lookups to your Rails app.
65
+ email:
66
+ - nicholas@zaillian.com
67
+ executables: []
68
+ extensions: []
69
+ extra_rdoc_files: []
70
+ files:
71
+ - lib/core_ext/string.rb
72
+ - lib/data/zipcode.csv
73
+ - lib/generators/templates/create_zip_meta_table.rb
74
+ - lib/generators/zip_meta_migration_generator.rb
75
+ - lib/zip_meta/migration_util.rb
76
+ - lib/zip_meta/railtie.rb
77
+ - lib/zip_meta/util.rb
78
+ - lib/zip_meta/version.rb
79
+ - lib/zip_meta.rb
80
+ - Rakefile
81
+ - README.rdoc
82
+ homepage: https://github.com/nzaillian/zip_meta
83
+ licenses: []
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 1.8.24
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: Drop-in zip code meta info queries for your Rails app
106
+ test_files: []