subdivision_responder 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MDRkM2RiY2Q2YWJkZjhhOTZkYTk1OGI5ZWQ5ODlmZjBjOTZmZGYzMQ==
5
+ data.tar.gz: !binary |-
6
+ ZDc1M2M0OTgyNDM0OWU3NGEwODAyYzE3MGEyMGI4MTAyNTY1MzZlMA==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ MDI2ZGI4ZDYwYzRjMmQ3NGY1MGMyNDJmOGM1NGRmNWVkZTIyMWRiMzY2N2Qw
10
+ MjVmMzY4ZDkyMjkxZmY2NTI2N2VlZDdmM2Y5MDU1OTc3ODdkYjc4M2M2NWUx
11
+ MjgyZThmMDNkNmZlMTI4ZjE0OGZmMzBlZThlM2FjMzY1OTVjY2E=
12
+ data.tar.gz: !binary |-
13
+ Mzc5ZjQ5N2I4OTIxMTkxODY0YjM0MWFlYmVjZDdhZjQzNzQ4ZjIwNmRmNDQ3
14
+ OWFkMjM5MmNmYzAwNThmM2Q3OGMzODQwNjQwNDY0MjAxMTRhYzNhMGQ1ZmUx
15
+ NTMzYTIwM2FiMWE5NWQ2YzBlOWE3Njk5OTE5MWNiMzBjMjhmMWM=
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem 'rack'
4
+ gem 'json'
5
+ gem 'countries'
6
+
7
+ group :development do
8
+ gem "jeweler"
9
+ end
10
+
11
+ group :development, :test do
12
+ gem 'rspec'
13
+ gem 'rack-test'
14
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,70 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ addressable (2.3.5)
5
+ builder (3.2.2)
6
+ countries (0.9.2)
7
+ currencies (>= 0.4.0)
8
+ currencies (0.4.2)
9
+ diff-lcs (1.2.4)
10
+ faraday (0.8.7)
11
+ multipart-post (~> 1.1)
12
+ git (1.2.5)
13
+ github_api (0.10.1)
14
+ addressable
15
+ faraday (~> 0.8.1)
16
+ hashie (>= 1.2)
17
+ multi_json (~> 1.4)
18
+ nokogiri (~> 1.5.2)
19
+ oauth2
20
+ hashie (2.0.5)
21
+ highline (1.6.19)
22
+ httpauth (0.2.0)
23
+ jeweler (1.8.6)
24
+ builder
25
+ bundler (~> 1.0)
26
+ git (>= 1.2.5)
27
+ github_api (= 0.10.1)
28
+ highline (>= 1.6.15)
29
+ nokogiri (= 1.5.10)
30
+ rake
31
+ rdoc
32
+ json (1.8.0)
33
+ jwt (0.1.8)
34
+ multi_json (>= 1.5)
35
+ multi_json (1.7.7)
36
+ multi_xml (0.5.4)
37
+ multipart-post (1.2.0)
38
+ nokogiri (1.5.10)
39
+ oauth2 (0.9.2)
40
+ faraday (~> 0.8)
41
+ httpauth (~> 0.2)
42
+ jwt (~> 0.1.4)
43
+ multi_json (~> 1.0)
44
+ multi_xml (~> 0.5)
45
+ rack (~> 1.2)
46
+ rack (1.5.2)
47
+ rack-test (0.6.2)
48
+ rack (>= 1.0)
49
+ rake (10.1.0)
50
+ rdoc (4.0.1)
51
+ json (~> 1.4)
52
+ rspec (2.13.0)
53
+ rspec-core (~> 2.13.0)
54
+ rspec-expectations (~> 2.13.0)
55
+ rspec-mocks (~> 2.13.0)
56
+ rspec-core (2.13.1)
57
+ rspec-expectations (2.13.0)
58
+ diff-lcs (>= 1.1.3, < 2.0)
59
+ rspec-mocks (2.13.1)
60
+
61
+ PLATFORMS
62
+ ruby
63
+
64
+ DEPENDENCIES
65
+ countries
66
+ jeweler
67
+ json
68
+ rack
69
+ rack-test
70
+ rspec
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Michael Klett / Chargify, LLC
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ Subdivision Responder
2
+ =====================
3
+
4
+ A simple Rack app that responds with subdivisions (states and provinces)
5
+ when queried with an alpha2 country code.
6
+
7
+ ```
8
+ % curl http://example.com/subdivisions\?country_code\=US
9
+ {"US":[["Alaska","AK"],["Alabama","AL"],...]}
10
+ ```
11
+
12
+ Installation for use in a Rails app
13
+ -----------------------------------
14
+
15
+ In your `Gemfile`:
16
+
17
+ ```
18
+ gem 'subdivision_responder'
19
+ ```
20
+
21
+ In your `config/routes.rb`:
22
+
23
+ ```
24
+ mount SubdivisionResponder::RackApp.new => '/subdivisions'
25
+ ```
26
+
27
+ Data
28
+ ----
29
+
30
+ All country and subdivision data is sourced from the [countries
31
+ gem](https://github.com/hexorx/countries)
32
+
33
+
34
+ Copyright
35
+ ---------
36
+
37
+ MIT Licensed
38
+
39
+ Copyright (c) 2013 Michael Klett / Chargify
40
+
41
+ See LICENSE.txt for further details.
42
+
data/Rakefile ADDED
@@ -0,0 +1,39 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "subdivision_responder"
18
+ gem.homepage = "https://github.com/chargify/subdivision_responder"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{A Rack app that responds to queries containing a country code with appropriate subdivision data as JSON.}
21
+ gem.description = %Q{A Rack app that responds to queries containing a country code with appropriate subdivision data as JSON.}
22
+ gem.email = "moklett@gmail.com"
23
+ gem.authors = ["Michael Klett"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,6 @@
1
+ require 'rack'
2
+ require 'json'
3
+ require 'countries'
4
+
5
+ require 'subdivision_responder/country_data'
6
+ require 'subdivision_responder/rack_app'
@@ -0,0 +1,16 @@
1
+ module SubdivisionResponder
2
+ module CountryData
3
+ def self.subdivisions_by_country_code
4
+ hash = {}
5
+ Country.all.each do |country_pair|
6
+ country_alpha2 = country_pair[1]
7
+ country = Country.find_country_by_alpha2(country_alpha2)
8
+ subdivision_pairs = country.subdivisions.map do |subdivision_alpha2, subdivision_data|
9
+ [ subdivision_data['name'], subdivision_alpha2 ]
10
+ end
11
+ hash[country_alpha2] = subdivision_pairs
12
+ end
13
+ hash
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,14 @@
1
+ module SubdivisionResponder
2
+ class RackApp
3
+ SUBDIVISIONS = CountryData.subdivisions_by_country_code
4
+
5
+ def call(env)
6
+ request = Rack::Request.new(env)
7
+ country_code = request.params['country_code'].to_s.upcase
8
+ data = country_code.empty? ? SUBDIVISIONS : SUBDIVISIONS.select {|k| k == country_code }
9
+ json = data.to_json
10
+
11
+ [200, {"Content-Type" => "application/json"}, [json]]
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe SubdivisionResponder::CountryData do
4
+ describe ".subdivisions_by_country_code" do
5
+ it "returns a hash with country codes as keys and arrays of subdivision name/code pairs as values" do
6
+ result = described_class.subdivisions_by_country_code
7
+ result.should be_a Hash
8
+ result.keys.should include("US")
9
+ result["US"].should include(["North Carolina", "NC"])
10
+ end
11
+ end
12
+
13
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe SubdivisionResponder::RackApp do
4
+ include Rack::Test::Methods
5
+
6
+ let(:app) { SubdivisionResponder::RackApp.new }
7
+
8
+ it "responds with a Hash with the country code as key and an array of name/code pairs as value when given a known, upcased country code" do
9
+ get "/", country_code: 'US'
10
+ expect(last_response.body).to eql({'US' => us_states_array}.to_json)
11
+ end
12
+
13
+ it "responds with a Hash with the country code as key and an array of name/code pairs as value when given a known, downcased country code" do
14
+ get "/", country_code: 'us'
15
+ expect(last_response.body).to eql({'US' => us_states_array}.to_json)
16
+ end
17
+
18
+ it "responds with an empty hash if an unknown country code is given" do
19
+ get "/", country_code: 'FOO'
20
+ expect(last_response.body).to eql({}.to_json)
21
+ end
22
+
23
+ it "response with a Hash of all country codes and subdivision arrays when no country code is given" do
24
+ get "/"
25
+ parsed_body = JSON.parse(last_response.body)
26
+
27
+ parsed_body.keys.should include("US")
28
+ parsed_body.keys.size.should be > 1
29
+ parsed_body["US"].should eql us_states_array
30
+ end
31
+
32
+ private
33
+
34
+ def us_states_array
35
+ [["Alaska","AK"],["Alabama","AL"],["Arkansas","AR"],["American Samoa","AS"],["Arizona","AZ"],["California","CA"],["Colorado","CO"],["Connecticut","CT"],["District of Columbia","DC"],["Delaware","DE"],["Florida","FL"],["Georgia","GA"],["Guam","GU"],["Hawaii","HI"],["Iowa","IA"],["Idaho","ID"],["Illinois","IL"],["Indiana","IN"],["Kansas","KS"],["Kentucky","KY"],["Louisiana","LA"],["Massachusetts","MA"],["Maryland","MD"],["Maine","ME"],["Michigan","MI"],["Minnesota","MN"],["Missouri","MO"],["Northern Mariana Islands","MP"],["Mississippi","MS"],["Montana","MT"],["North Carolina","NC"],["North Dakota","ND"],["Nebraska","NE"],["New Hampshire","NH"],["New Jersey","NJ"],["New Mexico","NM"],["Nevada","NV"],["New York","NY"],["Ohio","OH"],["Oklahoma","OK"],["Oregon","OR"],["Pennsylvania","PA"],["Puerto Rico","PR"],["Rhode Island","RI"],["South Carolina","SC"],["South Dakota","SD"],["Tennessee","TN"],["Texas","TX"],["United States Minor Outlying Islands","UM"],["Utah","UT"],["Virginia","VA"],["Virgin Islands, U.S.","VI"],["Vermont","VT"],["Washington","WA"],["Wisconsin","WI"],["West Virginia","WV"],["Wyoming","WY"]]
36
+ end
37
+ end
@@ -0,0 +1,14 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'rack/test'
5
+
6
+ require 'subdivision_responder'
7
+
8
+ # Requires supporting files with custom matchers and macros, etc,
9
+ # in ./support/ and its subdirectories.
10
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
11
+
12
+ RSpec.configure do |config|
13
+
14
+ end
@@ -0,0 +1,69 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "subdivision_responder"
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Michael Klett"]
12
+ s.date = "2013-07-25"
13
+ s.description = "A Rack app that responds to queries containing a country code with appropriate subdivision data as JSON."
14
+ s.email = "moklett@gmail.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.md",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "lib/subdivision_responder.rb",
29
+ "lib/subdivision_responder/country_data.rb",
30
+ "lib/subdivision_responder/rack_app.rb",
31
+ "spec/country_data_spec.rb",
32
+ "spec/rack_app_spec.rb",
33
+ "spec/spec_helper.rb",
34
+ "subdivision_responder.gemspec"
35
+ ]
36
+ s.homepage = "https://github.com/chargify/subdivision_responder"
37
+ s.licenses = ["MIT"]
38
+ s.require_paths = ["lib"]
39
+ s.rubygems_version = "2.0.4"
40
+ s.summary = "A Rack app that responds to queries containing a country code with appropriate subdivision data as JSON."
41
+
42
+ if s.respond_to? :specification_version then
43
+ s.specification_version = 4
44
+
45
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
46
+ s.add_runtime_dependency(%q<rack>, [">= 0"])
47
+ s.add_runtime_dependency(%q<json>, [">= 0"])
48
+ s.add_runtime_dependency(%q<countries>, [">= 0"])
49
+ s.add_development_dependency(%q<jeweler>, [">= 0"])
50
+ s.add_development_dependency(%q<rspec>, [">= 0"])
51
+ s.add_development_dependency(%q<rack-test>, [">= 0"])
52
+ else
53
+ s.add_dependency(%q<rack>, [">= 0"])
54
+ s.add_dependency(%q<json>, [">= 0"])
55
+ s.add_dependency(%q<countries>, [">= 0"])
56
+ s.add_dependency(%q<jeweler>, [">= 0"])
57
+ s.add_dependency(%q<rspec>, [">= 0"])
58
+ s.add_dependency(%q<rack-test>, [">= 0"])
59
+ end
60
+ else
61
+ s.add_dependency(%q<rack>, [">= 0"])
62
+ s.add_dependency(%q<json>, [">= 0"])
63
+ s.add_dependency(%q<countries>, [">= 0"])
64
+ s.add_dependency(%q<jeweler>, [">= 0"])
65
+ s.add_dependency(%q<rspec>, [">= 0"])
66
+ s.add_dependency(%q<rack-test>, [">= 0"])
67
+ end
68
+ end
69
+
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: subdivision_responder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Michael Klett
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: countries
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: jeweler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rack-test
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: A Rack app that responds to queries containing a country code with appropriate
98
+ subdivision data as JSON.
99
+ email: moklett@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files:
103
+ - LICENSE.txt
104
+ - README.md
105
+ files:
106
+ - .document
107
+ - .rspec
108
+ - Gemfile
109
+ - Gemfile.lock
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - VERSION
114
+ - lib/subdivision_responder.rb
115
+ - lib/subdivision_responder/country_data.rb
116
+ - lib/subdivision_responder/rack_app.rb
117
+ - spec/country_data_spec.rb
118
+ - spec/rack_app_spec.rb
119
+ - spec/spec_helper.rb
120
+ - subdivision_responder.gemspec
121
+ homepage: https://github.com/chargify/subdivision_responder
122
+ licenses:
123
+ - MIT
124
+ metadata: {}
125
+ post_install_message:
126
+ rdoc_options: []
127
+ require_paths:
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ! '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ requirements: []
140
+ rubyforge_project:
141
+ rubygems_version: 2.0.4
142
+ signing_key:
143
+ specification_version: 4
144
+ summary: A Rack app that responds to queries containing a country code with appropriate
145
+ subdivision data as JSON.
146
+ test_files: []