traffic 0.0.1 → 0.0.2

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.
@@ -2,7 +2,7 @@ module Traffic
2
2
  MAIN_ATTRIBUTES = [:timestamp, :count, :size, :traffic]
3
3
  Info = Struct.new(*MAIN_ATTRIBUTES) do
4
4
  def traffic?
5
- !traffic
5
+ traffic
6
6
  end
7
7
 
8
8
  def items
@@ -0,0 +1,59 @@
1
+ require 'httpclient'
2
+ require 'nokogiri'
3
+
4
+ module Traffic
5
+ module Providers
6
+ class Anwb
7
+ HTML_FEED = "http://flitsapp.nl/ios/anwb/"
8
+
9
+ def traffic
10
+ !(count_info[0].text =~ /Totaal aantal meldingen: 0/)
11
+ end
12
+
13
+ def timestamp
14
+ Time.parse(count_info[2].text[/Laatste update: (.+) uur/, 1])
15
+ end
16
+
17
+ def count
18
+ count_info[0].text[/Totaal aantal meldingen: (.+)/, 1].to_i
19
+ end
20
+
21
+ def size
22
+ count_info[1].text[/Totale lengte: (.+) km/, 1].to_i
23
+ end
24
+
25
+ def each_item(&block)
26
+ html.css(".message").each do |entry|
27
+ item = Struct.new(*ITEM_ATTRIBUTES).new
28
+
29
+ item.road = entry.css("span.roadnumber_a,span.roadnumber_n").text
30
+ data = entry.css("strong.desc_short").text
31
+ item.from = data[/(.*) - (.*)/, 1]
32
+ item.to = data[/(.*) - (.*)/, 2]
33
+
34
+ item.description = entry.css("p.desc_long").children.first.text
35
+ hmp = entry.css("p.desc_long span.hmp")
36
+ item.from_location = hmp[1].text.to_f
37
+ item.to_location = hmp[0].text.to_f
38
+
39
+ unless entry["class"].include?("roadworks")
40
+ item.length = entry.css("span.km").text.to_f
41
+ else
42
+ item.length = (hmp[1].text.to_f - hmp[0].text.to_f).abs.round(1)
43
+ end
44
+
45
+ yield item
46
+ end
47
+ end
48
+
49
+ private
50
+ def count_info
51
+ html.css("#count").children.select(&:text?)
52
+ end
53
+
54
+ def html
55
+ @data ||= Nokogiri::HTML::Document.parse(HTTPClient.new.get(HTML_FEED).content)
56
+ end
57
+ end
58
+ end
59
+ end
@@ -6,7 +6,7 @@ module Traffic
6
6
  RSS_FEED = "http://www.fileindex.nl/rss.php"
7
7
 
8
8
  def traffic
9
- rss.description =~ /Er zijn geen files./
9
+ !(rss.description =~ /Er zijn geen files./)
10
10
  end
11
11
 
12
12
  def timestamp
