quake 0.0.2 → 0.0.3

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.
data/README.md CHANGED
@@ -35,6 +35,11 @@ Fetch all the earthquakes from the past [week|day|hour] with minimum and/or maxi
35
35
  events = Quake::Event.last_week :max_magnitude => 4
36
36
  events = Quake::Event.last_week :min_magnitude => 3, :max_magnitude => 4
37
37
 
38
+ Fetch all the earthquakes from the past [week|day|hour] with a minimum and/or maximum magnitude at or near a location (in km):
39
+
40
+ events = Quake::Event.last_week :min_magnitude => 2, :epicenter => "37.156, -117.3723"
41
+ events = Quake::Event.last_week :min_magnitude => 2, :epicenter => "37.156, -117.3723", :distance => 100
42
+
38
43
  Questions or Problems?
39
44
  ----------------------
40
45
 
@@ -15,28 +15,35 @@ module Quake
15
15
  attr_accessor :nst
16
16
  attr_accessor :region
17
17
 
18
- def self.last_week(criteria = {:min_magnitude => 2.5, :max_magnitude => 10})
18
+ def self.last_week(criteria = {:min_magnitude => 2.5})
19
19
  raw_items = CSV.parse(Curl::Easy.perform(WEEK).body_str)
20
20
  create_events(raw_items, criteria)
21
21
  end
22
22
 
23
- def self.last_day(criteria = {:max_magnitude => 10})
23
+ def self.last_day(criteria = {})
24
24
  raw_items = CSV.parse(Curl::Easy.perform(DAY).body_str)
25
25
  create_events(raw_items, criteria)
26
26
  end
27
27
 
28
- def self.last_hour(criteria = {:max_magnitude => 10})
28
+ def self.last_hour(criteria = {})
29
29
  raw_items = CSV.parse(Curl::Easy.perform(HOUR).body_str)
30
30
  create_events(raw_items, criteria)
31
31
  end
32
32
 
33
+ def distance_from(lat, long)
34
+ (Math.acos(Math.sin(self.latitude) *
35
+ Math.sin(lat) + Math.cos(self.latitude) *
36
+ Math.cos(lat) * Math.cos(self.longitude - long)) *
37
+ SPHERICAL_APPROX_OF_EARTH).round(2)
38
+ end
39
+
33
40
  def initialize(raw)
34
41
  self.source = raw[0]
35
42
  self.id = raw[1]
36
43
  self.version = raw[2].to_i
37
44
  self.datetime = DateTime.strptime(raw[3], fmt="%A, %B %d, %Y %H:%M:%S UTC")
38
- self.latitude = raw[4]
39
- self.longitude = raw[5]
45
+ self.latitude = raw[4].to_f
46
+ self.longitude = raw[5].to_f
40
47
  self.magnitude = raw[6].to_f
41
48
  self.depth = raw[7].to_f
42
49
  self.nst = raw[8].to_i
@@ -58,6 +65,9 @@ module Quake
58
65
  valid_event = false unless event.magnitude >= criteria[criterion]
59
66
  when :max_magnitude
60
67
  valid_event = false unless event.magnitude <= criteria[criterion]
68
+ when :epicenter
69
+ criteria[:distance] = 0 unless criteria[:distance]
70
+ valid_event = self.check_distance(event, criteria)
61
71
  end
62
72
  end
63
73
  events << Event.new(raw_item) if valid_event
@@ -65,9 +75,22 @@ module Quake
65
75
  events
66
76
  end
67
77
 
78
+ def self.check_distance(event, criteria)
79
+ valid_event = true
80
+ if LAT_LONG_REGEX.match(criteria[:epicenter])
81
+ coordinates = criteria[:epicenter].split(',').collect{ |coordinate| coordinate.strip.to_f }
82
+ valid_event = false unless event.distance_from(coordinates[0], coordinates[1]) <= criteria[:distance]
83
+ else
84
+ valid_event = false
85
+ end
86
+ valid_event
87
+ end
88
+
68
89
  WEEK = "http://earthquake.usgs.gov/earthquakes/catalogs/eqs7day-M2.5.txt"
69
90
  DAY = "http://earthquake.usgs.gov/earthquakes/catalogs/eqs1day-M0.txt"
70
91
  HOUR = "http://earthquake.usgs.gov/earthquakes/catalogs/eqs1hour-M0.txt"
92
+ SPHERICAL_APPROX_OF_EARTH = 6371 # km
93
+ LAT_LONG_REGEX = /^\s*[-+]?\d+\.\d+\,\s?[-+]?\d+\.\d+\s*$/
71
94
 
72
95
  end
73
96
 
@@ -1,3 +1,3 @@
1
1
  module Quake
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -46,4 +46,31 @@ class EventTests < MiniTest::Unit::TestCase
46
46
  assert(events.count > 0)
47
47
  end
48
48
 
49
+ def test_event_distance_for_identicals
50
+ events = Quake::Event.last_day
51
+ event = events[0]
52
+ assert(event.distance_from(event.latitude, event.longitude) == 0)
53
+ end
54
+
55
+ def test_event_distance_for_different
56
+ events = Quake::Event.last_day
57
+ event1 = events[0]
58
+ event2 = events[events.count-1]
59
+ assert(event1.distance_from(event2.latitude, event2.longitude) > 0)
60
+ end
61
+
62
+ def test_revent_events_with_epicenter
63
+ events = Quake::Event.last_day
64
+ event = events[0]
65
+ events = Quake::Event.last_day :epicenter => "#{event.latitude},#{event.longitude}"
66
+ assert(events.count == 1)
67
+ end
68
+
69
+ def test_revent_events_with_epicenter_and_distance
70
+ events = Quake::Event.last_day
71
+ event = events[0]
72
+ events = Quake::Event.last_day :epicenter => "#{event.latitude},#{event.longitude}", :distance => 20004 # half the earth's circumference
73
+ assert(events.count > 1)
74
+ end
75
+
49
76
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: quake
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-18 00:00:00.000000000Z
12
+ date: 2011-08-20 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: curb
16
- requirement: &70213556338760 !ruby/object:Gem::Requirement
16
+ requirement: &70313665214360 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: 0.7.15
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70213556338760
24
+ version_requirements: *70313665214360
25
25
  description: A library exposing the U.S. Geological Survey's real-time earthquake
26
26
  data
27
27
  email: