gull 0.2.13 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7841c6854343d87ee16705b0606bea985d8d27cf
4
- data.tar.gz: 1102c41588ddeab4435d344420e47c1ba0b09956
3
+ metadata.gz: 85f0268b1bbaa2d578bd56d7009ae77e6677844d
4
+ data.tar.gz: 36044cbee5417556bd2957b12617ac28fc9d97b2
5
5
  SHA512:
6
- metadata.gz: fc13ad4a90732adcde90db23e27c948306f98ac8a23fe6a3726446627708be4803bd4d475396e2ebe156692cfd1d14f20d57165f80784288961ef0cc2ad4fd8f
7
- data.tar.gz: 73965ce13979a9ba0962a845f86f86797b78bb6811541a69f9dfd889c7423407c5d57b6a061917580c8753148dab20a21f94622b871e926edfa8f9c91993c0ad
6
+ metadata.gz: f5a27d2ce025475eb49c36be539f36776836ce4bd817cad9293aa87b18004972720262a969cb57acab801566986da218bdb0b1faf45decddce7cab590dce0e52
7
+ data.tar.gz: 9fe4fc8c1eb48d41200f0b469cc8334c05adc6684ef8836d9489edd6f04936906887d6097cfbf5bd3c9344fbe9dacf6ebcb98d3b0f39d3043c5e3abec46dabeb
@@ -1,15 +1,26 @@
1
- ### 0.2.13 (6/20/2015) - Handle missing cap section
1
+ #### 0.3.0 (6/23/2015)
2
+ Added Client class for error handling (bad entries accessed via errors array). Exposed option to turn on strict XML parsing for debugging. Alert.fetch uses new Client but is backwards compatible, however it doesn't give you access to errors array.
3
+ ***
4
+ #### 0.2.13 (6/20/2015)
5
+ Handle missing cap section
2
6
  ***
3
- ### 0.2.12 (6/12/2015) - Fix issue with missing link elements
7
+ #### 0.2.12 (6/12/2015)
8
+ Fix issue with missing link elements
4
9
  ***
5
- ### 0.2.11 (5/29/2015) - Added method to get original polygon string
10
+ #### 0.2.11 (5/29/2015)
11
+ Added method to get original polygon string
6
12
  ***
7
- ### 0.2.10 (3/23/2015) - Added option to override default alert service URI.
13
+ #### 0.2.10 (3/23/2015)
14
+ Added option to override default alert service URI.
8
15
  ***
9
- ### 0.2.1 (10/02/2014) - Added static map image to polygons.
16
+ #### 0.2.1 (10/02/2014)
17
+ Added static map image to polygons.
10
18
  ***
11
- #### 0.2.0 (10/02/2014) - Introduced Polygon type.
19
+ #### 0.2.0 (10/02/2014)
20
+ Introduced Polygon type.
12
21
  ***
13
- #### 0.1.1 (10/01/2014) - Refectored and simplfied alert processing.
22
+ #### 0.1.1 (10/01/2014)
23
+ Refectored and simplfied alert processing.
14
24
  ***
15
- #### 0.1.0 (10/01/2014) - Initial release, basic functionality of fetching and parsing alerts.
25
+ #### 0.1.0 (10/01/2014)
26
+ Initial release, basic functionality of fetching and parsing alerts.
@@ -1,5 +1,6 @@
1
1
  require 'gull/version'
2
2
  require 'gull/error'
3
+ require 'gull/client'
3
4
  require 'gull/alert'
4
5
  require 'gull/polygon'
5
6
  require 'gull/geocode'
@@ -2,6 +2,8 @@ require 'httpclient'
2
2
  require 'nokogiri'
3
3
 
4
4
  module Gull
5
+ # Gull represents an NWS/NOAA alert and provides the ability to fetch
6
+ # them from the public web service
5
7
  class Alert
6
8
  attr_accessor :id, :title, :summary, :link, :alert_type, :polygon, :area,
7
9
  :effective_at, :expires_at, :updated_at, :published_at,
@@ -12,13 +14,8 @@ module Gull
12
14
  end
13
15
 
14
16
  def self.fetch(options = {})
15
- options = {
16
- url: 'http://alerts.weather.gov/cap/us.php?x=1'
17
- }.merge options
18
-
19
- content = response options
20
- document = Nokogiri::XML content
21
- process document.xpath('//xmlns:feed/xmlns:entry', namespaces)
17
+ client = Client.new options
18
+ client.fetch
22
19
  end
23
20
 
24
21
  def parse(element)
@@ -33,43 +30,6 @@ module Gull
33
30
 
34
31
  private
35
32
 