@@ -1,3 +1,3 @@
1
1
  module Traffic
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,18 @@
1
+ <!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
2
+ <html>
3
+ <head>
4
+ <title>File overzicht</title>
5
+ <link rel="stylesheet" type="text/css" href="style.css">
6
+ <meta name="format-detection" content="telephone=no">
7
+ <meta name="viewport" content="user-scalable=no">
8
+ </head>
9
+ <body>
10
+ <a name="top"></a>
11
+ <div id='count'>
12
+ Totaal aantal meldingen: 0<br />
13
+ Totale lengte: 0 km<br />
14
+ Laatste update: 11:45 uur
15
+ </div>
16
+ <p class='source'>Bron:<br /><a href='http://www.anwb.nl/' target='_blank'><img src='anwb.png' width='160' border='0' /><br />ANWB Verkeersinformatie</a></p>
17
+ </body>
18
+ </html>
@@ -0,0 +1,39 @@
1
+ <!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
2
+ <html>
3
+ <head>
4
+ <title>File overzicht</title>
5
+ <link rel="stylesheet" type="text/css" href="style.css">
6
+ <meta name="format-detection" content="telephone=no">
7
+ <meta name="viewport" content="user-scalable=no">
8
+ </head>
9
+ <body>
10
+ <a name="top"></a>
11
+ <div id='count'>
12
+ Totaal aantal meldingen: 3<br />
13
+ Totale lengte: 5 km<br />
14
+ Laatste update: 11:20 uur
15
+ </div>
16
+ <div id="anchors"><a href="#A20" class="roadnumber_a">A20</a></div><a name="A20"></a> <a href='#top'>
17
+ <div id='msg_1541548' class='message roadworks'>
18
+ <span class='roadnumber_a'>A20</span>
19
+ <strong class='desc_short'>Gouda - Hoek van Holland</strong>
20
+ <p class='desc_long'>A20 Gouda richting Hoek van Holland bij Spaanse Polder is de rechterrijstrook dicht, door een ongeluk.<br /><span class="hmp">27.0</span><span class="hmp">27.9</span></p>
21
+ </div>
22
+ </a>
23
+ <a href='#top'>
24
+ <div id='msg_1541552' class='message'>
25
+ <span class='roadnumber_a'>A20</span>
26
+ <span class='km'>3 km</span> <strong class='desc_short'>Gouda - Hoek van Holland</strong>
27
+ <p class='desc_long'>A20 Gouda richting Hoek van Holland tussen knp. Terbregseplein en Rotterdam-Centrum 3 km, Filelengte neemt toe<br /><span class="hmp">30.3</span><span class="hmp">36.5</span></p>
28
+ </div>
29
+ </a>
30
+ <a href='#top'>
31
+ <div id='msg_1541547' class='message'>
32
+ <span class='roadnumber_a'>A20</span>
33
+ <span class='km'>2 km</span> <strong class='desc_short'>Gouda - Hoek van Holland</strong>
34
+ <p class='desc_long'>A20 Gouda richting Hoek van Holland tussen Rotterdam-Centrum en Spaanse Polder 2 km, door een ongeluk.<br /><span class="hmp">27.0</span><span class="hmp">31.1</span></p>
35
+ </div>
36
+ </a>
37
+ <p class='source'>Bron:<br /><a href='http://www.anwb.nl/' target='_blank'><img src='anwb.png' width='160' border='0' /><br />ANWB Verkeersinformatie</a></p>
38
+ </body>
39
+ </html>
@@ -0,0 +1,99 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe Traffic do
4
+
5
+ describe ".from(:anwb)" do
6
+
7
+ before :each do
8
+ Feedzirra::Feed.stubs(:fetch_and_parse).raises("do not connect to feed, stub this method")
9
+ HTTPClient.any_instance.stubs(:get).raises("do not connect to net, stub this method")
10
+ end
11
+
12
+ def stub_html(fixture)
13
+ stub = mock(:content => File.read(File.expand_path("../../fixtures/#{fixture}", __FILE__)))
14
+ HTTPClient.any_instance.stubs(:get).returns(stub)
15
+ end
16
+
17
+ context "when there is no traffic" do
18
+ before :each do
19
+ stub_html "anwb/empty.html"
20
+ @info = Traffic.from(:anwb)
21
+ end
22
+
23
+ it "should indicate that there is no traffic" do
24
+ @info.traffic.should_not be_true
25
+ end
26
+ end
27
+
28
+ context "when there is traffic" do
29
+ before :each do
30
+ stub_html "anwb/traffic.html"
31
+ @info = Traffic.from(:anwb)
32
+ end
33
+
34
+ it "should indicate that there is traffic" do
35
+ @info.traffic.should be_true
36
+ end
37
+
38
+ it "should return a timestamp" do
39
+ @info.timestamp.strftime("%H:%M").should == "11:20"
40
+ end
41
+
42
+ it "should return an info object" do
43
+ @info.class.should == Traffic::Info
44
+ end
45
+
46
+ it "should be able to return the total size" do
47
+ @info.size.should == 5
48
+ end
49
+
50
+ it "should be able to return the total count" do
51
+ @info.count.should == 3
52
+ end
53
+
54
+ it "should return a list with info items with the same length as count" do
55
+ @info.items.size.should == @info.count
56
+ end
57
+
58
+ it "should return a list with info items" do
59
+ @info.items.each { |i| i.class.should == Traffic::InfoItem }
60
+ end
61
+
62
+ context "the info items in the list" do
63
+ before :each do
64
+ @info_item = @info.items.first
65
+ end
66
+
67
+ it "should include a from" do
68
+ @info_item.from.should == "Gouda"
69
+ end
70
+
71
+ it "should include a to" do
72
+ @info_item.to.should == "Hoek van Holland"
73
+ end
74
+
75
+ it "should include a road number" do
76
+ @info_item.road.should == "A20"
77
+ end
78
+
79
+ it "should include a length in km" do
80
+ @info_item.length.should == 0.9
81
+ end
82
+
83
+ it "should include a description" do
84
+ @info_item.description.should == "A20 Gouda richting Hoek van Holland bij Spaanse Polder is de rechterrijstrook dicht, door een ongeluk."
85
+ end
86
+
87
+ it "should include a from location (hecto markers)" do
88
+ @info_item.from_location.should == 27.9
89
+ end
90
+
91
+ it "should include a to location" do
92
+ @info_item.to_location.should == 27.0
93
+ end
94
+ end
95
+ end
96
+
97
+ end
98
+
99
+ end
@@ -0,0 +1,110 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe Traffic do
4
+
5
+ describe ".from(:file_index)" do
6
+
7
+ before :each do
8
+ Feedzirra::Feed.stubs(:fetch_and_parse).raises("do not connect to feed, stub this method")
9
+ end
10
+
11
+ def stub_feed(fixture)
12
+ stub = Feedzirra::Feed.parse(File.read(File.expand_path("../../fixtures/#{fixture}", __FILE__)))
13
+ Feedzirra::Feed.stubs(:fetch_and_parse).returns(stub)
14
+ end
15
+
16
+ context "when there is no traffic" do
17
+ before :each do
18
+ stub_feed "file_index/empty_rss.xml"
19
+ @info = Traffic.from(:file_index)
20
+ end
21
+
22
+ it "should indicate that there is no traffic" do
23
+ @info.traffic.should_not be_true
24
+ end
25
+ end
26
+
27
+ context "when there is traffic" do
28
+ before :each do
29
+ stub_feed "file_index/rss.xml"
30
+ @info = Traffic.from(:file_index)
31
+ end
32
+
33
+ it "should indicate that there is traffic" do
34
+ @info.traffic.should be_true
35
+ end
36
+
37
+ it "should return a timestamp" do
38
+ @info.timestamp.strftime("%H:%M").should == "16:15"
39
+ end
40
+
41
+ it "should return an info object" do
42
+ @info.class.should == Traffic::Info
43
+ end
44
+
45
+ it "should be able to return the total size" do
46
+ @info.size.should == 45
47
+ end
48
+
49
+ it "should be able to return the total count" do
50
+ @info.count.should == 14
51
+ end
52
+
53
+ it "should return a list with info items with the same length as count" do
54
+ @info.items.size.should == @info.count
55
+ end
56
+
57
+ it "should return a list with info items" do
58
+ @info.items.each { |i| i.class.should == Traffic::InfoItem }
59
+ end
60
+
61
+ context "the info items in the list" do
62
+ before :each do
63
+ @info_item = @info.items.first
64
+ end
65
+
66
+ it "should include a from" do
67
+ @info_item.from.should == "Amsterdam"
68
+ end
69
+
70
+ it "should include a to" do
71
+ @info_item.to.should == "Amersfoort"
72
+ end
73
+
74
+ it "should include a road number" do
75
+ @info_item.road.should == "A1"
76
+ end
77
+
78
+ it "should include a length in km" do
79
+ @info_item.length.should == 2.6
80
+ end
81
+
82
+ it "should include a description" do
83
+ @info_item.description.should == "Langzaam rijdend verkeer"
84
+ end
85
+
86
+ it "should include a location" do
87
+ @info_item.location.should == "Eemnes en Eembrugge"
88
+ end
89
+
90
+ it "should include a cause" do
91
+ @info_item.cause.should == "Ongeval(len)"
92
+ end
93
+
94
+ it "should include a status" do
95
+ @info_item.status.should == "Deze file wordt langer."
96
+ end
97
+
98
+ it "should include a from location (hecto markers)" do
99
+ @info_item.from_location.should == 30.2
100
+ end
101
+
102
+ it "should include a to location" do
103
+ @info_item.to_location.should == 32.8
104
+ end
105
+ end
106
+ end
107
+
108
+ end
109
+
110
+ end
@@ -14,14 +14,7 @@ RSpec.configure do |config|
14
14
 
