bus_tracker 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 [Mark Connell]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # Bustracker
2
+
3
+ Edinburgh BusTracker via Ruby.
4
+
5
+ An attempt at providing an object-based interface for interacting with the BusTracker data.
6
+
7
+ # Examples
8
+
9
+ ## Service Information
10
+ Find all of the bus stops for a particular service:
11
+ service = BusTracker.service('N30')
12
+
13
+ service.number
14
+ #=> 'N30'
15
+
16
+ After fetching a service, you can also pull up information about each stop for that service:
17
+
18
+ bus_stop = service.bus_stops.first
19
+
20
+ bus_stop.code
21
+ > '36232545'
22
+
23
+ bus_stop.name
24
+ > 'Castle Street'
25
+
26
+ bus_stop.latitude
27
+ > "55.951084136963"
28
+ bus_stop.longitude
29
+ > "-3.2017660140991"
30
+
31
+ bus_stop.service_numbers
32
+ > ["1", "3", "3A", "4", "22", "25", "X25", "30", "33", "34", "44", "44A", "N22", "N25", "N30", "N44"]
33
+
34
+ ## Departures
35
+ If you know the code for a bus stop you can fetch the associated departures. This is the same information that
36
+ you'd see on the bus tracker boards, but instantiated as their own departure objects.
37
+ stop = BusTracker.bus_stop('36232545')
38
+ departure = stop.departures.first
39
+
40
+ departure.service_number
41
+ #=> 30
42
+ departure.destination
43
+ #=> MUSSELBURGH
44
+ departure.due
45
+ #=> 7
46
+
47
+ For any BusTracker::BusStop instance, you can fetch the latest departure times:
48
+ stop.fetch_departures!
49
+ stop.departures.first.due
50
+ #=> 5
51
+
52
+ Copyright (c) 2010 [Mark Connell], released under the MIT license
@@ -0,0 +1,24 @@
1
+ require 'nokogiri'
2
+ require 'open-uri'
3
+
4
+ require 'bus_tracker/service'
5
+ require 'bus_tracker/bus_stop'
6
+ require 'bus_tracker/departure'
7
+
8
+ module BusTracker
9
+
10
+ BASE_URI = 'http://mybustracker.co.uk/'
11
+
12
+ def self.service(number)
13
+ service = BusTracker::Service.new(number)
14
+ service.fetch_bus_stops!
15
+ service
16
+ end
17
+
18
+ def self.bus_stop(code)
19
+ bus_stop = BusTracker::BusStop.new(:code => code)
20
+ bus_stop.fetch_departures!
21
+ bus_stop
22
+ end
23
+
24
+ end
@@ -0,0 +1,25 @@
1
+ module BusTracker
2
+ class BusStop
3
+ attr_accessor :name, :code, :latitude, :longitude, :service_numbers, :departures
4
+
5
+ def initialize(options = {})
6
+ self.service_numbers = []
7
+ self.departures = []
8
+
9
+ options.each_pair do |option, value|
10
+ self.send("#{option}=", value) if self.respond_to? "#{option}="
11
+ end
12
+ end
13
+
14
+ def fetch_departures!
15
+ self.departures = []
16
+
17
+ uri = "#{BASE_URI}getBusStopDepartures.php?refreshCount=0&clientType=b&busStopCode=#{code}"
18
+ uri += "&busStopDay=0&busStopService=0&numberOfPassage=4&busStopTime=&busStopDestination=0"
19
+ document = Nokogiri::HTML(open(uri))
20
+ document.xpath('//pre').each do |departure|
21
+ self.departures << BusTracker::Departure.new(departure.content)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,17 @@
1
+ module BusTracker
2
+ class Departure
3
+ attr_accessor :service_number, :destination, :due
4
+
5
+ def initialize(raw_string)
6
+ set_attributes(raw_string)
7
+ end
8
+
9
+ private
10
+ def set_attributes(raw_string)
11
+ attributes = raw_string.split(/(\w*\d+\w*)\s*([a-z]+\s*(?!DUE)[a-z]*\s*(?!DUE)[a-z]+)\s+((DUE|\**[0-9:]*))\s*/i) - ['']
12
+ self.service_number = attributes[0]
13
+ self.destination = attributes[1]
14
+ self.due = attributes[2]
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,26 @@
1
+ module BusTracker
2
+ class Service
3
+ attr_accessor :number, :bus_stops
4
+
5
+ def initialize(number)
6
+ self.number = number
7
+ self.bus_stops = []
8
+ end
9
+
10
+ def fetch_bus_stops!
11
+ self.bus_stops = []
12
+
13
+ document = Nokogiri::HTML(open("#{BASE_URI}getServicePoints.php?serviceMnemo=#{self.number}"))
14
+ bus_stops = document.xpath('//map/markers/busstop')
15
+ bus_stops.each do |bus_stop|
16
+ self.bus_stops << BusTracker::BusStop.new(
17
+ :code => bus_stop.xpath('sms')[0].content,
18
+ :name => bus_stop.xpath('nom')[0].content,
19
+ :latitude => bus_stop.xpath('x')[0].content,
20
+ :longitude => bus_stop.xpath('y')[0].content,
21
+ :service_numbers => bus_stop.xpath('services/service/mnemo').map {|s| s.content}
22
+ )
23
+ end
24
+ end
25
+ end
26
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bus_tracker
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 0
9
+ version: 1.0.0
10
+ platform: ruby
11
+ authors:
12
+ - Mark Connell
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-07-03 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: activesupport
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: nokogiri
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 1
43
+ - 4
44
+ - 1
45
+ version: 1.4.1
46
+ type: :runtime
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: fakeweb
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ type: :development
60
+ version_requirements: *id003
61
+ description: A ruby interface for accessing service and bus stop information provided by the BusTracker service for Edinburgh's public transport.
62
+ email:
63
+ - mark@markconnell.co.uk
64
+ executables: []
65
+
66
+ extensions: []
67
+
68
+ extra_rdoc_files: []
69
+
70
+ files:
71
+ - MIT-LICENSE
72
+ - README.md
73
+ - lib/bus_tracker/bus_stop.rb
74
+ - lib/bus_tracker/departure.rb
75
+ - lib/bus_tracker/service.rb
76
+ - lib/bus_tracker.rb
77
+ has_rdoc: true
78
+ homepage: http://github.com/mconnell/bus_tracker
79
+ licenses: []
80
+
81
+ post_install_message:
82
+ rdoc_options: []
83
+
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ segments:
92
+ - 0
93
+ version: "0"
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ segments:
100
+ - 1
101
+ - 3
102
+ - 6
103
+ version: 1.3.6
104
+ requirements: []
105
+
106
+ rubyforge_project:
107
+ rubygems_version: 1.3.7
108
+ signing_key:
109
+ specification_version: 3
110
+ summary: Access Edinburgh's bus tracker service via ruby.
111
+ test_files: []
112
+