traffic 0.0.1

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/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ coverage
2
+ *.gem
3
+ .bundle
4
+ Gemfile.lock
5
+ pkg/*
6
+ .DS_Store
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm ruby-1.9.3-p0@traffic
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in traffic.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ Traffic: the traffic information gem
2
+ ====================================
3
+
4
+ Traffic is a gem that supplies traffic information from multiple data providers
5
+
6
+ Installation
7
+ ------------
8
+ gem install traffic
9
+
10
+ Usage Examples
11
+ --------------
12
+ require "traffic"
13
+
14
+ info = Traffic.from(:file_index)
15
+
16
+ puts info.count
17
+ #=> 15
18
+ puts "#{info.size} km"
19
+ #=> "45 km"
20
+
21
+ info_item = info.items.first
22
+
23
+ puts "#{info_item.from} to #{info_item.to}
24
+ #=> "Amsterdam to Amersfoort"
25
+ puts info_item.road
26
+ #=> "A1"
27
+ puts "#{info_item.length} km"
28
+ #=> "2.8 km"
29
+ puts "#{info_item.description} - #{info_item.location} - #{info_item.cause}"
30
+ #=> "stilstaand verkeerd - Eemnes en Eembrugge - defecte vrachtwagen"
31
+ puts "#{info_item.from_location} - #{info_item.to_location}"
32
+ #=> "40.0 - 42.8"
33
+
34
+ Providers
35
+ ---------
36
+ For now it only includes a provider for the Dutch traffic information from [fileindex.nl](http://www.fileindex.nl/).
37
+
38
+ Testing
39
+ -------
40
+ Run all tests:
41
+
42
+ rspec spec/
43
+
44
+ Copyright
45
+ ---------
46
+ Copyright (c) 2011 Daniël van Hoesel.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,21 @@
1
+ module Traffic
2
+ MAIN_ATTRIBUTES = [:timestamp, :count, :size, :traffic]
3
+ Info = Struct.new(*MAIN_ATTRIBUTES) do
4
+ def traffic?
5
+ !traffic
6
+ end
7
+
8
+ def items
9
+ @items ||= []
10
+ end
11
+ end
12
+
13
+ ITEM_ATTRIBUTES = [:road, :from, :to, :from_location, :to_location, :location, :length, :description, :cause, :status]
14
+ InfoItem = Struct.new(*ITEM_ATTRIBUTES) do
15
+ ITEM_ATTRIBUTES.each do |attr|
16
+ define_method "#{attr}?" do
17
+ !!self.send(attr)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,12 @@
1
+ # require all provider source files
2
+ Dir[File.expand_path("../providers/**/*.rb", __FILE__)].each {|f| require f}
3
+
4
+ module Traffic
5
+ def self.Provider(name)
6
+ # camelcase the name
7
+ Traffic::Providers.const_get "#{name.to_s.gsub(/(?:^|_)(.)/) { $1.upcase }}"
8
+ rescue NameError => e
9
+ raise "unknown provider: #{name}"
10
+ end
11
+ end
12
+
@@ -0,0 +1,51 @@
1
+ require 'feedzirra'
2
+
3
+ module Traffic
4
+ module Providers
5
+ class FileIndex
6
+ RSS_FEED = "http://www.fileindex.nl/rss.php"
7
+
8
+ def traffic
9
+ rss.description =~ /Er zijn geen files./
10
+ end
11
+
12
+ def timestamp
13
+ Time.parse(rss.title.gsub(/Fileindex.nl actuele files /, ""))
14
+ end
15
+
16
+ def count
17
+ rss.description[/(\d+) files?.+van .+ km./, 1].to_i
18
+ end
19
+
20
+ def size
21
+ rss.description[/\d+ files?.+van (.+) km./, 1].to_i
22
+ end
23
+
24
+ def each_item(&block)
25
+ rss.entries.each do |entry|
26
+ item = Struct.new(:road, :from, :to, :from_location, :to_location, :location, :length, :description, :cause, :status).new
27
+ data = entry.title.match /(.+?) van (.+) richting (.+) (.+) km/
28
+ item.road = data[1]
29
+ item.from = data[2]
30
+ item.to = data[3]
31
+ item.length = data[4].to_f
32
+
33
+ data = entry.summary.match /\b(.+)\b tussen \b(.+)\b( door (.*))? HMP([\d\.]+) .+ HMP([\d\.]+)\s*(.*)/
34
+ item.description = data[1]
35
+ item.location = data[2]
36
+ item.cause = data[4]
37
+ item.from_location = data[5].to_f
38
+ item.to_location = data[6].to_f
39
+ item.status = data[7]
40
+
41
+ yield item
42
+ end
43
+ end
44
+
45
+ private
46
+ def rss
47
+ @data ||= Feedzirra::Feed.fetch_and_parse(RSS_FEED)
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,3 @@
1
+ module Traffic
2
+ VERSION = "0.0.1"
3
+ end
data/lib/traffic.rb ADDED
@@ -0,0 +1,32 @@
1
+ require File.expand_path("../traffic/version", __FILE__)
2
+ require File.expand_path("../traffic/provider", __FILE__)
3
+ require File.expand_path("../traffic/info", __FILE__)
4
+
5
+ module Traffic
6
+ def self.from(provider_name)
7
+ provider = Provider(provider_name).new
8
+ info = Info.new
9
+
10
+ MAIN_ATTRIBUTES.each do |attr|
11
+ if provider.respond_to?(attr)
12
+ info.send("#{attr}=", provider.send(attr))
13
+ else
14
+ raise "attribute missing: #{attr}. the selected provider ('#{provider_name}') does not supply all required attributes"
15
+ end
16
+ end
17
+
18
+ provider.each_item do |item|
19
+ info_item = InfoItem.new
20
+ ITEM_ATTRIBUTES.each do |attr|
21
+ if item.respond_to?(attr)
22
+ info_item.send("#{attr}=", item.send(attr))
23
+ else
24
+ raise "attribute missing: #{attr}. the selected provider item ('#{provider_name}') does not supply all required attributes"
25
+ end
26
+ end
27
+ info.items << info_item
28
+ end
29
+
30
+ info
31
+ end
32
+ end
@@ -0,0 +1 @@
1
+ <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>Fileindex.nl actuele files 22-dec-2011 22:11:46</title><link>http://www.fileindex.nl/meldingenRSS.aspx</link><description>Er zijn geen files.</description><language>en-us</language></channel></rss>
@@ -0,0 +1,23 @@
1
+
2
+ <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>Fileindex.nl actuele files 22-dec-2011 16:15:52</title><link>http://www.fileindex.nl/meldingenRSS.aspx</link><description>Er zijn 14 files met een totale lengte van 45 km.</description><language>en-us</language><item><title>A1 van Amsterdam richting Amersfoort 2.6 km
3
+ </title><description>Langzaam rijdend verkeer tussen Eemnes en Eembrugge door Ongeval(len) HMP30.2 --&gt; HMP32.8 Deze file wordt langer.</description></item><item><title>A4 van Amsterdam richting Delft 7.3 km
4
+ </title><description>Stilstaand verkeer tussen Burgerveen en Brug over de Oude Rijn HMP22.6 --&gt; HMP29.9 Deze file wordt korter.
5
+ </description></item><item><title>A10 van De Nieuwe Meer richting Coenplein 3.4 km
6
+ </title><description>Stilstaand verkeer tussen Geuzenveld en Coenplein HMP25.7 --&gt; HMP29.1 Deze file wordt korter.
7
+ </description></item><item><title>A13 van Rijswijk richting Rotterdam 2.2 km
8
+ </title><description>Langzaam rijdend verkeer tussen TU Delft en Berkel en Rodenrijs HMP13.5 --&gt; HMP15.7</description></item><item><title>A15 van Ridderkerk richting Rozenburg 4 km
9
+ </title><description>Stilstaand verkeer tussen Hoogvliet en Spijkenisse HMP47.1 --&gt; HMP43.1 Deze file wordt korter.
10
+ </description></item><item><title>A15 van Ridderkerk richting Gorinchem 3.2 km
11
+ </title><description>Stilstaand verkeer tussen Alblasserdam en Sliedrecht-West HMP77.9 --&gt; HMP81.1 Deze file wordt langer.</description></item><item><title>A15 van Ridderkerk richting Gorinchem 1.5 km
12
+ </title><description>Stilstaand verkeer tussen Sliedrecht-Oost en Hardinxveld-Giessendam HMP85.5 --&gt; HMP87 Deze file wordt langer.</description></item><item><title>A16 van Breda richting Rotterdam 2.6 km
13
+ </title><description>Stilstaand verkeer tussen Feijenoord en Terbregseplein HMP20 --&gt; HMP17.4 Deze file wordt korter.
14
+ </description></item><item><title>A20 van Hoek van Holland richting Gouda 3.1 km
15
+ </title><description>Langzaam rijdend verkeer tussen Spaansepolder en Terbregseplein HMP28.5 --&gt; HMP31.6 Deze file wordt korter.
16
+ </description></item><item><title>A20 van Gouda richting Hoek van Holland 0 km
17
+ </title><description>Drie rijstroken afgesloten tussen Prins Alexander en Terbregseplein door Ongeval(len) HMP37.3 --&gt; HMP37.3 Deze file wordt langer.</description></item><item><title>A20 van Gouda richting Hoek van Holland 6.5 km
18
+ </title><description>Stilstaand verkeer tussen Moordrecht en Terbregseplein door Ongeval(len) HMP43.3 --&gt; HMP36.8 Deze file wordt langer.</description></item><item><title>A28 van Utrecht richting Amersfoort 3.1 km
19
+ </title><description>Stilstaand verkeer tussen De Uithof en Den Dolder HMP5.8 --&gt; HMP8.9 Deze file wordt korter.
20
+ </description></item><item><title>A28 van Utrecht richting Amersfoort 2.8 km
21
+ </title><description>Langzaam rijdend verkeer tussen Maarn en Leusden HMP16.2 --&gt; HMP19 Deze file wordt korter.
22
+ </description></item><item><title>A50 van Arnhem richting Oss 2.7 km
23
+ </title><description>Langzaam rijdend verkeer tussen Valburg en Ewijk HMP154.6 --&gt; HMP151.9</description></item></channel></rss>
@@ -0,0 +1,27 @@
1
+ require 'rspec'
2
+ require 'simplecov'
3
+ require 'fakeweb'
4
+ require 'pp'
5
+
6
+ SimpleCov.start do
7
+ add_group 'Traffic', 'lib/traffic'
8
+ add_group 'Specs', 'spec'
9
+ add_filter __FILE__
10
+ end
11
+
12
+ RSpec.configure do |config|
13
+ config.mock_with :mocha
14
+
15
+ config.before :each do
16
+ FakeWeb.allow_net_connect = false
17
+ Feedzirra::Feed.stubs(:fetch_and_parse).raises("do not connect to feed, stub this method")
18
+ end
19
+ end
20
+
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
+ load File.expand_path('../../lib/traffic.rb', __FILE__)
@@ -0,0 +1,17 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe Traffic do
4
+ describe "InfoItem" do
5
+
6
+ it "should have presence testers" do
7
+ item = Traffic::InfoItem.new
8
+ Traffic::ITEM_ATTRIBUTES.each do |attr|
9
+ item.send("#{attr}=", "foobar")
10
+ item.send("#{attr}?").should be_true
11
+ item.send("#{attr}=", nil)
12
+ item.send("#{attr}?").should be_false
13
+ end
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,15 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe Traffic do
4
+
5
+ describe "::Provider" do
6
+ it "should return a provider class based on a name" do
7
+ Traffic::Provider(:file_index).should == Traffic::Providers::FileIndex
8
+ end
9
+
10
+ it "should raise an exception when an unknown provider is specified" do
11
+ lambda { Traffic::Provider(:foobar) }.should raise_error
12
+ end
13
+ end
14
+
15
+ end
@@ -0,0 +1,123 @@
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+
3
+ describe Traffic do
4
+
5
+ describe ".from" do
6
+ it "should raise an exception when using an unknown provider" do
7
+ lambda { Traffic.from(:foobar) }.should raise_error
8
+ end
9
+
10
+ it "should raise an exception when using an provider with incomplete main attributes" do
11
+ module Traffic
12
+ module Providers
13
+ class EmptyProvider
14
+ end
15
+ end
16
+ end
17
+ lambda { Traffic.from(:empty_provider) }.should raise_error
18
+ end
19
+
20
+ it "should raise an exception when using an provider with incomplete item attributes" do
21
+ module Traffic
22
+ module Providers
23
+ class EmptyProvider
24
+ attr_accessor *Traffic::MAIN_ATTRIBUTES
25
+ def each_item(&block)
26
+ yield
27
+ end
28
+ end
29
+ end
30
+ end
31
+ lambda { Traffic.from(:empty_provider) }.should raise_error
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
+ end
122
+
123
+ end
data/traffic.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "traffic/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "traffic"
7
+ s.version = Traffic::VERSION
8
+ s.authors = ["Daniel van Hoesel"]
9
+ s.email = ["daniel@danielvanhoesel.nl"]
10
+ s.homepage = ""
11
+ s.summary = %q{Traffic: the traffic information gem}
12
+ s.description = %q{Traffic is a gem that supplies traffic information from multiple data providers}
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ["lib"]
18
+
19
+ s.add_development_dependency('bundler', '~> 1.0')
20
+ s.add_development_dependency('rake', '~> 0.9')
21
+ s.add_development_dependency('rspec', '~> 2.7')
22
+ s.add_development_dependency('mocha', '~> 0.10')
23
+ s.add_development_dependency('simplecov', '~> 0.5')
24
+ s.add_development_dependency('fakeweb', '~> 1.3')
25
+ s.add_dependency('feedzirra', '~> 0.1.1')
26
+ end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: traffic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Daniel van Hoesel
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-12-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: &70310037002700 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70310037002700
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &70310037001620 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '0.9'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70310037001620
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &70310037000800 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '2.7'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70310037000800
47
+ - !ruby/object:Gem::Dependency
48
+ name: mocha
49
+ requirement: &70310036999020 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '0.10'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70310036999020
58
+ - !ruby/object:Gem::Dependency
59
+ name: simplecov
60
+ requirement: &70310036998040 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: '0.5'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70310036998040
69
+ - !ruby/object:Gem::Dependency
70
+ name: fakeweb
71
+ requirement: &70310036997360 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ version: '1.3'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *70310036997360
80
+ - !ruby/object:Gem::Dependency
81
+ name: feedzirra
82
+ requirement: &70310036996600 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ~>
86
+ - !ruby/object:Gem::Version
87
+ version: 0.1.1
88
+ type: :runtime
89
+ prerelease: false
90
+ version_requirements: *70310036996600
91
+ description: Traffic is a gem that supplies traffic information from multiple data
92
+ providers
93
+ email:
94
+ - daniel@danielvanhoesel.nl
95
+ executables: []
96
+ extensions: []
97
+ extra_rdoc_files: []
98
+ files:
99
+ - .gitignore
100
+ - .rvmrc
101
+ - Gemfile
102
+ - README.md
103
+ - Rakefile
104
+ - lib/traffic.rb
105
+ - lib/traffic/info.rb
106
+ - lib/traffic/provider.rb
107
+ - lib/traffic/providers/file_index.rb
108
+ - lib/traffic/version.rb
109
+ - spec/fixtures/file_index/empty_rss.xml
110
+ - spec/fixtures/file_index/rss.xml
111
+ - spec/spec_helper.rb
112
+ - spec/traffic/info_spec.rb
113
+ - spec/traffic/providers_spec.rb
114
+ - spec/traffic_spec.rb
115
+ - traffic.gemspec
116
+ homepage: ''
117
+ licenses: []
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ none: false
124
+ requirements:
125
+ - - ! '>='
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 1.8.10
137
+ signing_key:
138
+ specification_version: 3
139
+ summary: ! 'Traffic: the traffic information gem'
140
+ test_files:
141
+ - spec/fixtures/file_index/empty_rss.xml
142
+ - spec/fixtures/file_index/rss.xml
143
+ - spec/spec_helper.rb
144
+ - spec/traffic/info_spec.rb
145
+ - spec/traffic/providers_spec.rb
146
+ - spec/traffic_spec.rb