bysykkel 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ .bundle/*
2
+ pkg/*
data/CHANGES ADDED
@@ -0,0 +1,19 @@
1
+ * 0.4.1
2
+ Tests runs.
3
+
4
+ * 0.4.0
5
+ Refactored to rack.
6
+
7
+ * 0.3.0
8
+ Using Typheous to gather all stations.
9
+ Not returning numbers in name.
10
+ * 0.2.0
11
+ Refactored. Renamed to bysykkel.
12
+ Added station.all
13
+
14
+ * 0.1.4
15
+ Return station
16
+ Turning string in xml into xml
17
+
18
+ * 0.1.0
19
+ Initial version
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ # Edit this Gemfile to bundle your application's dependencies.
2
+ source 'http://gemcutter.org'
3
+
4
+ gem 'nokogiri', '1.4.1', :require => 'nokogiri'
5
+ gem 'typhoeus', '0.1.31'
6
+
7
+ group :test do
8
+ gem 'rspec', '1.3.0'
9
+ end
10
+
11
+ group :development do
12
+ gem 'jeweler'
13
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,26 @@
1
+ GEM
2
+ remote: http://gemcutter.org/
3
+ specs:
4
+ gemcutter (0.6.1)
5
+ git (1.2.5)
6
+ jeweler (1.4.0)
7
+ gemcutter (>= 0.1.0)
8
+ git (>= 1.2.5)
9
+ rubyforge (>= 2.0.0)
10
+ json_pure (1.4.6)
11
+ nokogiri (1.4.1)
12
+ rack (1.2.1)
13
+ rspec (1.3.0)
14
+ rubyforge (2.0.4)
15
+ json_pure (>= 1.1.7)
16
+ typhoeus (0.1.31)
17
+ rack
18
+
19
+ PLATFORMS
20
+ ruby
21
+
22
+ DEPENDENCIES
23
+ jeweler
24
+ nokogiri (= 1.4.1)
25
+ rspec (= 1.3.0)
26
+ typhoeus (= 0.1.31)
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2010 Rune Botten & Espen Antonsen
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ 'Software'), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,27 @@
1
+ = Bysykkel API
2
+
3
+ == Description
4
+
5
+ Query the Clear Channel XML api to get a live overview of availability of city bikes in Oslo.
6
+
7
+ == TODO
8
+ * More tests.
9
+ * Handle http errors (404, 500)
10
+
11
+ == Note on Patches/Pull Requests
12
+
13
+ * Fork the project.
14
+ * Add tests in spec/
15
+ * Make your feature addition or bug fix.
16
+ * Commit, do not mess with Rakefile or VERSION. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
17
+ * Send me a pull request. Bonus points for topic branches.
18
+
19
+ == Running tests
20
+
21
+ rake
22
+
23
+ If you get errors about missing gems - `bundle install` should get you sorted.
24
+
25
+ == Copyright
26
+
27
+ Copyright (c) 2010 Espen Antonsen & Rune Botten. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,48 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "bysykkel"
8
+ gem.summary = %Q{Oslo City Bike (Bysykkel) Ruby API}
9
+ gem.description = %Q{Query the Oslo City Bike availability API at clearchannel.no with Ruby. Not affilliated with ClearChannel.}
10
+ gem.email = "espen@inspired.no"
11
+ gem.homepage = "http://github.com/espen/bysykkel"
12
+ gem.authors = ["Espen Antonsen & Rune Botten"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ gem.add_dependency('nokogiri', '>= 1.4.1')
15
+ gem.add_dependency('typhoeus', '>= 0.1.31')
16
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
17
+ end
18
+ Jeweler::GemcutterTasks.new
19
+ rescue LoadError
20
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
21
+ end
22
+
23
+ require 'spec/rake/spectask'
24
+ Spec::Rake::SpecTask.new(:spec) do |spec|
25
+ spec.libs << 'lib' << 'spec'
26
+ spec.spec_files = FileList['spec/**/*_spec.rb']
27
+ spec.spec_opts << '-O spec/spec.opts'
28
+ end
29
+
30
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
31
+ spec.libs << 'lib' << 'spec'
32
+ spec.pattern = 'spec/**/*_spec.rb'
33
+ spec.rcov = true
34
+ end
35
+
36
+ task :spec => :check_dependencies
37
+
38
+ task :default => :spec
39
+
40
+ require 'rake/rdoctask'
41
+ Rake::RDocTask.new do |rdoc|
42
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
43
+
44
+ rdoc.rdoc_dir = 'rdoc'
45
+ rdoc.title = "bysykkel #{version}"
46
+ rdoc.rdoc_files.include('README*')
47
+ rdoc.rdoc_files.include('lib/**/*.rb')
48
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.4.1
@@ -0,0 +1,70 @@
1
+ module Bysykkel
2
+
3
+ class Error < StandardError;end
4
+ class BadRequest < Error;end
5
+
6
+ class Rack
7
+ RACKS_URL = 'http://smartbikeportal.clearchannel.no/public/mobapp/maq.asmx/getRacks'
8
+ RACK_URL = 'http://smartbikeportal.clearchannel.no/public/mobapp/maq.asmx/getRack?id=%s'
9
+
10
+ attr_accessor :id, :name, :empty_locks, :ready_bikes, :online, :lat, :lng
11
+
12
+ def initialize(attrs = {})
13
+ attrs.each do |k,v|
14
+ self.__send__("#{k}=", v)
15
+ end
16
+ end
17
+
18
+ # Query the Bysykkel XML API @ clearchannel.no for racks.
19
+ def self.all()
20
+ raw = open(RACKS_URL)
21
+ doc = Nokogiri::XML.parse raw
22
+ racks_xml = Nokogiri::XML('<stations>' + doc.children[0].children[0].text + '</stations>')
23
+ racks = racks_xml.xpath('//station').children.inject([]) do |ary, rack|
24
+ ary << Rack.new({
25
+ :id => rack.text.to_i
26
+ }) unless rack.text.to_i >= 500
27
+ ary
28
+ end
29
+
30
+
31
+ hydra = Typhoeus::Hydra.new( :max_concurrency => 6, :initial_pool_size => 5 )
32
+ racks_all = []
33
+ racks.each do |rack|
34
+ req = Typhoeus::Request.new( RACK_URL % rack.id )
35
+ req.on_complete do |response|
36
+ doc = Nokogiri::XML.parse response.body
37
+ parsed_rack = self.parse_rack(rack.id, Nokogiri::XML(doc.children[0].children[0].text).children[0])
38
+ racks_all << parsed_rack unless parsed_rack.nil?
39
+ end
40
+ hydra.queue req
41
+ end
42
+ hydra.run
43
+ racks_all
44
+ end
45
+
46
+ # Query the Bysykkel XML API @ clearchannel.no for a rack.
47
+ def self.find(id)
48
+ raw = open(RACK_URL % id )
49
+ doc = Nokogiri::XML.parse raw
50
+ parsed_rack = self.parse_rack(id, Nokogiri::XML(doc.children[0].children[0].text).children[0])
51
+ return parsed_rack ? [parsed_rack] : []
52
+ end
53
+
54
+ private
55
+ def self.parse_rack(id, rack)
56
+ return if !rack
57
+ return if rack.xpath('online').text == ''
58
+ Rack.new( {
59
+ :id => id.to_i,
60
+ :name => rack.xpath('description').text.strip.gsub(/[0-9]+-/, ""),
61
+ :empty_locks => rack.xpath('empty_locks').text.to_i,
62
+ :ready_bikes => rack.xpath('ready_bikes').text.to_i,
63
+ :online => rack.xpath('online').text == '1' ? true : false,
64
+ :lat => rack.xpath('latitude').text,
65
+ :lng => rack.xpath('longitute').text
66
+ } )
67
+ end
68
+
69
+ end
70
+ end
data/lib/bysykkel.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'open-uri'
2
+
3
+ require 'rubygems'
4
+ require 'nokogiri'
5
+ require 'typhoeus'
6
+
7
+ require File.dirname(__FILE__) + '/bysykkel/rack'
8
+
9
+ module Bysykkel;end
@@ -0,0 +1,50 @@
1
+ #:encoding:utf-8
2
+
3
+ require File.dirname(__FILE__) + '/../../lib/bysykkel'
4
+
5
+ describe Bysykkel::Rack do
6
+ context 'view' do
7
+ it 'view by id and returns rack' do
8
+ racks = Bysykkel::Rack.find(1)
9
+ racks.size.should == 1
10
+
11
+ # Test first
12
+ rack = racks[0]
13
+ rack.name.should == "Middelthunsgate (vis-a-vis nr. 21, retning Kirkeveien)"
14
+ rack.id.should == 1
15
+
16
+ end
17
+
18
+ it 'returns an empty array when searching for racks and did not find any' do
19
+ rack = Bysykkel::Rack.find(289282)
20
+ rack.should == []
21
+ end
22
+
23
+ end
24
+
25
+ context 'all' do
26
+ it 'view all and returns rack' do
27
+ racks = Bysykkel::Rack.all
28
+ racks.size.should == 102
29
+
30
+ # Test first
31
+ rack = racks[0]
32
+ rack.id.should == 1
33
+
34
+ end
35
+
36
+ end
37
+
38
+ context 'geodata' do
39
+ it 'has latitude and longitude' do
40
+ racks = Bysykkel::Rack.find(1)
41
+
42
+ # Test first
43
+ rack = racks.first
44
+ rack.name.should == "Middelthunsgate (vis-a-vis nr. 21, retning Kirkeveien)"
45
+ rack.lat.should == '59.92786125852981'
46
+ rack.lng.should == '10.709009170532226'
47
+ end
48
+ end
49
+
50
+ end
@@ -0,0 +1,2 @@
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+ <string xmlns="http://smartbikeportal.clearchannel.no/public/mobapp/">&lt;station&gt;&lt;ready_bikes&gt;15&lt;/ready_bikes&gt;&lt;empty_locks&gt;3&lt;/empty_locks&gt;&lt;online&gt;1&lt;/online&gt;&lt;description&gt;01-Middelthunsgate (vis-a-vis nr. 21, retning Kirkeveien)&lt;/description&gt;&lt;longitute&gt;10.709009170532226&lt;/longitute&gt;&lt;latitude&gt;59.92786125852981&lt;/latitude&gt;&lt;/station&gt;</string>
@@ -0,0 +1,115 @@
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+ <string xmlns="http://smartbikeportal.clearchannel.no/public/mobapp/">&lt;station&gt;1&lt;/station&gt;
3
+ &lt;station&gt;2&lt;/station&gt;
4
+ &lt;station&gt;3&lt;/station&gt;
5
+ &lt;station&gt;4&lt;/station&gt;
6
+ &lt;station&gt;5&lt;/station&gt;
7
+ &lt;station&gt;6&lt;/station&gt;
8
+ &lt;station&gt;7&lt;/station&gt;
9
+ &lt;station&gt;8&lt;/station&gt;
10
+ &lt;station&gt;9&lt;/station&gt;
11
+ &lt;station&gt;10&lt;/station&gt;
12
+ &lt;station&gt;11&lt;/station&gt;
13
+ &lt;station&gt;12&lt;/station&gt;
14
+ &lt;station&gt;13&lt;/station&gt;
15
+ &lt;station&gt;14&lt;/station&gt;
16
+ &lt;station&gt;15&lt;/station&gt;
17
+ &lt;station&gt;16&lt;/station&gt;
18
+ &lt;station&gt;17&lt;/station&gt;
19
+ &lt;station&gt;18&lt;/station&gt;
20
+ &lt;station&gt;19&lt;/station&gt;
21
+ &lt;station&gt;20&lt;/station&gt;
22
+ &lt;station&gt;21&lt;/station&gt;
23
+ &lt;station&gt;22&lt;/station&gt;
24
+ &lt;station&gt;23&lt;/station&gt;
25
+ &lt;station&gt;24&lt;/station&gt;
26
+ &lt;station&gt;25&lt;/station&gt;
27
+ &lt;station&gt;26&lt;/station&gt;
28
+ &lt;station&gt;27&lt;/station&gt;
29
+ &lt;station&gt;28&lt;/station&gt;
30
+ &lt;station&gt;29&lt;/station&gt;
31
+ &lt;station&gt;30&lt;/station&gt;
32
+ &lt;station&gt;31&lt;/station&gt;
33
+ &lt;station&gt;32&lt;/station&gt;
34
+ &lt;station&gt;33&lt;/station&gt;
35
+ &lt;station&gt;34&lt;/station&gt;
36
+ &lt;station&gt;35&lt;/station&gt;
37
+ &lt;station&gt;36&lt;/station&gt;
38
+ &lt;station&gt;37&lt;/station&gt;
39
+ &lt;station&gt;38&lt;/station&gt;
40
+ &lt;station&gt;39&lt;/station&gt;
41
+ &lt;station&gt;40&lt;/station&gt;
42
+ &lt;station&gt;41&lt;/station&gt;
43
+ &lt;station&gt;42&lt;/station&gt;
44
+ &lt;station&gt;43&lt;/station&gt;
45
+ &lt;station&gt;44&lt;/station&gt;
46
+ &lt;station&gt;45&lt;/station&gt;
47
+ &lt;station&gt;46&lt;/station&gt;
48
+ &lt;station&gt;47&lt;/station&gt;
49
+ &lt;station&gt;48&lt;/station&gt;
50
+ &lt;station&gt;49&lt;/station&gt;
51
+ &lt;station&gt;50&lt;/station&gt;
52
+ &lt;station&gt;51&lt;/station&gt;
53
+ &lt;station&gt;52&lt;/station&gt;
54
+ &lt;station&gt;53&lt;/station&gt;
55
+ &lt;station&gt;54&lt;/station&gt;
56
+ &lt;station&gt;55&lt;/station&gt;
57
+ &lt;station&gt;57&lt;/station&gt;
58
+ &lt;station&gt;58&lt;/station&gt;
59
+ &lt;station&gt;59&lt;/station&gt;
60
+ &lt;station&gt;60&lt;/station&gt;
61
+ &lt;station&gt;61&lt;/station&gt;
62
+ &lt;station&gt;62&lt;/station&gt;
63
+ &lt;station&gt;63&lt;/station&gt;
64
+ &lt;station&gt;64&lt;/station&gt;
65
+ &lt;station&gt;65&lt;/station&gt;
66
+ &lt;station&gt;66&lt;/station&gt;
67
+ &lt;station&gt;67&lt;/station&gt;
68
+ &lt;station&gt;68&lt;/station&gt;
69
+ &lt;station&gt;69&lt;/station&gt;
70
+ &lt;station&gt;70&lt;/station&gt;
71
+ &lt;station&gt;71&lt;/station&gt;
72
+ &lt;station&gt;72&lt;/station&gt;
73
+ &lt;station&gt;73&lt;/station&gt;
74
+ &lt;station&gt;74&lt;/station&gt;
75
+ &lt;station&gt;75&lt;/station&gt;
76
+ &lt;station&gt;76&lt;/station&gt;
77
+ &lt;station&gt;77&lt;/station&gt;
78
+ &lt;station&gt;78&lt;/station&gt;
79
+ &lt;station&gt;79&lt;/station&gt;
80
+ &lt;station&gt;80&lt;/station&gt;
81
+ &lt;station&gt;81&lt;/station&gt;
82
+ &lt;station&gt;82&lt;/station&gt;
83
+ &lt;station&gt;83&lt;/station&gt;
84
+ &lt;station&gt;84&lt;/station&gt;
85
+ &lt;station&gt;85&lt;/station&gt;
86
+ &lt;station&gt;86&lt;/station&gt;
87
+ &lt;station&gt;87&lt;/station&gt;
88
+ &lt;station&gt;88&lt;/station&gt;
89
+ &lt;station&gt;89&lt;/station&gt;
90
+ &lt;station&gt;90&lt;/station&gt;
91
+ &lt;station&gt;91&lt;/station&gt;
92
+ &lt;station&gt;92&lt;/station&gt;
93
+ &lt;station&gt;93&lt;/station&gt;
94
+ &lt;station&gt;94&lt;/station&gt;
95
+ &lt;station&gt;95&lt;/station&gt;
96
+ &lt;station&gt;96&lt;/station&gt;
97
+ &lt;station&gt;97&lt;/station&gt;
98
+ &lt;station&gt;98&lt;/station&gt;
99
+ &lt;station&gt;99&lt;/station&gt;
100
+ &lt;station&gt;100&lt;/station&gt;
101
+ &lt;station&gt;102&lt;/station&gt;
102
+ &lt;station&gt;103&lt;/station&gt;
103
+ &lt;station&gt;104&lt;/station&gt;
104
+ &lt;station&gt;105&lt;/station&gt;
105
+ &lt;station&gt;106&lt;/station&gt;
106
+ &lt;station&gt;107&lt;/station&gt;
107
+ &lt;station&gt;108&lt;/station&gt;
108
+ &lt;station&gt;500&lt;/station&gt;
109
+ &lt;station&gt;501&lt;/station&gt;
110
+ &lt;station&gt;502&lt;/station&gt;
111
+ &lt;station&gt;503&lt;/station&gt;
112
+ &lt;station&gt;504&lt;/station&gt;
113
+ &lt;station&gt;597&lt;/station&gt;
114
+ &lt;station&gt;598&lt;/station&gt;
115
+ </string>
data/spec/options ADDED
@@ -0,0 +1,3 @@
1
+ -c
2
+ -f
3
+ specdoc
data/spec/spec.opts ADDED
@@ -0,0 +1,3 @@
1
+ -f
2
+ nested
3
+ -c
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bysykkel
3
+ version: !ruby/object:Gem::Version
4
+ hash: 13
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 4
9
+ - 1
10
+ version: 0.4.1
11
+ platform: ruby
12
+ authors:
13
+ - Espen Antonsen & Rune Botten
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-10-02 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 13
30
+ segments:
31
+ - 1
32
+ - 2
33
+ - 9
34
+ version: 1.2.9
35
+ type: :development
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: nokogiri
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 5
46
+ segments:
47
+ - 1
48
+ - 4
49
+ - 1
50
+ version: 1.4.1
51
+ type: :runtime
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: typhoeus
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 37
62
+ segments:
63
+ - 0
64
+ - 1
65
+ - 31
66
+ version: 0.1.31
67
+ type: :runtime
68
+ version_requirements: *id003
69
+ description: Query the Oslo City Bike availability API at clearchannel.no with Ruby. Not affilliated with ClearChannel.
70
+ email: espen@inspired.no
71
+ executables: []
72
+
73
+ extensions: []
74
+
75
+ extra_rdoc_files:
76
+ - LICENSE
77
+ - README.rdoc
78
+ files:
79
+ - .gitignore
80
+ - CHANGES
81
+ - Gemfile
82
+ - Gemfile.lock
83
+ - LICENSE
84
+ - README.rdoc
85
+ - Rakefile
86
+ - VERSION
87
+ - lib/bysykkel.rb
88
+ - lib/bysykkel/rack.rb
89
+ - spec/bysykkel/rack_spec.rb
90
+ - spec/fixtures/rack.xml
91
+ - spec/fixtures/racks.xml
92
+ - spec/options
93
+ - spec/spec.opts
94
+ has_rdoc: true
95
+ homepage: http://github.com/espen/bysykkel
96
+ licenses: []
97
+
98
+ post_install_message:
99
+ rdoc_options:
100
+ - --charset=UTF-8
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ hash: 3
109
+ segments:
110
+ - 0
111
+ version: "0"
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ hash: 3
118
+ segments:
119
+ - 0
120
+ version: "0"
121
+ requirements: []
122
+
123
+ rubyforge_project:
124
+ rubygems_version: 1.3.7
125
+ signing_key:
126
+ specification_version: 3
127
+ summary: Oslo City Bike (Bysykkel) Ruby API
128
+ test_files:
129
+ - spec/bysykkel/rack_spec.rb