15
15
  config.before :each do
16
16
  FakeWeb.allow_net_connect = false
17
- Feedzirra::Feed.stubs(:fetch_and_parse).raises("do not connect to feed, stub this method")
18
17
  end
19
18
  end
20
19
 
21
-
22
- def stub_feed(fixture)
23
- stub = Feedzirra::Feed.parse(File.read(File.expand_path("../fixtures/#{fixture}", __FILE__)))
24
- Feedzirra::Feed.stubs(:fetch_and_parse).returns(stub)
25
- end
26
-
27
20
  load File.expand_path('../../lib/traffic.rb', __FILE__)
@@ -1,6 +1,14 @@
1
1
  require File.expand_path('../../spec_helper', __FILE__)
2
2
 
3
3
  describe Traffic do
4
+ describe "Info" do
5
+ it "should have a traffic presence tester" do
6
+ item = Traffic::Info.new
7
+ item.traffic = true
8
+ item.traffic?.should be_true
9
+ end
10
+ end
11
+
4
12
  describe "InfoItem" do
5
13
 
6
14
  it "should have presence testers" do
@@ -30,94 +30,6 @@ describe Traffic do
30
30
  end
31
31
  lambda { Traffic.from(:empty_provider) }.should raise_error
32
32
  end
33
-
34
- context "when there is no traffic" do
35
- before :each do
36
- stub_feed "file_index/empty_rss.xml"
37
- @info = Traffic.from(:file_index)
38
- end
39
-
40
- it "should indicate that there is no traffic" do
41
- @info.should_not be_traffic
42
- end
43
- end
44
-
45
- context "when there is traffic" do
46
- before :each do
47
- stub_feed "file_index/rss.xml"
48
- @info = Traffic.from(:file_index)
49
- end
50
-
51
- it "should indicate that there is traffic" do
52
- @info.should be_traffic
53
- end
54
-
55
- it "should return an info object" do
56
- @info.class.should == Traffic::Info
57
- end
58
-
59
- it "should be able to return the total size" do
60
- @info.size.should == 45
61
- end
62
-
63
- it "should be able to return the total count" do
64
- @info.count.should == 14
65
- end
66
-
67
- it "should return a list with info items with the same length as count" do
68
- @info.items.size.should == @info.count
69
- end
70
-
71
- it "should return a list with info items" do
72
- @info.items.each { |i| i.class.should == Traffic::InfoItem }
73
- end
74
-
75
- context "the info items in the list" do
76
- before :each do
77
- @info_item = @info.items.first
78
- end
79
-
80
- it "should include a from" do
81
- @info_item.from.should == "Amsterdam"
82
- end
83
-
84
- it "should include a to" do
85
- @info_item.to.should == "Amersfoort"
86
- end
87
-
88
- it "should include a road number" do
89
- @info_item.road.should == "A1"
90
- end
91
-
92
- it "should include a length in km" do
93
- @info_item.length.should == 2.6
94
- end
95
-
96
- it "should include a description" do
97
- @info_item.description.should == "Langzaam rijdend verkeer"
98
- end
99
-
100
- it "should include a location" do
101
- @info_item.location.should == "Eemnes en Eembrugge"
102
- end
103
-
104
- it "should include a cause" do
105
- @info_item.cause.should == "Ongeval(len)"
106
- end
107
-
108
- it "should include a status" do
109
- @info_item.status.should == "Deze file wordt langer."
110
- end
111
-
112
- it "should include a from location (hecto markers)" do
113
- @info_item.from_location.should == 30.2
114
- end
115
-
116
- it "should include a to location" do
117
- @info_item.to_location.should == 32.8
118
- end
119
- end
120
- end
121
33
  end
