us_geo 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +5 -0
  3. data/Gemfile.lock +75 -0
  4. data/README.md +154 -0
  5. data/Rakefile +18 -0
  6. data/db/migrate/20190221054200_create_regions.rb +16 -0
  7. data/db/migrate/20190221054300_create_divisions.rb +17 -0
  8. data/db/migrate/20190221054400_create_states.rb +20 -0
  9. data/db/migrate/20190221054490_create_designated_market_areas.rb +16 -0
  10. data/db/migrate/20190221054500_create_combined_statistical_areas.rb +20 -0
  11. data/db/migrate/20190221054600_create_core_based_statistical_areas.rb +24 -0
  12. data/db/migrate/20190221054650_create_metropolitan_divisions.rb +21 -0
  13. data/db/migrate/20190221054700_create_counties.rb +34 -0
  14. data/db/migrate/20190221054800_create_zctas.rb +23 -0
  15. data/db/migrate/20190221054900_create_zcta_counties.rb +22 -0
  16. data/db/migrate/20190221055000_create_urban_areas.rb +25 -0
  17. data/db/migrate/20190221055100_create_urban_area_counties.rb +22 -0
  18. data/db/migrate/20190221055200_create_zcta_urban_areas.rb +22 -0
  19. data/db/migrate/20190221060000_create_places.rb +28 -0
  20. data/db/migrate/20190221061000_create_place_counties.rb +18 -0
  21. data/db/migrate/20190221062000_create_zcta_places.rb +22 -0
  22. data/db/migrate/20190221063000_create_county_subdivisions.rb +25 -0
  23. data/lib/tasks/us_geo/us_geo.rake +43 -0
  24. data/lib/us_geo/base_record.rb +104 -0
  25. data/lib/us_geo/combined_statistical_area.rb +40 -0
  26. data/lib/us_geo/core_based_statistical_area.rb +57 -0
  27. data/lib/us_geo/county.rb +89 -0
  28. data/lib/us_geo/county_subdivision.rb +46 -0
  29. data/lib/us_geo/demographics.rb +25 -0
  30. data/lib/us_geo/designated_market_area.rb +30 -0
  31. data/lib/us_geo/division.rb +29 -0
  32. data/lib/us_geo/engine.rb +6 -0
  33. data/lib/us_geo/metropolitan_area.rb +18 -0
  34. data/lib/us_geo/metropolitan_division.rb +42 -0
  35. data/lib/us_geo/micropolitan_area.rb +18 -0
  36. data/lib/us_geo/place.rb +61 -0
  37. data/lib/us_geo/place_county.rb +28 -0
  38. data/lib/us_geo/region.rb +28 -0
  39. data/lib/us_geo/state.rb +56 -0
  40. data/lib/us_geo/urban_area.rb +66 -0
  41. data/lib/us_geo/urban_area_county.rb +68 -0
  42. data/lib/us_geo/urban_cluster.rb +18 -0
  43. data/lib/us_geo/urbanized_area.rb +18 -0
  44. data/lib/us_geo/version.rb +5 -0
  45. data/lib/us_geo/zcta.rb +63 -0
  46. data/lib/us_geo/zcta_county.rb +68 -0
  47. data/lib/us_geo/zcta_place.rb +68 -0
  48. data/lib/us_geo/zcta_urban_area.rb +68 -0
  49. data/lib/us_geo.rb +53 -0
  50. data/spec/spec_helper.rb +22 -0
  51. data/spec/us_geo/base_record_spec.rb +67 -0
  52. data/spec/us_geo/combined_statistical_area_spec.rb +33 -0
  53. data/spec/us_geo/core_based_statistical_area_spec.rb +56 -0
  54. data/spec/us_geo/county_spec.rb +130 -0
  55. data/spec/us_geo/county_subdivision_spec.rb +37 -0
  56. data/spec/us_geo/demographics_spec.rb +19 -0
  57. data/spec/us_geo/designated_market_area_spec.rb +29 -0
  58. data/spec/us_geo/division_spec.rb +37 -0
  59. data/spec/us_geo/metropolitan_division_spec.rb +41 -0
  60. data/spec/us_geo/place_county_spec.rb +39 -0
  61. data/spec/us_geo/place_spec.rb +71 -0
  62. data/spec/us_geo/region_spec.rb +36 -0
  63. data/spec/us_geo/state_spec.rb +70 -0
  64. data/spec/us_geo/urban_area_county_spec.rb +82 -0
  65. data/spec/us_geo/urban_area_spec.rb +98 -0
  66. data/spec/us_geo/zcta_county_spec.rb +82 -0
  67. data/spec/us_geo/zcta_place_spec.rb +82 -0
  68. data/spec/us_geo/zcta_spec.rb +99 -0
  69. data/spec/us_geo/zcta_urban_area_spec.rb +82 -0
  70. metadata +229 -0
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+ describe USGeo::CoreBasedStatisticalArea do
4
+
5
+ describe "associations" do
6
+ it "should have a combined statistical area" do
7
+ core_based_statistical_area = USGeo::CoreBasedStatisticalArea.new
8
+ core_based_statistical_area.geoid = "00001"
9
+ expect{ core_based_statistical_area.combined_statistical_area }.to_not raise_error
10
+ expect(core_based_statistical_area.build_combined_statistical_area).to be_a(USGeo::CombinedStatisticalArea)
11
+ end
12
+
13
+ it "should have counties" do
14
+ core_based_statistical_area = USGeo::CoreBasedStatisticalArea.new
15
+ core_based_statistical_area.geoid = "00001"
16
+ expect{ core_based_statistical_area.counties }.to_not raise_error
17
+ expect(core_based_statistical_area.counties.build).to be_a(USGeo::County)
18
+ end
19
+
20
+ it "should have metropolitan divisions" do
21
+ core_based_statistical_area = USGeo::CoreBasedStatisticalArea.new
22
+ core_based_statistical_area.geoid = "00001"
23
+ expect{ core_based_statistical_area.metropolitan_divisions }.to_not raise_error
24
+ expect(core_based_statistical_area.metropolitan_divisions.build).to be_a(USGeo::MetropolitanDivision)
25
+ end
26
+ end
27
+
28
+ describe "load" do
29
+ after { USGeo::CoreBasedStatisticalArea.delete_all }
30
+
31
+ it "should load the fixture data" do
32
+ data = File.read(File.expand_path("../../data/dist/core_based_statistical_areas.csv", __dir__))
33
+ stub_request(:get, "#{USGeo.base_data_uri}/core_based_statistical_areas.csv").to_return(body: data, headers: {"Content-Type": "text/csv; charset=UTF-8"})
34
+ USGeo::CoreBasedStatisticalArea.load!
35
+ expect(USGeo::CoreBasedStatisticalArea.imported.count).to be > 900
36
+ expect(USGeo::CoreBasedStatisticalArea.removed.count).to eq 0
37
+
38
+ chicagoarea = USGeo::CoreBasedStatisticalArea.find("16980")
39
+ expect(chicagoarea).to be_a(USGeo::MetropolitanArea)
40
+ expect(chicagoarea.name).to eq "Chicago-Naperville-Elgin, IL-IN-WI"
41
+ expect(chicagoarea.csa_geoid).to eq "176"
42
+ expect(chicagoarea.population).to be > 8_000_000
43
+ expect(chicagoarea.housing_units).to be > 3_000_000
44
+ expect(chicagoarea.land_area.round).to eq 7197
45
+ expect(chicagoarea.water_area.round).to eq 2382
46
+ expect(chicagoarea.lat.round).to eq 42
47
+ expect(chicagoarea.lng.round).to eq -88
48
+
49
+ centralia = USGeo::CoreBasedStatisticalArea.find("16460")
50
+ expect(centralia.population).to be < 50_000
51
+ expect(centralia).to be_a(USGeo::MicropolitanArea)
52
+ expect(centralia.name).to eq "Centralia, IL"
53
+ end
54
+ end
55
+
56
+ end
@@ -0,0 +1,130 @@
1
+ require 'spec_helper'
2
+
3
+ describe USGeo::County do
4
+
5
+ describe "associations" do
6
+ it "should have a state" do
7
+ county = USGeo::County.new
8
+ county.geoid = "00001"
9
+ expect{ county.state }.to_not raise_error
10
+ expect(county.build_state).to be_a(USGeo::State)
11
+ end
12
+
13
+ it "should have a core_based_statistical_area" do
14
+ county = USGeo::County.new
15
+ county.geoid = "00001"
16
+ expect{ county.core_based_statistical_area }.to_not raise_error
17
+ expect(county.build_core_based_statistical_area).to be_a(USGeo::CoreBasedStatisticalArea)
18
+ end
19
+
20
+ it "should have a metropolitan area if the core_based_statistical_area is metropolitan" do
21
+ county = USGeo::County.new
22
+ expect(county.metropolitan_area).to eq nil
23
+ county.core_based_statistical_area = USGeo::MicropolitanArea.new
24
+ expect(county.metropolitan_area).to eq nil
25
+ metro_area = USGeo::MetropolitanArea.new
26
+ county.core_based_statistical_area = metro_area
27
+ expect(county.metropolitan_area).to eq metro_area
28
+ end
29
+
30
+ it "should have a metropolitan division" do
31
+ county = USGeo::County.new
32
+ county.geoid = "00001"
33
+ expect{ county.metropolitan_division }.to_not raise_error
34
+ expect(county.build_metropolitan_division).to be_a(USGeo::MetropolitanDivision)
35
+ end
36
+
37
+ it "should have zctas" do
38
+ county = USGeo::County.new
39
+ county.geoid = "00001"
40
+ expect{ county.zctas }.to_not raise_error
41
+ expect{ county.zcta_counties }.to_not raise_error
42
+ expect(county.zcta_counties.build).to be_a(USGeo::ZctaCounty)
43
+ end
44
+
45
+ it "should have urban areas" do
46
+ county = USGeo::County.new
47
+ county.geoid = "00001"
48
+ expect{ county.urban_areas }.to_not raise_error
49
+ expect{ county.urban_area_counties }.to_not raise_error
50
+ expect(county.urban_area_counties.build).to be_a(USGeo::UrbanAreaCounty)
51
+ end
52
+
53
+ it "should have places" do
54
+ county = USGeo::County.new
55
+ county.geoid = "00001"
56
+ expect{ county.places }.to_not raise_error
57
+ expect{ county.place_counties }.to_not raise_error
58
+ expect(county.place_counties.build).to be_a(USGeo::PlaceCounty)
59
+ end
60
+
61
+ it "should have subdivisions" do
62
+ county = USGeo::County.new
63
+ county.geoid = "00001"
64
+ expect{ county.subdivisions }.to_not raise_error
65
+ expect(county.subdivisions.build).to be_a(USGeo::CountySubdivision)
66
+ end
67
+
68
+ it "should have a designated market area" do
69
+ county = USGeo::County.new
70
+ county.geoid = "00001"
71
+ expect{ county.designated_market_area }.to_not raise_error
72
+ expect(county.build_designated_market_area).to be_a(USGeo::DesignatedMarketArea)
73
+ end
74
+ end
75
+
76
+ describe "load" do
77
+ after { USGeo::County.delete_all }
78
+
79
+ it "should load the fixture data" do
80
+ data = File.read(File.expand_path("../../data/dist/counties.csv", __dir__))
81
+ stub_request(:get, "#{USGeo.base_data_uri}/counties.csv").to_return(body: data, headers: {"Content-Type": "text/csv; charset=UTF-8"})
82
+ USGeo::County.load!
83
+ expect(USGeo::County.imported.count).to be > 3000
84
+ expect(USGeo::County.removed.count).to eq 0
85
+
86
+ cook = USGeo::County.find("17031")
87
+ expect(cook.name).to eq "Cook County"
88
+ expect(cook.short_name).to eq "Cook"
89
+ expect(cook.dma_code).to eq "602"
90
+ expect(cook.cbsa_geoid).to eq "16980"
91
+ expect(cook.state_code).to eq "IL"
92
+ expect(cook.state_fips).to eq "17"
93
+ expect(cook.county_fips).to eq "031"
94
+ expect(cook.fips_class_code).to eq "H1"
95
+ expect(cook.time_zone_name).to eq "America/Chicago"
96
+ expect(cook.central?).to eq true
97
+ expect(cook.population).to be > 5_000_000
98
+ expect(cook.housing_units).to be > 2_000_000
99
+ expect(cook.land_area.round).to eq 945
100
+ expect(cook.water_area.round).to eq 690
101
+ expect(cook.lat.round).to eq 42
102
+ expect(cook.lng.round).to eq -88
103
+
104
+ clinton = USGeo::County.find("17027")
105
+ expect(clinton.name).to eq "Clinton County"
106
+ expect(clinton.central?).to eq false
107
+ end
108
+ end
109
+
110
+ describe "attributes" do
111
+ it "should have a state FIPS code" do
112
+ county = USGeo::County.new
113
+ county.geoid = "17031"
114
+ expect(county.state_fips).to eq "17"
115
+ end
116
+
117
+ it "should have a county FIPS code" do
118
+ county = USGeo::County.new
119
+ county.geoid = "17031"
120
+ expect(county.county_fips).to eq "031"
121
+ end
122
+
123
+ it "should have a time zone" do
124
+ county = USGeo::County.new(time_zone_name: "America/Chicago")
125
+ expect(county.time_zone).to be_a(ActiveSupport::TimeZone)
126
+ expect(county.time_zone.name).to eq "America/Chicago"
127
+ end
128
+ end
129
+
130
+ end
@@ -0,0 +1,37 @@
1
+ require "spec_helper"
2
+
3
+ describe USGeo::CountySubdivision do
4
+
5
+ describe "associations" do
6
+ it "should have a county" do
7
+ subdivision = USGeo::CountySubdivision.new
8
+ subdivision.geoid = "0000000001"
9
+ expect{ subdivision.county }.to_not raise_error
10
+ expect(subdivision.build_county).to be_a(USGeo::County)
11
+ end
12
+ end
13
+
14
+ describe "load" do
15
+ after { USGeo::CountySubdivision.delete_all }
16
+
17
+ it "should load the fixture data" do
18
+ data = File.read(File.expand_path("../../data/dist/county_subdivisions.csv", __dir__))
19
+ stub_request(:get, "#{USGeo.base_data_uri}/county_subdivisions.csv").to_return(body: data, headers: {"Content-Type": "text/csv; charset=UTF-8"})
20
+ USGeo::CountySubdivision.load!
21
+ expect(USGeo::CountySubdivision.imported.count).to be > 35_000
22
+ expect(USGeo::CountySubdivision.removed.count).to eq 0
23
+
24
+ subdivision = USGeo::CountySubdivision.find("2600545180")
25
+ expect(subdivision.name).to eq "Township of Laketown"
26
+ expect(subdivision.county_geoid).to eq "26005"
27
+ expect(subdivision.population).to be > 5000
28
+ expect(subdivision.housing_units).to be > 2000
29
+ expect(subdivision.fips_class_code).to eq "T1"
30
+ expect(subdivision.land_area.round).to eq 22
31
+ expect(subdivision.water_area.round(1)).to eq 0.1
32
+ expect(subdivision.lat.round).to eq 43
33
+ expect(subdivision.lng.round).to eq -86
34
+ end
35
+ end
36
+
37
+ end
@@ -0,0 +1,19 @@
1
+ require "spec_helper"
2
+
3
+ describe USGeo::Demographics do
4
+
5
+ let(:record) { USGeo::County.new(population: 90_000, land_area: 45.0, water_area: 5.0) }
6
+
7
+ it "should return the population density of the land area" do
8
+ expect(record.population_density).to eq 2000.0
9
+ end
10
+
11
+ it "should return the total area" do
12
+ expect(record.total_area).to eq 50.0
13
+ end
14
+
15
+ it "should return the percent of land to water" do
16
+ expect(record.percent_land).to eq 0.9
17
+ end
18
+
19
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe USGeo::DesignatedMarketArea do
4
+
5
+ describe "associations" do
6
+ it "should have counties" do
7
+ division = USGeo::DesignatedMarketArea.new
8
+ division.id = 1
9
+ expect{ division.counties }.to_not raise_error
10
+ expect(division.counties.build).to be_a(USGeo::County)
11
+ end
12
+ end
13
+
14
+ describe "load" do
15
+ after { USGeo::DesignatedMarketArea.delete_all }
16
+
17
+ it "should load the fixture data" do
18
+ data = File.read(File.expand_path("../../data/dist/dmas.csv", __dir__))
19
+ stub_request(:get, "#{USGeo.base_data_uri}/dmas.csv").to_return(body: data, headers: {"Content-Type": "text/csv; charset=UTF-8"})
20
+ USGeo::DesignatedMarketArea.load!
21
+ expect(USGeo::DesignatedMarketArea.imported.count).to be > 200
22
+ expect(USGeo::DesignatedMarketArea.removed.count).to eq 0
23
+
24
+ dma = USGeo::DesignatedMarketArea.find("602")
25
+ expect(dma.name).to eq "Chicago, IL"
26
+ end
27
+ end
28
+
29
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe USGeo::Division do
4
+
5
+ describe "associations" do
6
+ it "should have a region" do
7
+ division = USGeo::Division.new
8
+ division.id = 1
9
+ expect{ division.region }.to_not raise_error
10
+ expect(division.build_region).to be_a(USGeo::Region)
11
+ end
12
+
13
+ it "should have states" do
14
+ division = USGeo::Division.new
15
+ division.id = 1
16
+ expect{ division.states }.to_not raise_error
17
+ expect(division.states.build).to be_a(USGeo::State)
18
+ end
19
+ end
20
+
21
+ describe "load" do
22
+ after { USGeo::Division.delete_all }
23
+
24
+ it "should load the fixture data" do
25
+ data = File.read(File.expand_path("../../data/dist/divisions.csv", __dir__))
26
+ stub_request(:get, "#{USGeo.base_data_uri}/divisions.csv").to_return(body: data, headers: {"Content-Type": "text/csv; charset=UTF-8"})
27
+ USGeo::Division.load!
28
+ expect(USGeo::Division.imported.count).to eq 9
29
+ expect(USGeo::Division.removed.count).to eq 0
30
+
31
+ division = USGeo::Division.find(2)
32
+ expect(division.name).to eq "Middle Atlantic"
33
+ expect(division.region_id).to eq 1
34
+ end
35
+ end
36
+
37
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe USGeo::MetropolitanDivision do
4
+
5
+ describe "associations" do
6
+ it "should have a core_based_statistical_area" do
7
+ division = USGeo::MetropolitanDivision.new
8
+ division.geoid = "00001"
9
+ expect{ division.core_based_statistical_area }.to_not raise_error
10
+ expect(division.build_core_based_statistical_area).to be_a(USGeo::CoreBasedStatisticalArea)
11
+ end
12
+
13
+ it "should have counties" do
14
+ division = USGeo::MetropolitanDivision.new
15
+ division.geoid = "00001"
16
+ expect{ division.counties }.to_not raise_error
17
+ expect(division.counties.build).to be_a(USGeo::County)
18
+ end
19
+ end
20
+
21
+ describe "load" do
22
+ after { USGeo::MetropolitanDivision.delete_all }
23
+
24
+ it "should load the fixture data" do
25
+ data = File.read(File.expand_path("../../data/dist/metropolitan_divisions.csv", __dir__))
26
+ stub_request(:get, "#{USGeo.base_data_uri}/metropolitan_divisions.csv").to_return(body: data, headers: {"Content-Type": "text/csv; charset=UTF-8"})
27
+ USGeo::MetropolitanDivision.load!
28
+ expect(USGeo::MetropolitanDivision.imported.count).to be > 25
29
+ expect(USGeo::MetropolitanDivision.removed.count).to eq 0
30
+
31
+ division = USGeo::MetropolitanDivision.find("16984")
32
+ expect(division.name).to eq "Chicago-Naperville-Evanston, IL"
33
+ expect(division.cbsa_geoid).to eq "16980"
34
+ expect(division.population).to be > 7_000_000
35
+ expect(division.housing_units).to be > 2_000_000
36
+ expect(division.land_area.round).to eq 3131
37
+ expect(division.water_area.round).to eq 731
38
+ end
39
+ end
40
+
41
+ end
@@ -0,0 +1,39 @@
1
+ require "spec_helper"
2
+
3
+ describe USGeo::PlaceCounty do
4
+
5
+ describe "associations" do
6
+ it "should have a place" do
7
+ place_county = USGeo::PlaceCounty.new
8
+ place_county.place_geoid = "0000001"
9
+ place_county.county_geoid = "00001"
10
+ expect{ place_county.place }.to_not raise_error
11
+ expect(place_county.build_place).to be_a(USGeo::Place)
12
+ end
13
+
14
+ it "should have a county" do
15
+ place_county = USGeo::PlaceCounty.new
16
+ place_county.place_geoid = "0000001"
17
+ place_county.county_geoid = "00001"
18
+ expect{ place_county.county }.to_not raise_error
19
+ expect(place_county.build_county).to be_a(USGeo::County)
20
+ end
21
+ end
22
+
23
+ describe "load" do
24
+ after { USGeo::PlaceCounty.delete_all }
25
+
26
+ it "should load the fixture data" do
27
+ data = File.read(File.expand_path("../../data/dist/place_counties.csv", __dir__))
28
+ stub_request(:get, "#{USGeo.base_data_uri}/place_counties.csv").to_return(body: data, headers: {"Content-Type": "text/csv; charset=UTF-8"})
29
+ USGeo::PlaceCounty.load!
30
+ expect(USGeo::PlaceCounty.imported.count).to be > 30_000
31
+ expect(USGeo::PlaceCounty.removed.count).to eq 0
32
+
33
+ place_counties = USGeo::PlaceCounty.where(place_geoid: "3651000")
34
+ expect(place_counties.size).to eq 5
35
+ expect(place_counties.collect(&:county_geoid)).to match_array(["36005", "36047", "36061", "36081", "36085"])
36
+ end
37
+ end
38
+
39
+ end
@@ -0,0 +1,71 @@
1
+ require "spec_helper"
2
+
3
+ describe USGeo::Place do
4
+
5
+ describe "associations" do
6
+ it "should have a state" do
7
+ place = USGeo::Place.new
8
+ place.geoid = "0000001"
9
+ expect{ place.state }.to_not raise_error
10
+ expect(place.build_state).to be_a(USGeo::State)
11
+ end
12
+
13
+ it "should have a primary county" do
14
+ place = USGeo::Place.new
15
+ place.geoid = "0000001"
16
+ expect{ place.primary_county }.to_not raise_error
17
+ expect(place.build_primary_county).to be_a(USGeo::County)
18
+ end
19
+
20
+ it "should have an urban area" do
21
+ place = USGeo::Place.new
22
+ place.geoid = "0000001"
23
+ expect{ place.urban_area }.to_not raise_error
24
+ expect(place.build_urban_area).to be_a(USGeo::UrbanArea)
25
+ end
26
+
27
+ it "should have counties" do
28
+ place = USGeo::Place.new
29
+ place.geoid = "0000001"
30
+ expect{ place.counties }.to_not raise_error
31
+ expect{ place.place_counties }.to_not raise_error
32
+ expect(place.place_counties.build).to be_a(USGeo::PlaceCounty)
33
+ end
34
+
35
+ it "should have zctas" do
36
+ place = USGeo::Place.new
37
+ place.geoid = "0000001"
38
+ expect{ place.zctas }.to_not raise_error
39
+ expect{ place.zcta_places }.to_not raise_error
40
+ expect(place.zcta_places.build).to be_a(USGeo::ZctaPlace)
41
+ end
42
+ end
43
+
44
+ describe "load" do
45
+ after { USGeo::Place.delete_all }
46
+
47
+ it "should load the fixture data" do
48
+ data = File.read(File.expand_path("../../data/dist/places.csv", __dir__))
49
+ stub_request(:get, "#{USGeo.base_data_uri}/places.csv").to_return(body: data, headers: {"Content-Type": "text/csv; charset=UTF-8"})
50
+ USGeo::Place.load!
51
+ expect(USGeo::Place.imported.count).to be > 30_000
52
+ expect(USGeo::Place.removed.count).to eq 0
53
+
54
+ place = USGeo::Place.find("0649187")
55
+ expect(place.gnis_id).to eq 2413013
56
+ expect(place.name).to eq "Town of Moraga"
57
+ expect(place.short_name).to eq "Moraga"
58
+ expect(place.state_code).to eq "CA"
59
+ expect(place.primary_county_geoid).to eq "06013"
60
+ expect(place.urban_area_geoid).to eq "19504"
61
+ expect(place.population).to be > 15_000
62
+ expect(place.housing_units).to be > 5000
63
+ expect(place.fips_class_code).to eq "C1"
64
+ expect(place.land_area.round(1)).to eq 9.5
65
+ expect(place.water_area.round(3)).to eq 0.009
66
+ expect(place.lat.round).to eq 38
67
+ expect(place.lng.round).to eq -122
68
+ end
69
+ end
70
+
71
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe USGeo::Region do
4
+
5
+ describe "associations" do
6
+ it "should have divisions" do
7
+ region = USGeo::Region.new
8
+ region.id = 1
9
+ expect{ region.divisions }.to_not raise_error
10
+ expect(region.divisions.build).to be_a(USGeo::Division)
11
+ end
12
+
13
+ it "should have states" do
14
+ region = USGeo::Region.new
15
+ region.id = 1
16
+ expect{ region.states }.to_not raise_error
17
+ expect(region.states.build).to be_a(USGeo::State)
18
+ end
19
+ end
20
+
21
+ describe "load" do
22
+ after { USGeo::Region.delete_all }
23
+
24
+ it "should load the fixture data" do
25
+ data = File.read(File.expand_path("../../data/dist/divisions.csv", __dir__))
26
+ stub_request(:get, "#{USGeo.base_data_uri}/divisions.csv").to_return(body: data, headers: {"Content-Type": "text/csv; charset=UTF-8"})
27
+ USGeo::Region.load!
28
+ expect(USGeo::Region.imported.count).to eq 4
29
+ expect(USGeo::Region.removed.count).to eq 0
30
+
31
+ region = USGeo::Region.find(2)
32
+ expect(region.name).to eq "Midwest"
33
+ end
34
+ end
35
+
36
+ end
@@ -0,0 +1,70 @@
1
+ require 'spec_helper'
2
+
3
+ describe USGeo::State do
4
+
5
+ describe "associations" do
6
+ it "should belong to a region" do
7
+ state = USGeo::State.new
8
+ state.code = "XX"
9
+ expect{ state.region }.to_not raise_error
10
+ expect(state.build_region).to be_a(USGeo::Region)
11
+ end
12
+
13
+ it "should belong to a division" do
14
+ state = USGeo::State.new
15
+ state.code = "XX"
16
+ expect{ state.division }.to_not raise_error
17
+ expect(state.build_division).to be_a(USGeo::Division)
18
+ end
19
+
20
+ it "should have counties" do
21
+ state = USGeo::State.new
22
+ state.code = "XX"
23
+ expect{ state.counties }.to_not raise_error
24
+ expect(state.counties.build).to be_a(USGeo::County)
25
+ end
26
+
27
+ it "should have places" do
28
+ state = USGeo::State.new
29
+ state.code = "XX"
30
+ expect{ state.places }.to_not raise_error
31
+ expect(state.places.build).to be_a(USGeo::Place)
32
+ end
33
+ end
34
+
35
+ describe "load" do
36
+ after { USGeo::State.delete_all }
37
+
38
+ it "should load the fixture data" do
39
+ data = File.read(File.expand_path("../../data/dist/states.csv", __dir__))
40
+ stub_request(:get, "#{USGeo.base_data_uri}/states.csv").to_return(body: data, headers: {"Content-Type": "text/csv; charset=UTF-8"})
41
+ USGeo::State.load!
42
+ expect(USGeo::State.imported.count).to eq 59
43
+ expect(USGeo::State.removed.count).to eq 0
44
+
45
+ illinois = USGeo::State.find("IL")
46
+ expect(illinois.name).to eq "Illinois"
47
+ expect(illinois.type).to eq "state"
48
+ expect(illinois.fips).to eq "17"
49
+ expect(illinois.region_id).to eq 2
50
+ expect(illinois.division_id).to eq 3
51
+ end
52
+ end
53
+
54
+ describe "type" do
55
+ it "should detect the state type" do
56
+ expect(USGeo::State.new(type: "state").state?).to eq true
57
+ expect(USGeo::State.new(type: "territory").state?).to eq false
58
+ expect(USGeo::State.new(type: "district").state?).to eq false
59
+
60
+ expect(USGeo::State.new(type: "state").district?).to eq false
61
+ expect(USGeo::State.new(type: "territory").district?).to eq false
62
+ expect(USGeo::State.new(type: "district").district?).to eq true
63
+
64
+ expect(USGeo::State.new(type: "state").territory?).to eq false
65
+ expect(USGeo::State.new(type: "territory").territory?).to eq true
66
+ expect(USGeo::State.new(type: "district").territory?).to eq false
67
+ end
68
+ end
69
+
70
+ end
@@ -0,0 +1,82 @@
1
+ require 'spec_helper'
2
+
3
+ describe USGeo::UrbanAreaCounty do
4
+
5
+ describe "percentages" do
6
+ it "should return the percentage of the population of the urban area" do
7
+ urban_area = USGeo::UrbanArea.new(population: 20_000)
8
+ urban_area_county = urban_area.urban_area_counties.build(population: 5000)
9
+ expect(urban_area_county.percent_urban_area_population).to eq 0.25
10
+ end
11
+
12
+ it "should return the percentage of the land area of the urban area" do
13
+ urban_area = USGeo::UrbanArea.new(land_area: 200)
14
+ urban_area_county = urban_area.urban_area_counties.build(land_area: 50)
15
+ expect(urban_area_county.percent_urban_area_land_area).to eq 0.25
16
+ end
17
+
18
+ it "should return the percentage of the total area of the urban area" do
19
+ urban_area = USGeo::UrbanArea.new(land_area: 150, water_area: 50)
20
+ urban_area_county = urban_area.urban_area_counties.build(land_area: 30, water_area: 20)
21
+ expect(urban_area_county.percent_urban_area_total_area).to eq 0.25
22
+ end
23
+
24
+ it "should return the percentage of the population of the county" do
25
+ county = USGeo::County.new(population: 20_000)
26
+ urban_area_county = county.urban_area_counties.build(population: 5000)
27
+ expect(urban_area_county.percent_county_population).to eq 0.25
28
+ end
29
+
30
+ it "should return the percentage of the land area of the county" do
31
+ county = USGeo::County.new(land_area: 200)
32
+ urban_area_county = county.urban_area_counties.build(land_area: 50)
33
+ expect(urban_area_county.percent_county_land_area).to eq 0.25
34
+ end
35
+
36
+ it "should return the percentage of the total area of the county" do
37
+ county = USGeo::County.new(land_area: 150, water_area: 50)
38
+ urban_area_county = county.urban_area_counties.build(land_area: 30, water_area: 20)
39
+ expect(urban_area_county.percent_county_total_area).to eq 0.25
40
+ end
41
+ end
42
+
43
+ describe "associations" do
44
+ it "should have a county" do
45
+ urban_area_county = USGeo::UrbanAreaCounty.new
46
+ urban_area_county.county_geoid = "60304"
47
+ urban_area_county.urban_area_geoid = "00001"
48
+ expect{ urban_area_county.county }.to_not raise_error
49
+ expect(urban_area_county.build_county).to be_a(USGeo::County)
50
+ end
51
+
52
+ it "should have an urban area" do
53
+ urban_area_county = USGeo::UrbanAreaCounty.new
54
+ urban_area_county.county_geoid = "60304"
55
+ urban_area_county.urban_area_geoid = "00001"
56
+ expect{ urban_area_county.urban_area }.to_not raise_error
57
+ expect(urban_area_county.build_urban_area).to be_a(USGeo::UrbanArea)
58
+ end
59
+ end
60
+
61
+ describe "load" do
62
+ after { USGeo::UrbanAreaCounty.delete_all }
63
+
64
+ it "should load the fixture data" do
65
+ data = File.read(File.expand_path("../../data/dist/urban_area_counties.csv", __dir__))
66
+ stub_request(:get, "#{USGeo.base_data_uri}/urban_area_counties.csv").to_return(body: data, headers: {"Content-Type": "text/csv; charset=UTF-8"})
67
+ USGeo::UrbanAreaCounty.load!
68
+ expect(USGeo::UrbanAreaCounty.imported.count).to be > 4000
69
+ expect(USGeo::UrbanAreaCounty.removed.count).to eq 0
70
+
71
+ urban_area_counties = USGeo::UrbanAreaCounty.where(urban_area_geoid: "39430")
72
+ expect(urban_area_counties.size).to eq 2
73
+ expect(urban_area_counties.collect(&:county_geoid)).to match_array(["26005", "26139"])
74
+ urban_area_county = urban_area_counties.detect{ |z| z.county_geoid == "26005"}
75
+ expect(urban_area_county.population).to be > 9000
76
+ expect(urban_area_county.housing_units).to be > 4000
77
+ expect(urban_area_county.land_area.round).to eq 8
78
+ expect(urban_area_county.water_area.round(2)).to eq 0.03
79
+ end
80
+ end
81
+
82
+ end