36
- def self.namespaces
37
- { 'xmlns' => 'http://www.w3.org/2005/Atom',
38
- 'cap' => 'urn:oasis:names:tc:emergency:cap:1.1',
39
- 'ha' => 'http://www.alerting.net/namespace/index_1.0' }
40
- end
41
-
42
- def namespaces
43
- Alert.namespaces
44
- end
45
-
46
- def self.response(options)
47
- client = HTTPClient.new
48
- begin
49
- return client.get_content options[:url]
50
- rescue HTTPClient::TimeoutError
51
- raise TimeoutError, 'Timeout while connecting to NWS web service'
52
- end
53
- end
54
-
55
- def self.process(entries)
56
- alerts = []
57
- entries.each do |entry|
58
- alert = create_instance entry
59
- alerts.push alert unless alert.nil?
60
- end
61
-
62
- alerts
63
- end
64
-
65
- def self.create_instance(entry)
66
- return if entry.xpath('cap:event').empty?
67
-
68
- alert = Alert.new
69
- alert.parse entry
70
- alert
71
- end
72
-
73
33
  def parse_core_attributes(element)
74
34
  self.id = element.css('id').inner_text
75
35
  self.title = element.css('title').inner_text
@@ -0,0 +1,62 @@
1
+ require 'httpclient'
2
+ require 'nokogiri'
3
+
4
+ module Gull
5
+ # Client exposes methods and options for fetching alerts from the NWS/NOAA
6
+ # web service
7
+ class Client
8
+ attr_accessor :errors
9
+
10
+ def initialize(options = {})
11
+ @options = {
12
+ url: 'http://alerts.weather.gov/cap/us.php?x=1',
13
+ strict: false
14
+ }.merge options
15
+ end
16
+
17
+ def fetch
18
+ self.errors = []
19
+ content = response
20
+ document = Nokogiri::XML content do |config|
21
+ config.strict if @options[:strict]
22
+ end
23
+ process document.xpath('//xmlns:feed/xmlns:entry', namespaces)
24
+ end
25
+
26
+ private
27
+
28
+ def response
29
+ client = HTTPClient.new
30
+ begin
31
+ return client.get_content @options[:url]
32
+ rescue HTTPClient::TimeoutError
33
+ raise TimeoutError, 'Timeout while connecting to NWS web service'
34
+ end
35
+ end
36
+
37
+ def process(entries)
38
+ alerts = []
39
+ entries.each do |entry|
40
+ alert = create_instance entry
41
+ alerts.push alert unless alert.nil?
42
+ errors.push entry if alert.nil?
43
+ end
44
+
45
+ alerts
46
+ end
47
+
48
+ def create_instance(entry)
49
+ return if entry.xpath('cap:event').empty?
50
+
51
+ alert = Alert.new
52
+ alert.parse entry
53
+ alert
54
+ end
55
+
56
+ def namespaces
57
+ { 'xmlns' => 'http://www.w3.org/2005/Atom',
58
+ 'cap' => 'urn:oasis:names:tc:emergency:cap:1.1',
59
+ 'ha' => 'http://www.alerting.net/namespace/index_1.0' }
60
+ end
61
+ end
62
+ end
@@ -1,3 +1,3 @@
1
1
  module Gull
2
- VERSION = '0.2.13'
2
+ VERSION = '0.3.0'
3
3
  end
@@ -59,14 +59,25 @@ describe Gull::Alert do
59
59
  it 'should fetch from url in options' do
60
60
  xml = File.read 'spec/fixtures/alerts.xml'
61
61
 
62
- stub_request(:get, 'http://alerts.weather.gov/cap/ok.php?x=1')
62
+ stub_request(:get, 'http://test.url')
63
63
  .with(headers: { 'Accept' => '*/*' })
64
64
  .to_return(status: 200, body: xml, headers: {})
65
65
 
66
- alerts = Gull::Alert.fetch(url: 'http://alerts.weather.gov/cap/ok.php?x=1')
66
+ alerts = Gull::Alert.fetch(url: 'http://test.url')
67
67
  expect(alerts.size).to eq(3)
68
68
  end
69
69
 
70
+ it 'should enable strict xml parsing via option' do
71
+ xml = File.read 'spec/fixtures/bad.xml'
72
+
73
+ stub_request(:get, 'http://alerts.weather.gov/cap/us.php?x=1')
74
+ .with(headers: { 'Accept' => '*/*' })
75
+ .to_return(status: 200, body: xml, headers: {})
76
+
77
+ expect { Gull::Alert.fetch(strict: true) }
78
+ .to raise_error(Nokogiri::XML::SyntaxError)
79
+ end
80
+
70
81
  it 'should handle empty alerts' do
71
82
  xml = File.read 'spec/fixtures/empty.xml'
