ruby-irail 0.4.1 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +8 -0
- data/lib/ruby-irail/packages/mivbstib/api.rb +4 -12
- data/lib/ruby-irail/packages/mivbstib/models/station.rb +14 -4
- data/lib/ruby-irail/packages/mivbstib/parser/json_parser.rb +15 -0
- data/lib/version.rb +1 -1
- data/ruby-irail.gemspec +1 -0
- data/spec/packages/mivbstib/api_spec.rb +19 -58
- data/spec/packages/mivbstib/parser/json_parser_spec.rb +33 -0
- metadata +24 -16
- data/lib/ruby-irail/packages/mivbstib/parser/document_parser.rb +0 -14
- data/lib/ruby-irail/packages/mivbstib/parser/station_parser.rb +0 -27
- data/spec/packages/mivbstib/parser/document_parser_spec.rb +0 -55
- data/spec/packages/mivbstib/parser/station_parser_spec.rb +0 -43
data/README.md
CHANGED
@@ -44,6 +44,14 @@ irail.connections(origin, destination)
|
|
44
44
|
#get train trip information (all stops for a train)
|
45
45
|
|
46
46
|
irail.vehicle("BE.NMBS.IC2240") # the vehicle ids cans be found when querying for a connection
|
47
|
+
|
48
|
+
#get departures from a specific station
|
49
|
+
|
50
|
+
irail.departures("BE.NMBS.008813003") # get station identifiers from the stations call
|
51
|
+
|
52
|
+
#get arrivals to a specific station
|
53
|
+
|
54
|
+
irail.arrivals("BE.NMBS.008813003")
|
47
55
|
```
|
48
56
|
|
49
57
|
## Contributing
|
@@ -3,20 +3,12 @@ module IRail::API
|
|
3
3
|
API_URL = "http://data.irail.be"
|
4
4
|
PACKAGE_ID = "MIVBSTIB"
|
5
5
|
STATIONS_URI = "Stations"
|
6
|
-
PARSER_FORMAT = "
|
6
|
+
PARSER_FORMAT = "json"
|
7
7
|
|
8
8
|
def stations
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
def get_stations
|
13
|
-
station_list_url = build_station_list_url
|
14
|
-
xml_station_list = get_station_list(station_list_url)
|
15
|
-
IRail::MIVBSTIB::DocumentParser.parse_stations(xml_station_list)
|
16
|
-
end
|
17
|
-
|
18
|
-
def get_station_list(station_list_url)
|
19
|
-
IRail::Request.get(station_list_url)
|
9
|
+
url = build_station_list_url
|
10
|
+
json = IRail::Request.get(url)
|
11
|
+
IRail::MIVBSTIB::JSONParser.parse_stations(json)
|
20
12
|
end
|
21
13
|
|
22
14
|
private
|
@@ -1,12 +1,22 @@
|
|
1
1
|
require "virtus"
|
2
2
|
|
3
3
|
module IRail::MIVBSTIB
|
4
|
+
module Attributes
|
5
|
+
class Coordinate < Virtus::Attribute::Object
|
6
|
+
primitive Float
|
7
|
+
|
8
|
+
def coerce(value)
|
9
|
+
value.strip.to_f
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
4
14
|
class Station
|
5
15
|
include Virtus
|
6
16
|
|
7
|
-
attribute :id,
|
8
|
-
attribute :latitude,
|
9
|
-
attribute :longitude,
|
10
|
-
attribute :name,
|
17
|
+
attribute :id, Integer
|
18
|
+
attribute :latitude, Attributes::Coordinate
|
19
|
+
attribute :longitude, Attributes::Coordinate
|
20
|
+
attribute :name, String
|
11
21
|
end
|
12
22
|
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require "json"
|
2
|
+
|
3
|
+
module IRail::MIVBSTIB
|
4
|
+
class JSONParser
|
5
|
+
STATIONS_KEY = "Stations"
|
6
|
+
|
7
|
+
def self.parse_stations(json_string)
|
8
|
+
json_hash = JSON.parse(json_string)
|
9
|
+
stations_json = json_hash[STATIONS_KEY]
|
10
|
+
stations_json.inject([]) do |stations, station_attributes|
|
11
|
+
stations << IRail::MIVBSTIB::Station.new(station_attributes)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/version.rb
CHANGED
data/ruby-irail.gemspec
CHANGED
@@ -2,73 +2,34 @@ require_relative "../../../lib/ruby-irail/packages/mivbstib/api.rb"
|
|
2
2
|
|
3
3
|
describe IRail::API::MIVBSTIB do
|
4
4
|
let(:irail) { IRail::API::MIVBSTIB.new }
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
it "returns the stations when they were already fetched" do
|
15
|
-
irail.instance_variable_set(:@stations, stations)
|
16
|
-
irail.stations.should eql stations
|
17
|
-
end
|
18
|
-
|
19
|
-
context "when stations have not been fetched yet" do
|
20
|
-
it "fetches them" do
|
21
|
-
irail.should_receive(:get_stations)
|
22
|
-
irail.stations
|
23
|
-
end
|
24
|
-
|
25
|
-
it "returns them" do
|
26
|
-
irail.stations.should eql fetched_stations
|
27
|
-
end
|
28
|
-
end
|
5
|
+
let(:url) { mock("Stations url") }
|
6
|
+
let(:json) { mock("Json stations") }
|
7
|
+
let(:stations) { mock("Stations") }
|
8
|
+
|
9
|
+
before :each do
|
10
|
+
irail.stub(:build_station_list_url => url)
|
11
|
+
IRail::Request.stub(:get => json)
|
12
|
+
IRail::MIVBSTIB::JSONParser.stub(:parse_stations => stations)
|
29
13
|
end
|
30
14
|
|
31
|
-
describe :
|
32
|
-
|
33
|
-
let(:xml_station_list) { mock("Xml station list") }
|
34
|
-
let(:stations) { mock("Stations") }
|
35
|
-
|
36
|
-
before :each do
|
37
|
-
irail.stub(:build_station_list_url => station_list_url)
|
38
|
-
irail.stub(:get_station_list => xml_station_list)
|
39
|
-
IRail::MIVBSTIB::DocumentParser.stub(:parse_stations => stations)
|
40
|
-
end
|
41
|
-
|
42
|
-
it "builds the station list url" do
|
15
|
+
describe :stations do
|
16
|
+
it "builds the station url" do
|
43
17
|
irail.should_receive(:build_station_list_url)
|
44
|
-
irail.
|
45
|
-
end
|
46
|
-
|
47
|
-
it "gets the xml station list" do
|
48
|
-
irail.should_receive(:get_station_list).with(station_list_url)
|
49
|
-
irail.get_stations
|
18
|
+
irail.stations
|
50
19
|
end
|
51
20
|
|
52
|
-
it "
|
53
|
-
|
54
|
-
|
55
|
-
end
|
56
|
-
|
57
|
-
describe :get_station_list do
|
58
|
-
let(:url) { mock("Url") }
|
59
|
-
let(:response) { mock("Response") }
|
60
|
-
|
61
|
-
before :each do
|
62
|
-
IRail::Request.stub(:get => response)
|
21
|
+
it "calls the api" do
|
22
|
+
IRail::Request.should_receive(:get).with(url)
|
23
|
+
irail.stations
|
63
24
|
end
|
64
25
|
|
65
|
-
it "
|
66
|
-
IRail::
|
67
|
-
irail.
|
26
|
+
it "parses the returned json" do
|
27
|
+
IRail::MIVBSTIB::JSONParser.should_receive(:parse_stations).with(json)
|
28
|
+
irail.stations
|
68
29
|
end
|
69
30
|
|
70
|
-
it "returns the
|
71
|
-
irail.
|
31
|
+
it "returns the stations" do
|
32
|
+
irail.stations.should eql stations
|
72
33
|
end
|
73
34
|
end
|
74
35
|
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require_relative "../../../../lib/ruby-irail/packages/mivbstib/parser/json_parser.rb"
|
2
|
+
|
3
|
+
describe IRail::MIVBSTIB::JSONParser do
|
4
|
+
class IRail::MIVBSTIB::Station
|
5
|
+
end
|
6
|
+
|
7
|
+
describe :'self.parse_stations' do
|
8
|
+
let(:json_string) { mock("Json string") }
|
9
|
+
let(:json_stations) { [ IRail::MIVBSTIB::Station.new, IRail::MIVBSTIB::Station.new, IRail::MIVBSTIB::Station.new ] }
|
10
|
+
let(:station) { mock("Station") }
|
11
|
+
|
12
|
+
before :each do
|
13
|
+
JSON.stub(:parse => {"Stations" => json_stations})
|
14
|
+
IRail::MIVBSTIB::Station.stub(:new => station)
|
15
|
+
end
|
16
|
+
|
17
|
+
it "parses the json" do
|
18
|
+
JSON.should_receive(:parse).with(json_string)
|
19
|
+
IRail::MIVBSTIB::JSONParser.parse_stations(json_string)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "creates a station for each json station" do
|
23
|
+
json_stations.each do |station|
|
24
|
+
IRail::MIVBSTIB::Station.should_receive(:new).with(station)
|
25
|
+
end
|
26
|
+
IRail::MIVBSTIB::JSONParser.parse_stations(json_string)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "returns the stations" do
|
30
|
+
IRail::MIVBSTIB::JSONParser.parse_stations(json_string).should eql [ station, station, station ]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
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.
|
4
|
+
version: 0.4.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: 2012-07-
|
12
|
+
date: 2012-07-20 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &70163615043800 !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: *70163615043800
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: httparty
|
27
|
-
requirement: &
|
27
|
+
requirement: &70163615043380 !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: *70163615043380
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: roxml
|
38
|
-
requirement: &
|
38
|
+
requirement: &70163615042960 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,21 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70163615042960
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: json
|
49
|
+
requirement: &70163615036020 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :runtime
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70163615036020
|
47
58
|
- !ruby/object:Gem::Dependency
|
48
59
|
name: nokogiri
|
49
|
-
requirement: &
|
60
|
+
requirement: &70163615035400 !ruby/object:Gem::Requirement
|
50
61
|
none: false
|
51
62
|
requirements:
|
52
63
|
- - ! '>='
|
@@ -54,7 +65,7 @@ dependencies:
|
|
54
65
|
version: '0'
|
55
66
|
type: :runtime
|
56
67
|
prerelease: false
|
57
|
-
version_requirements: *
|
68
|
+
version_requirements: *70163615035400
|
58
69
|
description: IRail makes Belgian railway schedule easily available for anyone. This
|
59
70
|
is a Ruby wrapper for their API.
|
60
71
|
email:
|
@@ -75,8 +86,7 @@ files:
|
|
75
86
|
- lib/ruby-irail/http/request.rb
|
76
87
|
- lib/ruby-irail/packages/mivbstib/api.rb
|
77
88
|
- lib/ruby-irail/packages/mivbstib/models/station.rb
|
78
|
-
- lib/ruby-irail/packages/mivbstib/parser/
|
79
|
-
- lib/ruby-irail/packages/mivbstib/parser/station_parser.rb
|
89
|
+
- lib/ruby-irail/packages/mivbstib/parser/json_parser.rb
|
80
90
|
- lib/ruby-irail/packages/nmbs/api.rb
|
81
91
|
- lib/ruby-irail/packages/nmbs/models/connection.rb
|
82
92
|
- lib/ruby-irail/packages/nmbs/models/liveboard.rb
|
@@ -95,8 +105,7 @@ files:
|
|
95
105
|
- spec/http/request_spec.rb
|
96
106
|
- spec/packages/mivbstib/api_spec.rb
|
97
107
|
- spec/packages/mivbstib/models/station_spec.rb
|
98
|
-
- spec/packages/mivbstib/parser/
|
99
|
-
- spec/packages/mivbstib/parser/station_parser_spec.rb
|
108
|
+
- spec/packages/mivbstib/parser/json_parser_spec.rb
|
100
109
|
- spec/packages/nmbs/api_spec.rb
|
101
110
|
- spec/packages/nmbs/models/connection_spec.rb
|
102
111
|
- spec/packages/nmbs/models/liveboard_spec.rb
|
@@ -138,8 +147,7 @@ test_files:
|
|
138
147
|
- spec/http/request_spec.rb
|
139
148
|
- spec/packages/mivbstib/api_spec.rb
|
140
149
|
- spec/packages/mivbstib/models/station_spec.rb
|
141
|
-
- spec/packages/mivbstib/parser/
|
142
|
-
- spec/packages/mivbstib/parser/station_parser_spec.rb
|
150
|
+
- spec/packages/mivbstib/parser/json_parser_spec.rb
|
143
151
|
- spec/packages/nmbs/api_spec.rb
|
144
152
|
- spec/packages/nmbs/models/connection_spec.rb
|
145
153
|
- spec/packages/nmbs/models/liveboard_spec.rb
|
@@ -1,14 +0,0 @@
|
|
1
|
-
module IRail::MIVBSTIB
|
2
|
-
class DocumentParser
|
3
|
-
STATION_XPATH = "//Stations"
|
4
|
-
|
5
|
-
def self.parse_stations(xml_string)
|
6
|
-
xml_payload = Nokogiri::XML(xml_string)
|
7
|
-
xml_stations = xml_payload.xpath(STATION_XPATH)
|
8
|
-
xml_stations.inject([]) do |stations, xml_station|
|
9
|
-
attributes = IRail::MIVBSTIB::StationParser.parse(xml_station)
|
10
|
-
stations << IRail::MIVBSTIB::Station.new(attributes)
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
@@ -1,27 +0,0 @@
|
|
1
|
-
module IRail::MIVBSTIB
|
2
|
-
class StationParser
|
3
|
-
def self.parse(xml_payload)
|
4
|
-
id = parse_station_id(xml_payload)
|
5
|
-
latitude = parse_station_latitude(xml_payload)
|
6
|
-
longitude = parse_station_longitude(xml_payload)
|
7
|
-
name = parse_station_name(xml_payload)
|
8
|
-
{id: id, latitude: latitude, longitude: longitude, name: name}
|
9
|
-
end
|
10
|
-
|
11
|
-
def self.parse_station_id(xml_payload)
|
12
|
-
xml_payload.xpath('id').text.strip
|
13
|
-
end
|
14
|
-
|
15
|
-
def self.parse_station_latitude(xml_payload)
|
16
|
-
xml_payload.xpath('latitude').text.strip
|
17
|
-
end
|
18
|
-
|
19
|
-
def self.parse_station_longitude(xml_payload)
|
20
|
-
xml_payload.xpath('longitude').text.strip
|
21
|
-
end
|
22
|
-
|
23
|
-
def self.parse_station_name(xml_payload)
|
24
|
-
xml_payload.xpath('name').text.strip
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
@@ -1,55 +0,0 @@
|
|
1
|
-
require_relative "../../../../lib/ruby-irail/packages/mivbstib/parser/document_parser.rb"
|
2
|
-
|
3
|
-
describe IRail::MIVBSTIB::DocumentParser do
|
4
|
-
module Nokogiri
|
5
|
-
module XML
|
6
|
-
end
|
7
|
-
end
|
8
|
-
|
9
|
-
class IRail::MIVBSTIB::StationParser
|
10
|
-
end
|
11
|
-
|
12
|
-
class IRail::MIVBSTIB::Station
|
13
|
-
end
|
14
|
-
|
15
|
-
describe :'self.parse_stations' do
|
16
|
-
let(:xml_string) { mock("Xml string") }
|
17
|
-
let(:xml_payload) { mock("Xml payload") }
|
18
|
-
let(:stations) { [ mock("Station"), mock("Station"), mock("Station") ] }
|
19
|
-
let(:attributes) { {} }
|
20
|
-
|
21
|
-
before :each do
|
22
|
-
Nokogiri.stub(:XML => xml_payload)
|
23
|
-
xml_payload.stub(:xpath => stations)
|
24
|
-
IRail::MIVBSTIB::StationParser.stub(:parse => attributes)
|
25
|
-
end
|
26
|
-
|
27
|
-
it "gets the xml payload from the string passed as parameter" do
|
28
|
-
Nokogiri.should_receive(:XML).with(xml_string)
|
29
|
-
IRail::MIVBSTIB::DocumentParser.parse_stations(xml_string)
|
30
|
-
end
|
31
|
-
|
32
|
-
it "extracts the stations from the xml payload" do
|
33
|
-
xml_payload.should_receive(:xpath).with(IRail::MIVBSTIB::DocumentParser::STATION_XPATH)
|
34
|
-
IRail::MIVBSTIB::DocumentParser.parse_stations(xml_string)
|
35
|
-
end
|
36
|
-
|
37
|
-
it "parses the attributes of all stations" do
|
38
|
-
stations.each do |xml_station|
|
39
|
-
IRail::MIVBSTIB::StationParser.should_receive(:parse).with(xml_station)
|
40
|
-
end
|
41
|
-
IRail::MIVBSTIB::DocumentParser.parse_stations(xml_string)
|
42
|
-
end
|
43
|
-
|
44
|
-
it "creates a new station for all parsed station attributes" do
|
45
|
-
stations.each do |station|
|
46
|
-
IRail::MIVBSTIB::Station.should_receive(:new).with(attributes)
|
47
|
-
end
|
48
|
-
IRail::MIVBSTIB::DocumentParser.parse_stations(xml_string)
|
49
|
-
end
|
50
|
-
|
51
|
-
it "returns as many stations as in the xml payload" do
|
52
|
-
IRail::MIVBSTIB::DocumentParser.parse_stations(xml_string).size.should eql stations.size
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
@@ -1,43 +0,0 @@
|
|
1
|
-
require_relative "../../../../lib/ruby-irail/packages/mivbstib/parser/station_parser.rb"
|
2
|
-
|
3
|
-
describe IRail::MIVBSTIB::StationParser do
|
4
|
-
describe :'self.parse' do
|
5
|
-
let(:xml_payload) { mock("Xml Payload") }
|
6
|
-
let(:id) { mock("Station Id") }
|
7
|
-
let(:latitude) { mock("Latitude") }
|
8
|
-
let(:longitude) { mock("Longitude") }
|
9
|
-
let(:name) { mock("Name") }
|
10
|
-
|
11
|
-
before :each do
|
12
|
-
IRail::MIVBSTIB::StationParser.stub(parse_station_id: id)
|
13
|
-
IRail::MIVBSTIB::StationParser.stub(parse_station_latitude: latitude)
|
14
|
-
IRail::MIVBSTIB::StationParser.stub(parse_station_longitude: longitude)
|
15
|
-
IRail::MIVBSTIB::StationParser.stub(parse_station_name: name)
|
16
|
-
end
|
17
|
-
|
18
|
-
it "parses the id" do
|
19
|
-
IRail::MIVBSTIB::StationParser.should_receive(:parse_station_id).with(xml_payload)
|
20
|
-
IRail::MIVBSTIB::StationParser.parse(xml_payload)
|
21
|
-
end
|
22
|
-
|
23
|
-
it "parses the latitude" do
|
24
|
-
IRail::MIVBSTIB::StationParser.should_receive(:parse_station_latitude).with(xml_payload)
|
25
|
-
IRail::MIVBSTIB::StationParser.parse(xml_payload)
|
26
|
-
end
|
27
|
-
|
28
|
-
it "parses the longitude" do
|
29
|
-
IRail::MIVBSTIB::StationParser.should_receive(:parse_station_longitude).with(xml_payload)
|
30
|
-
IRail::MIVBSTIB::StationParser.parse(xml_payload)
|
31
|
-
end
|
32
|
-
|
33
|
-
it "parses the name" do
|
34
|
-
IRail::MIVBSTIB::StationParser.should_receive(:parse_station_name).with(xml_payload)
|
35
|
-
IRail::MIVBSTIB::StationParser.parse(xml_payload)
|
36
|
-
end
|
37
|
-
|
38
|
-
it "returns a hash containing the parsed attributes" do
|
39
|
-
expected = { id: id, latitude: latitude, longitude: longitude, name: name}
|
40
|
-
IRail::MIVBSTIB::StationParser.parse(xml_payload).should eql expected
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|