state_geo_tools 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,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a38cb80e205d57912a0cb0f2a0154c731613f168
4
+ data.tar.gz: 9e52f549e48190731110d2658fffe25396f75149
5
+ SHA512:
6
+ metadata.gz: '00469303beafc0947f1cb362131ff8df968b0e4d617d53a655209c66567c003982fed0fe1a4450f53aee46216790ed282906ebf221908d31ad4caff0db3cc2df'
7
+ data.tar.gz: 60526aa38bbd99b4fcaa51fd89185c9f6b1b4295fbba69d7e49e9533fb13b99ddbb0d6c64c175c5d4ef50dea138921987789f589efdfa505e8b7159059f6ea2e
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /test/reports/
9
+ /tmp/
data/.rubocop.yml ADDED
@@ -0,0 +1,2 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.4.1
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.14.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in state_geo_tools.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Colin Fleming
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,101 @@
1
+ # StateGeoTools
2
+
3
+ [![Build Status](https://travis-ci.org/colinxfleming/state_geo_tools.svg?branch=master)](https://travis-ci.org/colinxfleming/state_geo_tools)
4
+
5
+ These are tools to save me from having to copy and paste the same state
6
+ constants across tools when I need to iterate thru or populate a list of
7
+ states (which is basically all the time).
8
+
9
+ TODOS:
10
+
11
+ * option rename DC to whatever bonkers things people put down in text
12
+ * count DC (and other states, I guess) when AKA strings show up
13
+
14
+ ## Installation
15
+
16
+ Add this line to your application's Gemfile:
17
+
18
+ ```rb
19
+ gem 'state_geo_tools'
20
+ ```
21
+
22
+ And then execute:
23
+
24
+ $ bundle install
25
+
26
+ Or install it yourself as:
27
+
28
+ $ gem install state_geo_tools
29
+
30
+ ## Usage
31
+
32
+ ### States and territories
33
+
34
+ This provides access to an array of states (and Washington, DC, which
35
+ is not a state because of (longstanding injustice by the U.S. Congress)[https://statehood.dc.gov/])
36
+ and territories, as strings.
37
+
38
+ ```rb
39
+ > require 'state_geo_tools'
40
+ > StateGeoTools.states
41
+ => ["Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "District of Columbia", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming"]
42
+ > StateGeoTools.territories
43
+ => ["American Samoa", "Federated States of Micronesia", "Guam", "Marshall Islands", "Northern Mariana Islands", "Palau", "Puerto Rico", "Virgin Islands"]
44
+ ```
45
+
46
+ ### State codes and territory codes
47
+
48
+ Prefer a two letter code instead of the full string? You're covered:
49
+
50
+ ```rb
51
+ > require 'state_geo_tools'
52
+ > StateGeoTools.state_codes
53
+ => ["AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "DC", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY"]
54
+ > StateGeoTools.territory_codes
55
+ => ["AS", "FM", "GU", "MH", "MP", "PW", "PR", "VI"]
56
+ ```
57
+
58
+ ### Count instances of states in a string
59
+
60
+ There is a convenience method provided to scan a string for state or territory
61
+ names and return a count.
62
+
63
+ Coming eventually: Handling all the aliases for Washington, DC.
64
+
65
+ ```rb
66
+ > require 'state_geo_tools'
67
+ > StateGeoTools.count_states_in('Pantera were from Texas, I saw them in Indiana')
68
+ => {"Texas"=>1, "Indiana"=>1}
69
+
70
+ > StateGeoTools.count_territories_in('The Simpsons are going to Guam!')
71
+ => {"Guam"=>1}
72
+
73
+ # There's some special handling for Washington/Washington DC and Virginia/West
74
+ # Virginia. It doesn't work flawlessly yet, so heads up!
75
+ > StateGeoTools.count_states_in('Virginia and West Virginia are for lovers')
76
+ => {"West Virginia"=>1, "Virginia"=>1}
77
+
78
+ > StateGeoTools.count_states_in('Washington and Washington, DC are on opposite sides of the country')
79
+ => {"Washington"=>1}
80
+ ```
81
+
82
+ ## Development
83
+
84
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run
85
+ `rake test` to run the tests. You can also run `bin/console` for an interactive
86
+ prompt that will allow you to experiment.
87
+
88
+ To install this gem onto your local machine, run `bundle exec rake install`. To
89
+ release a new version, update the version number in `version.rb`, and then run
90
+ `bundle exec rake release`, which will create a git tag for the version, push
91
+ git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
92
+
93
+ ## Contributing
94
+
95
+ Bug reports and pull requests are welcome on GitHub at
96
+ https://github.com/colinxfleming/state_geo_tools.
97
+
98
+ ## License
99
+
100
+ The gem is available as open source under the terms of the
101
+ [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'test'
6
+ t.libs << 'lib'
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'state_geo_tools'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module StateGeoTools
4
+ # Methods for counting instances in a string.
5
+ module Counters
6
+ # Count appearances of const's elements in the string.
7
+ #
8
+ # Const can be any array of strings, but we generally use the list of
9
+ # states and territories.
10
+ #
11
+ # Returns a single hash with the const element as the keys. No zeroes.
12
+ def count_instances(string, const)
13
+ instances = const.sort.map do |geo|
14
+ { geo.to_s => string.scan(geo_regex(geo)).count }
15
+ end
16
+ instances.reduce { |x, y| x.merge(y) }
17
+ .delete_if { |_, v| v.zero? }
18
+ end
19
+
20
+ private
21
+
22
+ def geo_regex(geo)
23
+ # Need special handling for the great state of Virginia,
24
+ # because of West Virginia.
25
+ return /(?<!west )virginia/i if geo == 'Virginia'
26
+ return /washington(?!, D\.*C\.*)/i if geo == 'Washington'
27
+ /#{geo}/i
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ module StateGeoTools
4
+ # USA state constants (and DC).
5
+ module States
6
+ # An array of hashes of all state data that we map out from.
7
+ STATE_DATA = [
8
+ { name: 'Alabama', code: 'AL' },
9
+ { name: 'Alaska', code: 'AK' },
10
+ { name: 'Arizona', code: 'AZ' },
11
+ { name: 'Arkansas', code: 'AR' },
12
+ { name: 'California', code: 'CA' },
13
+ { name: 'Colorado', code: 'CO' },
14
+ { name: 'Connecticut', code: 'CT' },
15
+ { name: 'Delaware', code: 'DE' },
16
+ # Statehood for DC: An opinion I am expressing in code by including DC
17
+ { name: 'District of Columbia', code: 'DC', also_known_as: ['Washington, DC', 'Washington DC'] },
18
+ { name: 'Florida', code: 'FL' },
19
+ { name: 'Georgia', code: 'GA' },
20
+ { name: 'Hawaii', code: 'HI' },
21
+ { name: 'Idaho', code: 'ID' },
22
+ { name: 'Illinois', code: 'IL' },
23
+ { name: 'Indiana', code: 'IN' },
24
+ { name: 'Iowa', code: 'IA' },
25
+ { name: 'Kansas', code: 'KS' },
26
+ { name: 'Kentucky', code: 'KY' },
27
+ { name: 'Louisiana', code: 'LA' },
28
+ { name: 'Maine', code: 'ME' },
29
+ { name: 'Maryland', code: 'MD' },
30
+ { name: 'Massachusetts', code: 'MA' },
31
+ { name: 'Michigan', code: 'MI' },
32
+ { name: 'Minnesota', code: 'MN' },
33
+ { name: 'Mississippi', code: 'MS' },
34
+ { name: 'Missouri', code: 'MO' },
35
+ { name: 'Montana', code: 'MT' },
36
+ { name: 'Nebraska', code: 'NE' },
37
+ { name: 'Nevada', code: 'NV' },
38
+ { name: 'New Hampshire', code: 'NH' },
39
+ { name: 'New Jersey', code: 'NJ' },
40
+ { name: 'New Mexico', code: 'NM' },
41
+ { name: 'New York', code: 'NY' },
42
+ { name: 'North Carolina', code: 'NC' },
43
+ { name: 'North Dakota', code: 'ND' },
44
+ { name: 'Ohio', code: 'OH' },
45
+ { name: 'Oklahoma', code: 'OK' },
46
+ { name: 'Oregon', code: 'OR' },
47
+ { name: 'Pennsylvania', code: 'PA' },
48
+ { name: 'Rhode Island', code: 'RI' },
49
+ { name: 'South Carolina', code: 'SC' },
50
+ { name: 'South Dakota', code: 'SD' },
51
+ { name: 'Tennessee', code: 'TN' },
52
+ { name: 'Texas', code: 'TX' },
53
+ { name: 'Utah', code: 'UT' },
54
+ { name: 'Vermont', code: 'VT' },
55
+ { name: 'Virginia', code: 'VA' },
56
+ { name: 'Washington', code: 'WA' },
57
+ { name: 'West Virginia', code: 'WV' },
58
+ { name: 'Wisconsin', code: 'WI' },
59
+ { name: 'Wyoming', code: 'WY' }
60
+ ].freeze
61
+
62
+ # An array of all state names (and DC).
63
+ STATES = STATE_DATA.map { |state| state[:name] }.freeze
64
+
65
+ # An array of all two-letter state codes (and DC).
66
+ STATE_CODES = STATE_DATA.map { |state| state[:code] }.freeze
67
+ end
68
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module StateGeoTools
4
+ # Constants for US territories
5
+ module Territories
6
+ # An array of hashes of all territories that we map out from.
7
+ TERRITORY_DATA = [
8
+ { name: 'American Samoa', code: 'AS' },
9
+ { name: 'Federated States of Micronesia', code: 'FM' },
10
+ { name: 'Guam', code: 'GU' },
11
+ { name: 'Marshall Islands', code: 'MH' },
12
+ { name: 'Northern Mariana Islands', code: 'MP' },
13
+ { name: 'Palau', code: 'PW' },
14
+ { name: 'Puerto Rico', code: 'PR' },
15
+ { name: 'Virgin Islands', code: 'VI' }
16
+ ].freeze
17
+
18
+ # An array of all territory names.
19
+ TERRITORIES = TERRITORY_DATA.map { |terr| terr[:name] }.freeze
20
+
21
+ # An array of all two-letter territory codes.
22
+ TERRITORY_CODES = TERRITORY_DATA.map { |terr| terr[:code] }.freeze
23
+ end
24
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module StateGeoTools
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'state_geo_tools/version'
4
+ require 'state_geo_tools/state_constants'
5
+ require 'state_geo_tools/territory_constants'
6
+ require 'state_geo_tools/counters'
7
+
8
+ # Because I got sick of copying and pasting arrays of states everywhere, this
9
+ # utility provides some convenience methods to get a full set of US states and
10
+ # territories. It also includes a pair of convenience methods.
11
+ #
12
+ # The utility modules are all public, in the event that one should need to
13
+ # use them in a slightly different way, but the common use case is in this
14
+ #
15
+ # See the README for examples.
16
+ module StateGeoTools
17
+ extend StateGeoTools::Counters
18
+
19
+ # Return an array of states and the District of Columbia.
20
+ def self.states
21
+ States::STATES
22
+ end
23
+
24
+ # Return an array of two-letter state codes, and DC.
25
+ def self.state_codes
26
+ States::STATE_CODES
27
+ end
28
+
29
+ # Return an array of US territories.
30
+ def self.territories
31
+ Territories::TERRITORIES
32
+ end
33
+
34
+ # Return an array of two-letter US territory codes.
35
+ def self.territory_codes
36
+ Territories::TERRITORY_CODES
37
+ end
38
+
39
+ # Count instances of states in a string.
40
+ def self.count_states_in(string)
41
+ count_instances string, States::STATES
42
+ end
43
+
44
+ # Count instances of territories passed in a string.
45
+ def self.count_territories_in(string)
46
+ count_instances string, Territories::TERRITORIES
47
+ end
48
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'state_geo_tools/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'state_geo_tools'
9
+ spec.version = StateGeoTools::VERSION
10
+ spec.authors = ['colinxfleming']
11
+ spec.email = ['c3flemin@gmail.com']
12
+
13
+ spec.summary = 'A US geography/state utility.'
14
+ spec.description = 'I got sick of copying the same US states constant.'
15
+ spec.homepage = 'https://github.com/colinxfleming/state_geo_tools'
16
+ spec.license = 'MIT'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.14'
26
+ spec.add_development_dependency 'minitest', '~> 5.0'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: state_geo_tools
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - colinxfleming
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-05-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: I got sick of copying the same US states constant.
56
+ email:
57
+ - c3flemin@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rubocop.yml"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - lib/state_geo_tools.rb
72
+ - lib/state_geo_tools/counters.rb
73
+ - lib/state_geo_tools/state_constants.rb
74
+ - lib/state_geo_tools/territory_constants.rb
75
+ - lib/state_geo_tools/version.rb
76
+ - state_geo_tools.gemspec
77
+ homepage: https://github.com/colinxfleming/state_geo_tools
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.6.14
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: A US geography/state utility.
101
+ test_files: []