122
34
 
123
35
  end
@@ -22,5 +22,7 @@ Gem::Specification.new do |s|
22
22
  s.add_development_dependency('mocha', '~> 0.10')
23
23
  s.add_development_dependency('simplecov', '~> 0.5')
24
24
  s.add_development_dependency('fakeweb', '~> 1.3')
25
- s.add_dependency('feedzirra', '~> 0.1.1')
25
+ s.add_dependency('feedzirra', '~> 0.1')
26
+ s.add_dependency('httpclient', '~> 2.2')
27
+ s.add_dependency('nokogiri', '~> 1.4')
26
28
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: traffic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
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-12-23 00:00:00.000000000 Z
12
+ date: 2012-01-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
16
- requirement: &70310037002700 !ruby/object:Gem::Requirement
16
+ requirement: &70206768921240 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '1.0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70310037002700
24
+ version_requirements: *70206768921240
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &70310037001620 !ruby/object:Gem::Requirement
27
+ requirement: &70206768920680 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0.9'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70310037001620
35
+ version_requirements: *70206768920680
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &70310037000800 !ruby/object:Gem::Requirement
38
+ requirement: &70206768920140 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '2.7'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70310037000800
46
+ version_requirements: *70206768920140
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: mocha
49
- requirement: &70310036999020 !ruby/object:Gem::Requirement
49
+ requirement: &70206768919580 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0.10'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70310036999020
57
+ version_requirements: *70206768919580
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: simplecov
60
- requirement: &70310036998040 !ruby/object:Gem::Requirement
60
+ requirement: &70206768918960 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0.5'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70310036998040
68
+ version_requirements: *70206768918960
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: fakeweb
71
- requirement: &70310036997360 !ruby/object:Gem::Requirement
71
+ requirement: &70206768918320 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ~>
@@ -76,18 +76,40 @@ dependencies:
76
76
  version: '1.3'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *70310036997360