72
83
 
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+
3
+ describe Gull::Client do
4
+ it 'should initialize with options' do
5
+ xml = File.read 'spec/fixtures/alerts.xml'
6
+ stub_request(:get, 'http://test.url')
7
+ .with(headers: { 'Accept' => '*/*' })
8
+ .to_return(status: 200, body: xml, headers: {})
9
+
10
+ options = { url: 'http://test.url', strict: true }
11
+ client = Gull::Client.new(options)
12
+ alerts = client.fetch
13
+ expect(alerts.size).to eq 3
14
+
15
+ xml = File.read 'spec/fixtures/bad.xml'
16
+ stub_request(:get, 'http://test.url')
17
+ .with(headers: { 'Accept' => '*/*' })
18
+ .to_return(status: 200, body: xml, headers: {})
19
+
20
+ expect { client.fetch }
21
+ .to raise_error(Nokogiri::XML::SyntaxError)
22
+ end
23
+
24
+ it 'should fetch alerts without options' do
25
+ xml = File.read 'spec/fixtures/alerts.xml'
26
+
27
+ stub_request(:get, 'http://alerts.weather.gov/cap/us.php?x=1')
28
+ .with(headers: { 'Accept' => '*/*' })
29
+ .to_return(status: 200, body: xml, headers: {})
30
+
31
+ client = Gull::Client.new
32
+ alerts = client.fetch
33
+ expect(alerts.size).to eq 3
34
+ expect(client.errors.size).to eq 0
35
+ end
36
+
37
+ it 'should handle incomplete entries in xml' do
38
+ xml = File.read 'spec/fixtures/missing_cap.xml'
39
+
40
+ stub_request(:get, 'http://alerts.weather.gov/cap/us.php?x=1')
41
+ .with(headers: { 'Accept' => '*/*' })
42
+ .to_return(status: 200, body: xml, headers: {})
43
+
44
+ client = Gull::Client.new
45
+ alerts = client.fetch
46
+ expect(alerts.size).to eq 0
47
+ expect(client.errors.size).to eq 1
48
+ end
49
+ end
@@ -41,16 +41,6 @@ xmlns:ha = 'http://www.alerting.net/namespace/index_1.0'
41
41
  <title>Flood Warning issued June 20 at 8:02AM CDT until June 23 at 2:53AM CDT by NWS</title>
42
42
  <link href='http://alerts.weather.gov/cap/wwacapget.php?x=LA1253AE8E0B98.FloodWarning.1253AEBAFBE4LA.SHVFLSSHV.3908c4d415c0ed7efa65ce2f6eea425e'/>
43
43
  <summary>The flood warning continues for the Red Chute Bayou At Sligo, Louisiana. * until late Monday night...or until the warning is cancelled. * At 70 AM Saturday the stage was 32.0 feet. * Minor flooding is occurring and minor flooding is forecast. * Flood stage is 31 feet.</summary>
44
- <valueName>FIPS6</valueName>
45
- <value>022013 022015</value>
46
- <valueName>UGC</valueName>
47
- <value>LAC013 LAC015</value>
48
- </cap:geocode>
49
- <cap:parameter>
50
- <valueName>VTEC</valueName>
51
- <value>/O.EXT.KSHV.FL.W.0168.000000T0000Z-150623T0753Z/
52
- /SLGL1.1.ER.150618T1228Z.150619T1330Z.150622T1353Z.NO/</value>
53
- </cap:parameter>
54
44
  </entry>
55
45
 
56
46
  </feed>
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gull
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.13
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Seth Deckard
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-20 00:00:00.000000000 Z
11
+ date: 2015-06-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httpclient
@@ -114,6 +114,7 @@ files:
114
114
  - gull.gemspec
115
115
  - lib/gull.rb
116
116
  - lib/gull/alert.rb
117
+ - lib/gull/client.rb
117
118
  - lib/gull/error.rb
118
119
  - lib/gull/geocode.rb
119
120
  - lib/gull/polygon.rb
@@ -121,6 +122,7 @@ files:
121
122
  - spec/alert_spec.rb
122
123
  - spec/fixtures/alerts.xml
123
124
  - spec/fixtures/bad.xml
125
+ - spec/fixtures/client_spec.rb
124
126
  - spec/fixtures/empty.xml
125
127
  - spec/fixtures/missing_cap.xml
126
128
  - spec/polygon_spec.rb
@@ -153,6 +155,7 @@ test_files:
153
155
  - spec/alert_spec.rb
154
156
  - spec/fixtures/alerts.xml
155
157
  - spec/fixtures/bad.xml
158
+ - spec/fixtures/client_spec.rb
156
159
  - spec/fixtures/empty.xml
157
160
  - spec/fixtures/missing_cap.xml
158
161
  - spec/polygon_spec.rb