ruby-irail 0.2.1 → 0.3.0
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/README.md +4 -0
- data/lib/ruby-irail/packages/nmbs/api.rb +27 -3
- data/lib/ruby-irail/packages/nmbs/models/stopover.rb +3 -1
- data/lib/ruby-irail/packages/nmbs/models/vehicle_information.rb +12 -0
- data/lib/ruby-irail/packages/nmbs/models/vehicle_trip.rb +13 -0
- data/lib/ruby-irail/packages/nmbs/parser/document_parser.rb +11 -0
- data/lib/version.rb +1 -1
- data/spec/packages/nmbs/api_spec.rb +28 -0
- data/spec/packages/nmbs/models/stopover_spec.rb +8 -0
- data/spec/packages/nmbs/models/vehicle_information_spec.rb +13 -0
- data/spec/packages/nmbs/models/vehicle_trip_spec.rb +13 -0
- data/spec/packages/nmbs/parser/document_parser_spec.rb +32 -0
- metadata +15 -9
data/README.md
CHANGED
@@ -40,6 +40,10 @@ train_stations = irail.stations
|
|
40
40
|
origin = train_stations.first
|
41
41
|
destination = train_stations.last
|
42
42
|
irail.connections(origin, destination)
|
43
|
+
|
44
|
+
#get train trip information (all stops for a train)
|
45
|
+
|
46
|
+
irail.vehicle("BE.NMBS.IC2240") # the vehicle ids cans be found when querying for a connection
|
43
47
|
```
|
44
48
|
|
45
49
|
## Contributing
|
@@ -3,6 +3,7 @@ module IRail::API
|
|
3
3
|
API_URL = "http://api.irail.be"
|
4
4
|
STATIONS_URI = "stations/"
|
5
5
|
CONNECTIONS_URI = "connections/"
|
6
|
+
VEHICLE_URI = "vehicle/"
|
6
7
|
|
7
8
|
def stations(options = {})
|
8
9
|
station_list_url = build_station_list_url
|
@@ -16,9 +17,10 @@ module IRail::API
|
|
16
17
|
IRail::NMBS::DocumentParser.parse_connections(xml_connections)
|
17
18
|
end
|
18
19
|
|
19
|
-
def
|
20
|
-
|
21
|
-
|
20
|
+
def vehicle(vehicle_id, options = {})
|
21
|
+
vehicle_url = build_vehicle_url
|
22
|
+
xml_vehicle = get_vehicle(vehicle_url, vehicle_id, options)
|
23
|
+
IRail::NMBS::DocumentParser.parse_vehicle(xml_vehicle)
|
22
24
|
end
|
23
25
|
|
24
26
|
def get_station_list(station_list_url, options = {})
|
@@ -26,6 +28,16 @@ module IRail::API
|
|
26
28
|
IRail::Request.get(station_list_url, options)
|
27
29
|
end
|
28
30
|
|
31
|
+
def get_connections(connections_url, origin_station, destination_station, options = {})
|
32
|
+
options = build_connections_option_hash(origin_station, destination_station, options)
|
33
|
+
IRail::Request.get(connections_url, options)
|
34
|
+
end
|
35
|
+
|
36
|
+
def get_vehicle(vehicle_url, vehicle_id, options = {})
|
37
|
+
options = build_vehicle_option_hash(vehicle_id, options)
|
38
|
+
IRail::Request.get(vehicle_url, options)
|
39
|
+
end
|
40
|
+
|
29
41
|
private
|
30
42
|
def build_connections_option_hash(origin_station, destination_station, options = {})
|
31
43
|
{
|
@@ -36,6 +48,14 @@ module IRail::API
|
|
36
48
|
}
|
37
49
|
end
|
38
50
|
|
51
|
+
def build_vehicle_option_hash(vehicle_id, options = {})
|
52
|
+
{
|
53
|
+
:query => options.merge({
|
54
|
+
:id => vehicle_id
|
55
|
+
})
|
56
|
+
}
|
57
|
+
end
|
58
|
+
|
39
59
|
def build_station_list_url
|
40
60
|
[API_URL, STATIONS_URI].join('/')
|
41
61
|
end
|
@@ -43,5 +63,9 @@ module IRail::API
|
|
43
63
|
def build_connections_url
|
44
64
|
[API_URL, CONNECTIONS_URI].join('/')
|
45
65
|
end
|
66
|
+
|
67
|
+
def build_vehicle_url
|
68
|
+
[API_URL, VEHICLE_URI].join('/')
|
69
|
+
end
|
46
70
|
end
|
47
71
|
end
|
@@ -9,12 +9,14 @@ module IRail::NMBS
|
|
9
9
|
|
10
10
|
xml_name "via"
|
11
11
|
|
12
|
-
xml_accessor :id, :from => "@id",
|
12
|
+
xml_accessor :id, :from => "@id", :as => Integer
|
13
|
+
xml_accessor :delay, :from => "@delay", :as => Integer
|
13
14
|
xml_accessor :station, :as => Station
|
14
15
|
xml_accessor :vehicle, :as => Vehicle
|
15
16
|
xml_accessor :arrival, :as => Step, :from => "arrival"
|
16
17
|
xml_accessor :departure, :as => Step, :from => "departure"
|
17
18
|
xml_accessor :direction, :as => Station
|
18
19
|
xml_reader :wait_time, :from => "timeBetween", :as => Integer
|
20
|
+
xml_accessor :unix_time, :from => "time", :as => UnixTime
|
19
21
|
end
|
20
22
|
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require "roxml"
|
2
|
+
require_relative "./vehicle.rb"
|
3
|
+
require_relative "./vehicle_trip.rb"
|
4
|
+
|
5
|
+
module IRail::NMBS
|
6
|
+
class VehicleInformation
|
7
|
+
include ROXML
|
8
|
+
|
9
|
+
xml_accessor :vehicle, :as => Vehicle
|
10
|
+
xml_accessor :trip, :from => "stops", :as => VehicleTrip
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require "roxml"
|
2
|
+
require_relative "./stopover.rb"
|
3
|
+
|
4
|
+
module IRail::NMBS
|
5
|
+
class VehicleTrip
|
6
|
+
include ROXML
|
7
|
+
|
8
|
+
xml_name 'stops'
|
9
|
+
|
10
|
+
xml_accessor :stop_count, :from => "@number", :as => Integer
|
11
|
+
xml_accessor :stops, :from => "stop", :as => [Stopover]
|
12
|
+
end
|
13
|
+
end
|
@@ -2,6 +2,7 @@ module IRail::NMBS
|
|
2
2
|
class DocumentParser
|
3
3
|
STATION_XPATH = "//station"
|
4
4
|
CONNECTION_XPATH = "//connection"
|
5
|
+
VEHICLE_XPATH = "//vehicleinformation"
|
5
6
|
|
6
7
|
def self.parse_stations(xml_string)
|
7
8
|
xml_stations(xml_string).inject([]) do |stations, xml_station|
|
@@ -15,6 +16,16 @@ module IRail::NMBS
|
|
15
16
|
end
|
16
17
|
end
|
17
18
|
|
19
|
+
def self.parse_vehicle(xml_string)
|
20
|
+
vehicle = xml_vehicle(xml_string)
|
21
|
+
IRail::NMBS::VehicleInformation.from_xml(vehicle.to_s)
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.xml_vehicle(xml_string)
|
25
|
+
xml_payload = Nokogiri::XML(xml_string)
|
26
|
+
xml_payload.xpath(VEHICLE_XPATH)
|
27
|
+
end
|
28
|
+
|
18
29
|
def self.xml_stations(xml_string)
|
19
30
|
xml_payload = Nokogiri::XML(xml_string)
|
20
31
|
xml_payload.xpath(STATION_XPATH)
|
data/lib/version.rb
CHANGED
@@ -32,6 +32,34 @@ describe IRail::API::NMBS do
|
|
32
32
|
end
|
33
33
|
end
|
34
34
|
|
35
|
+
describe :vehicle do
|
36
|
+
let(:vehicle_id) { mock("Vehicle id") }
|
37
|
+
let(:vehicle_url) { mock("Vehicle url") }
|
38
|
+
let(:xml_vehicle) { mock("Xml vehicle") }
|
39
|
+
let(:vehicle) { mock("vehicle") }
|
40
|
+
let(:options) { mock("Options") }
|
41
|
+
|
42
|
+
before :each do
|
43
|
+
irail.stub(:build_vehicle_url => vehicle_url)
|
44
|
+
irail.stub(:get_vehicle => xml_vehicle)
|
45
|
+
IRail::NMBS::DocumentParser.stub(:parse_vehicle => vehicle)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "builds the vehicle url" do
|
49
|
+
irail.should_receive(:build_vehicle_url)
|
50
|
+
irail.vehicle(vehicle_id)
|
51
|
+
end
|
52
|
+
|
53
|
+
it "gets the vehicle" do
|
54
|
+
irail.should_receive(:get_vehicle).with(vehicle_url, vehicle_id, options)
|
55
|
+
irail.vehicle(vehicle_id, options)
|
56
|
+
end
|
57
|
+
|
58
|
+
it "returns the parsed vehicle" do
|
59
|
+
irail.vehicle(vehicle_id, options).should eql vehicle
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
35
63
|
describe :stations do
|
36
64
|
let(:station_list_url) { mock("Url") }
|
37
65
|
let(:xml_station_list) { mock("Xml station list") }
|
@@ -7,6 +7,10 @@ describe IRail::NMBS::Stopover do
|
|
7
7
|
stopover.should respond_to(:id)
|
8
8
|
end
|
9
9
|
|
10
|
+
it "has a delay" do
|
11
|
+
stopover.should respond_to(:delay)
|
12
|
+
end
|
13
|
+
|
10
14
|
it "has a station" do
|
11
15
|
stopover.should respond_to(:station)
|
12
16
|
end
|
@@ -30,4 +34,8 @@ describe IRail::NMBS::Stopover do
|
|
30
34
|
it "has a wait time in seconds" do
|
31
35
|
stopover.should respond_to(:wait_time)
|
32
36
|
end
|
37
|
+
|
38
|
+
it "has a unix time" do
|
39
|
+
stopover.should respond_to(:unix_time)
|
40
|
+
end
|
33
41
|
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require_relative '../../../../lib/ruby-irail/packages/nmbs/models/vehicle_information.rb'
|
2
|
+
|
3
|
+
describe IRail::NMBS::VehicleInformation do
|
4
|
+
let(:vehicle_information) { IRail::NMBS::VehicleInformation.new }
|
5
|
+
|
6
|
+
it "has a vehicle" do
|
7
|
+
vehicle_information.should respond_to(:vehicle)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "has a trip" do
|
11
|
+
vehicle_information.should respond_to(:trip)
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require_relative '../../../../lib/ruby-irail/packages/nmbs/models/vehicle_trip.rb'
|
2
|
+
|
3
|
+
describe IRail::NMBS::VehicleTrip do
|
4
|
+
let(:vehicle_trip) { IRail::NMBS::VehicleTrip.new }
|
5
|
+
|
6
|
+
it "has a count of the number of stops" do
|
7
|
+
vehicle_trip.should respond_to(:stop_count)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "has many stops" do
|
11
|
+
vehicle_trip.should respond_to(:stops)
|
12
|
+
end
|
13
|
+
end
|
@@ -77,4 +77,36 @@ describe IRail::NMBS::DocumentParser do
|
|
77
77
|
IRail::NMBS::DocumentParser.parse_connections(xml_string).size.should eql connections.size
|
78
78
|
end
|
79
79
|
end
|
80
|
+
|
81
|
+
describe :'self.parse_vehicle' do
|
82
|
+
let(:xml_string) { mock("Xml string") }
|
83
|
+
let(:xml_payload) { mock("Xml payload") }
|
84
|
+
let(:vehicle) { mock("Vehicle") }
|
85
|
+
let(:vehicle_instance) { mock("Vehicle instance") }
|
86
|
+
|
87
|
+
before :each do
|
88
|
+
Nokogiri.stub(:XML => xml_payload)
|
89
|
+
xml_payload.stub(:xpath => vehicle)
|
90
|
+
IRail::NMBS::VehicleInformation.stub(:from_xml => vehicle_instance)
|
91
|
+
end
|
92
|
+
|
93
|
+
it "gets the xml payload from the string passed as parameter" do
|
94
|
+
Nokogiri.should_receive(:XML).with(xml_string)
|
95
|
+
IRail::NMBS::DocumentParser.parse_vehicle(xml_string)
|
96
|
+
end
|
97
|
+
|
98
|
+
it "extracts the vehicle from the xml payload" do
|
99
|
+
xml_payload.should_receive(:xpath).with(IRail::NMBS::DocumentParser::VEHICLE_XPATH)
|
100
|
+
IRail::NMBS::DocumentParser.parse_vehicle(xml_string)
|
101
|
+
end
|
102
|
+
|
103
|
+
it "creates a new vehicle for the parsed vehicle attributes" do
|
104
|
+
IRail::NMBS::VehicleInformation.should_receive(:from_xml).with(vehicle.to_s)
|
105
|
+
IRail::NMBS::DocumentParser.parse_vehicle(xml_string)
|
106
|
+
end
|
107
|
+
|
108
|
+
it "returns a vehicule instance" do
|
109
|
+
IRail::NMBS::DocumentParser.parse_vehicle(xml_string).should eql vehicle_instance
|
110
|
+
end
|
111
|
+
end
|
80
112
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-irail
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2012-07-18 00:00:00.000000000Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &70168325071420 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70168325071420
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: httparty
|
27
|
-
requirement: &
|
27
|
+
requirement: &70168325071000 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70168325071000
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: roxml
|
38
|
-
requirement: &
|
38
|
+
requirement: &70168325070580 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70168325070580
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: nokogiri
|
49
|
-
requirement: &
|
49
|
+
requirement: &70168325070140 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,7 +54,7 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :runtime
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70168325070140
|
58
58
|
description: IRail makes Belgian railway schedule easily available for anyone. This
|
59
59
|
is a Ruby wrapper for their API.
|
60
60
|
email:
|
@@ -86,6 +86,8 @@ files:
|
|
86
86
|
- lib/ruby-irail/packages/nmbs/models/trip.rb
|
87
87
|
- lib/ruby-irail/packages/nmbs/models/unix_time.rb
|
88
88
|
- lib/ruby-irail/packages/nmbs/models/vehicle.rb
|
89
|
+
- lib/ruby-irail/packages/nmbs/models/vehicle_information.rb
|
90
|
+
- lib/ruby-irail/packages/nmbs/models/vehicle_trip.rb
|
89
91
|
- lib/ruby-irail/packages/nmbs/parser/document_parser.rb
|
90
92
|
- lib/version.rb
|
91
93
|
- ruby-irail.gemspec
|
@@ -102,7 +104,9 @@ files:
|
|
102
104
|
- spec/packages/nmbs/models/stopover_spec.rb
|
103
105
|
- spec/packages/nmbs/models/trip_spec.rb
|
104
106
|
- spec/packages/nmbs/models/unix_time_spec.rb
|
107
|
+
- spec/packages/nmbs/models/vehicle_information_spec.rb
|
105
108
|
- spec/packages/nmbs/models/vehicle_spec.rb
|
109
|
+
- spec/packages/nmbs/models/vehicle_trip_spec.rb
|
106
110
|
- spec/packages/nmbs/parser/document_parser_spec.rb
|
107
111
|
homepage: https://github.com/mlainez/ruby-irail
|
108
112
|
licenses: []
|
@@ -142,5 +146,7 @@ test_files:
|
|
142
146
|
- spec/packages/nmbs/models/stopover_spec.rb
|
143
147
|
- spec/packages/nmbs/models/trip_spec.rb
|
144
148
|
- spec/packages/nmbs/models/unix_time_spec.rb
|
149
|
+
- spec/packages/nmbs/models/vehicle_information_spec.rb
|
145
150
|
- spec/packages/nmbs/models/vehicle_spec.rb
|
151
|
+
- spec/packages/nmbs/models/vehicle_trip_spec.rb
|
146
152
|
- spec/packages/nmbs/parser/document_parser_spec.rb
|