79
+ version_requirements: *70206768918320
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: feedzirra
82
- requirement: &70310036996600 !ruby/object:Gem::Requirement
82
+ requirement: &70206768917600 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ~>
86
86
  - !ruby/object:Gem::Version
87
- version: 0.1.1
87
+ version: '0.1'
88
88
  type: :runtime
89
89
  prerelease: false
90
- version_requirements: *70310036996600
90
+ version_requirements: *70206768917600
91
+ - !ruby/object:Gem::Dependency
92
+ name: httpclient
93
+ requirement: &70206768916920 !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ~>
97
+ - !ruby/object:Gem::Version
98
+ version: '2.2'
99
+ type: :runtime
100
+ prerelease: false
101
+ version_requirements: *70206768916920
102
+ - !ruby/object:Gem::Dependency
103
+ name: nokogiri
104
+ requirement: &70206768915980 !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: '1.4'
110
+ type: :runtime
111
+ prerelease: false
112
+ version_requirements: *70206768915980
91
113
  description: Traffic is a gem that supplies traffic information from multiple data
92
114
  providers
93
115
  email:
@@ -104,10 +126,15 @@ files:
104
126
  - lib/traffic.rb
105
127
  - lib/traffic/info.rb
106
128
  - lib/traffic/provider.rb
129
+ - lib/traffic/providers/anwb.rb
107
130
  - lib/traffic/providers/file_index.rb
108
131
  - lib/traffic/version.rb
132
+ - spec/fixtures/anwb/empty.html
133
+ - spec/fixtures/anwb/traffic.html
109
134
  - spec/fixtures/file_index/empty_rss.xml
110
135
  - spec/fixtures/file_index/rss.xml
136
+ - spec/providers/anwb_spec.rb
137
+ - spec/providers/file_index_spec.rb
111
138
  - spec/spec_helper.rb
112
139
  - spec/traffic/info_spec.rb
113
140
  - spec/traffic/providers_spec.rb
@@ -138,8 +165,12 @@ signing_key:
138
165
  specification_version: 3
139
166
  summary: ! 'Traffic: the traffic information gem'
140
167
  test_files:
168
+ - spec/fixtures/anwb/empty.html
169
+ - spec/fixtures/anwb/traffic.html
141
170
  - spec/fixtures/file_index/empty_rss.xml
142
171
  - spec/fixtures/file_index/rss.xml
172
+ - spec/providers/anwb_spec.rb
173
+ - spec/providers/file_index_spec.rb
143
174
  - spec/spec_helper.rb
144
175
  - spec/traffic/info_spec.rb
145
176
  - spec/traffic/providers_spec.rb