Skip to content

Commit

Permalink
Merge pull request jgv#12 from jfranz/master
Browse files Browse the repository at this point in the history
Thanks!
  • Loading branch information
jgv committed Aug 27, 2013
2 parents 223fde3 + ec1c23c commit 3ebd3ed
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 1 deletion.
26 changes: 26 additions & 0 deletions lib/area/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,30 @@ def to_gmt_offset
row[5] if row
end


# Public: Determine if a lat/lon pair observes daylight savings time.
#
# Examples
#
# [40.71209, -73.95427].to_dst
# #=> "1"
#
# Returns a String representation of daylight savings time observance.
def to_dst
row = Area.zip_codes.find {|row| row[3] == self[0].to_s and row[4] == self[1].to_s }
row[6] if row
end

# Public: Return boolean for daylight savings time observance.
#
# Examples
#
# [40.71209, -73.95427].observes_dst?
# #=> true
#
# Returns a Boolean of the daylight savings time observance.
def observes_dst?
to_dst == "1"
end

end
30 changes: 30 additions & 0 deletions lib/area/integer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,34 @@ def to_gmt_offset
end
end


# Public: Determine if a zipcode observes daylight savings time.
#
# Examples
#
# 11211.to_dst
# #=> "1"
#
# Returns a String representation of of the daylight savings time observance.
def to_dst
if Area.zip?(self)
warn "[DEPRECATION] using `to_dst` with an integer representaion of a zipcode is deprecated and will be removed in future versions. Please use a string instead."
row = Area.zip_codes.find {|row| row.first == self.to_s }
row[6] if row
end
end

# Public: Return boolean for daylight savings time observance.
#
# Examples
#
# 11211.observes_dst?
# #=> true
#
# Returns a Boolean of the daylight savings time observance.
def observes_dst?
to_dst == "1"
end


end
28 changes: 28 additions & 0 deletions lib/area/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,34 @@ def to_gmt_offset
end


# Public: Determine if a zipcode observes daylight savings time.
#
# Examples
#
# "11211".to_dst
# #=> "1"
#
# Returns a String representation of the daylight savings time observance.
def to_dst
if Area.zip_or_territory?(self.to_s)
row = Area.zip_codes.find {|row| row[2] != nil and (row[2].upcase == self.to_s.upcase or row[0] == self.to_s) }
row[6] if row
end
end

# Public: Return boolean for daylight savings time observance.
#
# Examples
#
# "11211".observes_dst?
# #=> true
#
# Returns a Boolean of the daylight savings time observance.
def observes_dst?
to_dst == "1"
end


# Public: Convert a zipcode to its latitude and longitude.
#
# Examples
Expand Down
17 changes: 16 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Hi. This gem allows you to perform the following conversions:
* A state to its GMT offset
* A lat/lon pair to a region
* A lat/lon pair to its GMT offset
* A lat/lon pair to its zip code
* A lat/lon pair to its zip code

Area uses public domain data and does not rely on any external services (the internets). Usage is meant to be more lightweight than the Geocoder gem.

Expand Down Expand Up @@ -73,6 +73,16 @@ In your gemfile: `gem 'area'`
"NY".to_gmt_offset #=> "-5" # by state
```

#### Determine daylight savings time observance of a zipcode
```` ruby
"11211".observes_dst? #=> true
```

#### Determine daylight savings time observance of a state
```` ruby
"NY".observes_dst? #=> true # by state
```

#### Convert a lat/lon pair to a zipcode
```` ruby
[40.71209, -73.95427].to_zip #=> "11211"
Expand All @@ -88,6 +98,11 @@ In your gemfile: `gem 'area'`
[40.71209, -73.95427].to_gmt_offset #=> "-5"
```

#### Determine daylight savings time observance for a lat/lon pair
```` ruby
[40.71209, -73.95427].observes_dst? #=> true
```

## Testing and Contributing and Stuff

Contributions are more than welcome. I'm [testing](http://travis-ci.org/jgv/area) with minitest. This gem supports:
Expand Down
44 changes: 44 additions & 0 deletions test/unit/area_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,22 @@ def test_that_it_converts_zip_code_int_to_gmt_offset
assert_equal "-5", 11211.to_gmt_offset
end

def test_that_it_converts_zip_code_int_daylight_savings_time_observance
assert_equal "1", 11211.to_dst
end

def test_that_it_returns_boolean_for_daylight_savings_time_observance
assert_equal true, 11211.observes_dst?
end

def test_that_it_handles_bad_area_codes
assert_raises(ArgumentError) { 1234.to_region }
assert_raises(ArgumentError) { 1234.to_latlon }
assert_raises(ArgumentError) { 1234.to_lat }
assert_raises(ArgumentError) { 1234.to_lon }
assert_raises(ArgumentError) { 1234.to_gmt_offset }
assert_raises(ArgumentError) { 1234.to_dst }
assert_raises(ArgumentError) { 1234.observes_dst? }
end

# Benchmarks
Expand Down Expand Up @@ -76,11 +86,25 @@ def test_that_it_converts_to_offset
assert_equal "-5", "ny".to_gmt_offset
end

def test_that_it_converts_daylight_savings_time_observance
assert_equal "1", "11211".to_dst
end

def test_that_it_returns_true_for_daylight_savings_time_observance
assert_equal true, "11211".observes_dst?
end

def test_that_it_returns_false_for_daylight_savings_time_nonobservance
assert_equal false, "AZ".observes_dst?
end

def test_that_it_handles_incorrect_zips
assert_equal [], "9888".to_zip
assert_raises(ArgumentError) { "9888".to_region }
assert_raises(ArgumentError) { "9888".to_area }
assert_raises(ArgumentError) { "9888".to_gmt_offset }
assert_raises(ArgumentError) { "9888".to_dst }
assert_raises(ArgumentError) { "9888".observes_dst? }
end

def test_that_it_converts_zip_code_to_latlon
Expand Down Expand Up @@ -149,6 +173,18 @@ def bench_to_offset
end
end

def bench_to_dst
assert_performance_constant 0.9999 do |n|
n.times { "ny".to_dst }
end
end

def bench_observes_dst?
assert_performance_constant 0.9999 do |n|
n.times { "ny".observes_dst? }
end
end

end

class TestArray < MiniTest::Unit::TestCase
Expand All @@ -169,6 +205,14 @@ def test_that_it_converts_latlon_to_gmt_offset
assert_equal "-5", ["40.71209", "-73.95427"].to_gmt_offset
end

def test_that_it_converts_latlon_to_daylight_savings_time_observance
assert_equal "1", ["40.71209", "-73.95427"].to_dst
end

def test_that_it_returns_boolean_for_daylight_savings_time_observance
assert_equal true, ["40.71209", "-73.95427"].observes_dst?
end

def test_that_it_handles_latlon_precision
assert_equal "11211", ["40.71209123228157", "-73.95488409019887"].to_zip
end
Expand Down

0 comments on commit 3ebd3ed

Please sign in to comment.