ruby-irail 0.1.0 → 0.2.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.
Files changed (37) hide show
  1. data/README.md +18 -4
  2. data/lib/ruby-irail/api.rb +12 -18
  3. data/lib/ruby-irail/http/request.rb +2 -2
  4. data/lib/ruby-irail/packages/mivbstib/api.rb +28 -0
  5. data/lib/ruby-irail/{models → packages/mivbstib/models}/station.rb +2 -3
  6. data/lib/ruby-irail/{parser → packages/mivbstib/parser}/document_parser.rb +4 -4
  7. data/lib/ruby-irail/{parser → packages/mivbstib/parser}/station_parser.rb +6 -12
  8. data/lib/ruby-irail/packages/nmbs/api.rb +49 -0
  9. data/lib/ruby-irail/packages/nmbs/models/connection.rb +16 -0
  10. data/lib/ruby-irail/packages/nmbs/models/platform.rb +10 -0
  11. data/lib/ruby-irail/packages/nmbs/models/station.rb +13 -0
  12. data/lib/ruby-irail/packages/nmbs/models/step.rb +18 -0
  13. data/lib/ruby-irail/packages/nmbs/models/stopover.rb +20 -0
  14. data/lib/ruby-irail/packages/nmbs/models/trip.rb +13 -0
  15. data/lib/ruby-irail/packages/nmbs/models/unix_time.rb +12 -0
  16. data/lib/ruby-irail/packages/nmbs/models/vehicle.rb +9 -0
  17. data/lib/ruby-irail/packages/nmbs/parser/document_parser.rb +28 -0
  18. data/lib/ruby-irail.rb +2 -8
  19. data/lib/version.rb +1 -1
  20. data/ruby-irail.gemspec +1 -1
  21. data/spec/http/request_spec.rb +8 -2
  22. data/spec/{irail_spec.rb → packages/mivbstib/api_spec.rb} +4 -4
  23. data/spec/packages/mivbstib/models/station_spec.rb +21 -0
  24. data/spec/{parser → packages/mivbstib/parser}/document_parser_spec.rb +13 -13
  25. data/spec/packages/mivbstib/parser/station_parser_spec.rb +43 -0
  26. data/spec/packages/nmbs/api_spec.rb +129 -0
  27. data/spec/packages/nmbs/models/connection_spec.rb +29 -0
  28. data/spec/packages/nmbs/models/platform_spec.rb +13 -0
  29. data/spec/{models → packages/nmbs/models}/station_spec.rb +3 -3
  30. data/spec/packages/nmbs/models/step_spec.rb +29 -0
  31. data/spec/packages/nmbs/models/stopover_spec.rb +33 -0
  32. data/spec/packages/nmbs/models/trip_spec.rb +13 -0
  33. data/spec/packages/nmbs/models/unix_time_spec.rb +13 -0
  34. data/spec/packages/nmbs/models/vehicle_spec.rb +9 -0
  35. data/spec/packages/nmbs/parser/document_parser_spec.rb +80 -0
  36. metadata +53 -22
  37. data/spec/parser/station_parser_spec.rb +0 -50
data/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  IRail makes Belgian railway schedule easily available for anyone.
4
4
 
5
+ Check it out on [http://project.irail.be/](http://project.irail.be/) or [http://data.irail.be/](http://data.irail.be/)
6
+
5
7
  There are already several wrappers available in different languages. This is a Ruby wrapper for their API.
6
8
 
7
9
  ## Installation
@@ -21,11 +23,23 @@ Or install it yourself as:
21
23
  ## Usage
22
24
 
23
25
  ```ruby
24
- # get a list of all belgian train stations
25
-
26
26
  require "ruby-irail"
27
- irail = IRail::API.new
27
+
28
+ # get a list of all mivb/stib stations in brussels
29
+
30
+ irail = IRail::API.new(:stib) # use :mivb symbol instead of :stib if you want
28
31
  irail.stations
32
+
33
+ # get a list of all belgian train stations
34
+
35
+ irail = IRail::API.new # by default it takes the nmbs/sncb provider but you can also use :sncb or :nmbs symbols
36
+ train_stations = irail.stations
37
+
38
+ # get train trips from one station to another
39
+
40
+ origin = train_stations.first
41
+ destination = train_stations.last
42
+ irail.connections(origin, destination)
29
43
  ```
30
44
 
31
45
  ## Contributing
@@ -41,7 +55,7 @@ irail.stations
41
55
  All tests are in the specs folder. To run them simply run the following from the root folder of your fork:
42
56
 
43
57
  ```ruby
44
- rspec specs
58
+ rspec
45
59
  ```
46
60
 
47
61
  ### Contributors
@@ -1,25 +1,19 @@
1
- API_URL = "http://api.irail.be"
2
- STATIONS_URI = "stations/"
3
-
4
1
  module IRail
5
- class API
6
- def stations
7
- @stations ||= get_stations
8
- end
9
-
10
- def get_stations
11
- station_list_url = build_station_list_url
12
- xml_station_list = get_station_list(station_list_url)
13
- IRail::DocumentParser.parse_stations(xml_station_list)
14
- end
2
+ module API
3
+ SUPPORTED_APIS = {
4
+ :nmbs => "NMBS",
5
+ :sncb => "NMBS",
6
+ :stib => "MIVBSTIB",
7
+ :mivb => "MIVBSTIB"
8
+ }
15
9
 
16
- def get_station_list(station_list_url)
17
- IRail::Request.get(station_list_url)
10
+ def self.new(provider = :nmbs)
11
+ klass = api_name_for_provider(provider)
12
+ const_get(klass).new
18
13
  end
19
14
 
20
- private
21
- def build_station_list_url
22
- [API_URL, STATIONS_URI].join('/')
15
+ def self.api_name_for_provider(provider)
16
+ SUPPORTED_APIS[provider]
23
17
  end
24
18
  end
25
19
  end
@@ -1,7 +1,7 @@
1
1
  module IRail
2
2
  module Request
3
- def self.get(url)
4
- request = HTTParty.get(url)
3
+ def self.get(url, options = {})
4
+ request = HTTParty.get(url, options)
5
5
  request.response.body
6
6
  end
7
7
  end
@@ -0,0 +1,28 @@
1
+ module IRail::API
2
+ class MIVBSTIB
3
+ API_URL = "http://data.irail.be"
4
+ PACKAGE_ID = "MIVBSTIB"
5
+ STATIONS_URI = "Stations"
6
+ PARSER_FORMAT = "xml"
7
+
8
+ def stations
9
+ @stations ||= get_stations
10
+ end
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)
20
+ end
21
+
22
+ private
23
+ def build_station_list_url
24
+ base_url = [API_URL, PACKAGE_ID, STATIONS_URI].join('/')
25
+ [base_url, PARSER_FORMAT].join('.')
26
+ end
27
+ end
28
+ end
@@ -1,13 +1,12 @@
1
1
  require "virtus"
2
2
 
3
- module IRail
3
+ module IRail::MIVBSTIB
4
4
  class Station
5
5
  include Virtus
6
6
 
7
- attribute :id, String
7
+ attribute :id, Integer
8
8
  attribute :latitude, Float
9
9
  attribute :longitude, Float
10
- attribute :standard_name, String
11
10
  attribute :name, String
12
11
  end
13
12
  end
@@ -1,13 +1,13 @@
1
- module IRail
1
+ module IRail::MIVBSTIB
2
2
  class DocumentParser
3
- STATION_XPATH = "//station"
3
+ STATION_XPATH = "//Stations"
4
4
 
5
5
  def self.parse_stations(xml_string)
6
6
  xml_payload = Nokogiri::XML(xml_string)
7
7
  xml_stations = xml_payload.xpath(STATION_XPATH)
8
8
  xml_stations.inject([]) do |stations, xml_station|
9
- attributes = StationParser.parse(xml_station)
10
- stations << Station.new(attributes)
9
+ attributes = IRail::MIVBSTIB::StationParser.parse(xml_station)
10
+ stations << IRail::MIVBSTIB::Station.new(attributes)
11
11
  end
12
12
  end
13
13
  end
@@ -1,33 +1,27 @@
1
- module IRail
1
+ module IRail::MIVBSTIB
2
2
  class StationParser
3
3
  def self.parse(xml_payload)
4
4
  id = parse_station_id(xml_payload)
5
5
  latitude = parse_station_latitude(xml_payload)
6
6
  longitude = parse_station_longitude(xml_payload)
7
- standard_name = parse_station_standard_name(xml_payload)
8
7
  name = parse_station_name(xml_payload)
9
- {:id => id, :latitude => latitude, :longitude => longitude,
10
- :standard_name => standard_name, :name => name}
8
+ {id: id, latitude: latitude, longitude: longitude, name: name}
11
9
  end
12
10
 
13
11
  def self.parse_station_id(xml_payload)
14
- xml_payload.attr('id')
12
+ xml_payload.xpath('id').text.strip
15
13
  end
16
14
 
17
15
  def self.parse_station_latitude(xml_payload)
18
- xml_payload.attr('locationX')
16
+ xml_payload.xpath('latitude').text.strip
19
17
  end
20
18
 
21
19
  def self.parse_station_longitude(xml_payload)
22
- xml_payload.attr('locationY')
23
- end
24
-
25
- def self.parse_station_standard_name(xml_payload)
26
- xml_payload.attr('standardname')
20
+ xml_payload.xpath('longitude').text.strip
27
21
  end
28
22
 
29
23
  def self.parse_station_name(xml_payload)
30
- xml_payload.content
24
+ xml_payload.xpath('name').text.strip
31
25
  end
32
26
  end
33
27
  end
@@ -0,0 +1,49 @@
1
+ module IRail::API
2
+ class NMBS
3
+ API_URL = "http://api.irail.be"
4
+ STATIONS_URI = "stations/"
5
+ CONNECTIONS_URI = "connections/"
6
+
7
+ def stations
8
+ @stations ||= get_stations
9
+ end
10
+
11
+ def connections(origin_station, destination_station)
12
+ connections_url = build_connections_url
13
+ xml_connections = get_connections(connections_url, origin_station, destination_station)
14
+ IRail::NMBS::DocumentParser.parse_connections(xml_connections)
15
+ end
16
+
17
+ def get_stations
18
+ station_list_url = build_station_list_url
19
+ xml_station_list = get_station_list(station_list_url)
20
+ IRail::NMBS::DocumentParser.parse_stations(xml_station_list)
21
+ end
22
+
23
+ def get_connections(connections_url, origin_station, destination_station)
24
+ options = build_connections_option_hash(origin_station, destination_station)
25
+ IRail::Request.get(connections_url, options)
26
+ end
27
+
28
+ def get_station_list(station_list_url)
29
+ IRail::Request.get(station_list_url)
30
+ end
31
+
32
+ private
33
+ def build_connections_option_hash(origin_station, destination_station)
34
+ {
35
+ :query => {
36
+ :from => origin_station.name,
37
+ :to => destination_station.name }
38
+ }
39
+ end
40
+
41
+ def build_station_list_url
42
+ [API_URL, STATIONS_URI].join('/')
43
+ end
44
+
45
+ def build_connections_url
46
+ [API_URL, CONNECTIONS_URI].join('/')
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,16 @@
1
+ require "roxml"
2
+ require_relative "./step.rb"
3
+ require_relative "./trip.rb"
4
+
5
+ module IRail::NMBS
6
+ class Connection
7
+ include ROXML
8
+
9
+ xml_accessor :id, :as => Integer, :from => "@id"
10
+ xml_accessor :departure, :as => Step, :from => "departure"
11
+ xml_accessor :arrival, :as => Step, :from => "arrival"
12
+ xml_accessor :duration, :as => Integer, :from => "duration"
13
+ xml_accessor :delay, :from => "duration[@delay]"
14
+ xml_accessor :trip, :as => Trip
15
+ end
16
+ end
@@ -0,0 +1,10 @@
1
+ require "roxml"
2
+
3
+ module IRail::NMBS
4
+ class Platform
5
+ include ROXML
6
+
7
+ xml_accessor :number, :from => :content
8
+ xml_accessor :normal?, :from => "@normal"
9
+ end
10
+ end
@@ -0,0 +1,13 @@
1
+ require "roxml"
2
+
3
+ module IRail::NMBS
4
+ class Station
5
+ include ROXML
6
+
7
+ xml_accessor :id, :from => "@id"
8
+ xml_accessor :latitude, :from => "@locationY", :as => Float
9
+ xml_accessor :longitude, :from => "@locationX", :as => Float
10
+ xml_accessor :standard_name, :from => "@standardname"
11
+ xml_accessor :name, :from => :content
12
+ end
13
+ end
@@ -0,0 +1,18 @@
1
+ require "roxml"
2
+ require_relative "./station.rb"
3
+ require_relative "./unix_time.rb"
4
+ require_relative "./vehicle.rb"
5
+ require_relative "./platform.rb"
6
+
7
+ module IRail::NMBS
8
+ class Step
9
+ include ROXML
10
+
11
+ xml_accessor :delay, :from => "@delay", :as => Integer
12
+ xml_accessor :station, :as => Station
13
+ xml_accessor :unix_time, :as => UnixTime
14
+ xml_accessor :vehicle, :as => Vehicle
15
+ xml_accessor :platform, :as => Platform
16
+ xml_accessor :direction, :as => Station
17
+ end
18
+ end
@@ -0,0 +1,20 @@
1
+ require "roxml"
2
+ require_relative "./vehicle.rb"
3
+ require_relative "./station.rb"
4
+ require_relative "./step.rb"
5
+
6
+ module IRail::NMBS
7
+ class Stopover
8
+ include ROXML
9
+
10
+ xml_name "via"
11
+
12
+ xml_accessor :id, :from => "@id", :as => Integer
13
+ xml_accessor :station, :as => Station
14
+ xml_accessor :vehicle, :as => Vehicle
15
+ xml_accessor :arrival, :as => Step, :from => "arrival"
16
+ xml_accessor :departure, :as => Step, :from => "departure"
17
+ xml_accessor :direction, :as => Station
18
+ xml_reader :wait_time, :from => "timeBetween", :as => Integer
19
+ end
20
+ end
@@ -0,0 +1,13 @@
1
+ require "roxml"
2
+ require_relative "./stopover.rb"
3
+
4
+ module IRail::NMBS
5
+ class Trip
6
+ include ROXML
7
+
8
+ xml_name 'vias'
9
+
10
+ xml_accessor :stopover_count, :from => "@number", :as => Integer
11
+ xml_accessor :stopovers, :as => [Stopover]
12
+ end
13
+ end
@@ -0,0 +1,12 @@
1
+ require "roxml"
2
+
3
+ module IRail::NMBS
4
+ class UnixTime
5
+ include ROXML
6
+
7
+ xml_name "time"
8
+
9
+ xml_accessor :format, :from => "@formatted"
10
+ xml_accessor :time, :from => :content
11
+ end
12
+ end
@@ -0,0 +1,9 @@
1
+ require "roxml"
2
+
3
+ module IRail::NMBS
4
+ class Vehicle
5
+ include ROXML
6
+
7
+ xml_accessor :id, :from => :content
8
+ end
9
+ end
@@ -0,0 +1,28 @@
1
+ module IRail::NMBS
2
+ class DocumentParser
3
+ STATION_XPATH = "//station"
4
+ CONNECTION_XPATH = "//connection"
5
+
6
+ def self.parse_stations(xml_string)
7
+ xml_stations(xml_string).inject([]) do |stations, xml_station|
8
+ stations << IRail::NMBS::Station.from_xml(xml_station.to_s)
9
+ end
10
+ end
11
+
12
+ def self.parse_connections(xml_string)
13
+ xml_connections(xml_string).inject([]) do |connections, xml_connection|
14
+ connections << IRail::NMBS::Connection.from_xml(xml_connection.to_s)
15
+ end
16
+ end
17
+
18
+ def self.xml_stations(xml_string)
19
+ xml_payload = Nokogiri::XML(xml_string)
20
+ xml_payload.xpath(STATION_XPATH)
21
+ end
22
+
23
+ def self.xml_connections(xml_string)
24
+ xml_payload = Nokogiri::XML(xml_string)
25
+ xml_payload.xpath(CONNECTION_XPATH)
26
+ end
27
+ end
28
+ end
data/lib/ruby-irail.rb CHANGED
@@ -1,11 +1,5 @@
1
1
  require "httparty"
2
2
  require "nokogiri"
3
+ require "roxml"
3
4
 
4
- require File.join(File.dirname(__FILE__), 'ruby-irail', 'api')
5
- require File.join(File.dirname(__FILE__), 'ruby-irail', 'http', 'request')
6
- require File.join(File.dirname(__FILE__), 'ruby-irail', 'models', 'station')
7
- require File.join(File.dirname(__FILE__), 'ruby-irail', 'parser', 'document_parser')
8
- require File.join(File.dirname(__FILE__), 'ruby-irail', 'parser', 'station_parser')
9
-
10
- module IRail
11
- end
5
+ Dir.glob(File.join(File.dirname(__FILE__), "**", "*.rb")).each{ |file| require file }
data/lib/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module IRail
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/ruby-irail.gemspec CHANGED
@@ -16,6 +16,6 @@ Gem::Specification.new do |gem|
16
16
  gem.version = IRail::VERSION
17
17
  gem.add_development_dependency "rspec"
18
18
  gem.add_dependency "httparty"
19
- gem.add_dependency "virtus"
19
+ gem.add_dependency "roxml"
20
20
  gem.add_dependency "nokogiri"
21
21
  end
@@ -9,6 +9,7 @@ describe IRail::Request do
9
9
  let(:request) { mock("Request") }
10
10
  let(:response) { mock("Response") }
11
11
  let(:body) { mock("Body") }
12
+ let(:options) { mock("Options") }
12
13
 
13
14
  before :each do
14
15
  HTTParty.stub(:get => request)
@@ -16,11 +17,16 @@ describe IRail::Request do
16
17
  response.stub(:body => body)
17
18
  end
18
19
 
19
- it "calls the get method of HTTParty with the url as param" do
20
- HTTParty.should_receive(:get).with(url)
20
+ it "calls the get method of HTTParty with the url as param and an empty default hash as options" do
21
+ HTTParty.should_receive(:get).with(url, {})
21
22
  IRail::Request.get(url)
22
23
  end
23
24
 
25
+ it "calls the get method of HTTParty with the url asa param and an option hash when one is passed as parameter" do
26
+ HTTParty.should_receive(:get).with(url, options)
27
+ IRail::Request.get(url, options)
28
+ end
29
+
24
30
  it "gets the body from the response" do
25
31
  response.should_receive(:body)
26
32
  IRail::Request.get(url)
@@ -1,7 +1,7 @@
1
- require_relative "../lib/ruby-irail/api.rb"
1
+ require_relative "../../../lib/ruby-irail/packages/mivbstib/api.rb"
2
2
 
3
- describe IRail::API do
4
- let(:irail) { IRail::API.new }
3
+ describe IRail::API::MIVBSTIB do
4
+ let(:irail) { IRail::API::MIVBSTIB.new }
5
5
 
6
6
  describe :stations do
7
7
  let(:stations) { mock("Stations") }
@@ -36,7 +36,7 @@ describe IRail::API do
36
36
  before :each do
37
37
  irail.stub(:build_station_list_url => station_list_url)
38
38
  irail.stub(:get_station_list => xml_station_list)
39
- IRail::DocumentParser.stub(:parse_stations => stations)
39
+ IRail::MIVBSTIB::DocumentParser.stub(:parse_stations => stations)
40
40
  end
41
41
 
42
42
  it "builds the station list url" do
@@ -0,0 +1,21 @@
1
+ require_relative '../../../../lib/ruby-irail/packages/mivbstib/models/station.rb'
2
+
3
+ describe IRail::MIVBSTIB::Station do
4
+ let(:station) { IRail::MIVBSTIB::Station.new }
5
+
6
+ it "has an id" do
7
+ station.should respond_to(:id)
8
+ end
9
+
10
+ it "has an latitude" do
11
+ station.should respond_to(:latitude)
12
+ end
13
+
14
+ it "has an longitude" do
15
+ station.should respond_to(:longitude)
16
+ end
17
+
18
+ it "has a name" do
19
+ station.should respond_to(:name)
20
+ end
21
+ end
@@ -1,15 +1,15 @@
1
- require_relative "../../lib/ruby-irail/parser/document_parser.rb"
1
+ require_relative "../../../../lib/ruby-irail/packages/mivbstib/parser/document_parser.rb"
2
2
 
3
- describe IRail::DocumentParser do
3
+ describe IRail::MIVBSTIB::DocumentParser do
4
4
  module Nokogiri
5
5
  module XML
6
6
  end
7
7
  end
8
8
 
9
- class StationParser
9
+ class IRail::MIVBSTIB::StationParser
10
10
  end
11
11
 
12
- class Station
12
+ class IRail::MIVBSTIB::Station
13
13
  end
14
14
 
15
15
  describe :'self.parse_stations' do
@@ -21,35 +21,35 @@ describe IRail::DocumentParser do
21
21
  before :each do
22
22
  Nokogiri.stub(:XML => xml_payload)
23
23
  xml_payload.stub(:xpath => stations)
24
- IRail::StationParser.stub(:parse => attributes)
24
+ IRail::MIVBSTIB::StationParser.stub(:parse => attributes)
25
25
  end
26
26
 
27
27
  it "gets the xml payload from the string passed as parameter" do
28
28
  Nokogiri.should_receive(:XML).with(xml_string)
29
- IRail::DocumentParser.parse_stations(xml_string)
29
+ IRail::MIVBSTIB::DocumentParser.parse_stations(xml_string)
30
30
  end
31
31
 
32
32
  it "extracts the stations from the xml payload" do
33
- xml_payload.should_receive(:xpath).with(IRail::DocumentParser::STATION_XPATH)
34
- IRail::DocumentParser.parse_stations(xml_string)
33
+ xml_payload.should_receive(:xpath).with(IRail::MIVBSTIB::DocumentParser::STATION_XPATH)
34
+ IRail::MIVBSTIB::DocumentParser.parse_stations(xml_string)
35
35
  end
36
36
 
37
37
  it "parses the attributes of all stations" do
38
38
  stations.each do |xml_station|
39
- IRail::StationParser.should_receive(:parse).with(xml_station)
39
+ IRail::MIVBSTIB::StationParser.should_receive(:parse).with(xml_station)
40
40
  end
41
- IRail::DocumentParser.parse_stations(xml_string)
41
+ IRail::MIVBSTIB::DocumentParser.parse_stations(xml_string)
42
42
  end
43
43
 
44
44
  it "creates a new station for all parsed station attributes" do
45
45
  stations.each do |station|
46
- IRail::Station.should_receive(:new).with(attributes)
46
+ IRail::MIVBSTIB::Station.should_receive(:new).with(attributes)
47
47
  end
48
- IRail::DocumentParser.parse_stations(xml_string)
48
+ IRail::MIVBSTIB::DocumentParser.parse_stations(xml_string)
49
49
  end
50
50
 
51
51
  it "returns as many stations as in the xml payload" do
52
- IRail::DocumentParser.parse_stations(xml_string).size.should eql stations.size
52
+ IRail::MIVBSTIB::DocumentParser.parse_stations(xml_string).size.should eql stations.size
53
53
  end
54
54
  end
55
55
  end
@@ -0,0 +1,43 @@
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
@@ -0,0 +1,129 @@
1
+ require_relative "../../../lib/ruby-irail/packages/nmbs/api.rb"
2
+
3
+ describe IRail::API::NMBS do
4
+ let(:irail) { IRail::API::NMBS.new }
5
+
6
+ describe :stations do
7
+ let(:stations) { mock("Stations") }
8
+ let(:fetched_stations) { mock("Fetched stations") }
9
+
10
+ before :each do
11
+ irail.stub(:get_stations => fetched_stations)
12
+ end
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
29
+ end
30
+
31
+ describe :connections do
32
+ let(:origin_station) { mock("Origin station") }
33
+ let(:destination_station) { mock("Destination station") }
34
+ let(:connections_url) { mock("Connections url") }
35
+ let(:xml_connections) { mock("Xml connections") }
36
+ let(:connections) { mock("Connections") }
37
+
38
+ before :each do
39
+ irail.stub(:build_connections_url => connections_url)
40
+ irail.stub(:get_connections => xml_connections)
41
+ IRail::NMBS::DocumentParser.stub(:parse_connections => connections)
42
+ end
43
+
44
+ it "builds the connections url" do
45
+ irail.should_receive(:build_connections_url)
46
+ irail.connections(origin_station, destination_station)
47
+ end
48
+
49
+ it "gets the connections" do
50
+ irail.should_receive(:get_connections).with(connections_url, origin_station, destination_station)
51
+ irail.connections(origin_station, destination_station)
52
+ end
53
+
54
+ it "returns the parsed connections" do
55
+ irail.connections(origin_station, destination_station).should eql connections
56
+ end
57
+ end
58
+
59
+ describe :get_stations do
60
+ let(:station_list_url) { mock("Url") }
61
+ let(:xml_station_list) { mock("Xml station list") }
62
+ let(:stations) { mock("Stations") }
63
+
64
+ before :each do
65
+ irail.stub(:build_station_list_url => station_list_url)
66
+ irail.stub(:get_station_list => xml_station_list)
67
+ IRail::NMBS::DocumentParser.stub(:parse_stations => stations)
68
+ end
69
+
70
+ it "builds the station list url" do
71
+ irail.should_receive(:build_station_list_url)
72
+ irail.get_stations
73
+ end
74
+
75
+ it "gets the xml station list" do
76
+ irail.should_receive(:get_station_list).with(station_list_url)
77
+ irail.get_stations
78
+ end
79
+
80
+ it "returns the parsed stations" do
81
+ irail.get_stations.should eql stations
82
+ end
83
+ end
84
+
85
+ describe :get_connections do
86
+ let(:origin_station) { mock("Origin station") }
87
+ let(:destination_station) { mock("Destination station") }
88
+ let(:connections_url) { mock("Connections url") }
89
+ let(:options_hash) { mock("Options hash") }
90
+ let(:response) { mock("Response") }
91
+
92
+ before :each do
93
+ irail.stub(:build_connections_option_hash => options_hash)
94
+ IRail::Request.stub(:get => response)
95
+ end
96
+
97
+ it "builds the options hash" do
98
+ irail.should_receive(:build_connections_option_hash).with(origin_station, destination_station)
99
+ irail.get_connections(connections_url, origin_station, destination_station)
100
+ end
101
+
102
+ it "calls the connections url through a get request" do
103
+ IRail::Request.should_receive(:get).with(connections_url, options_hash)
104
+ irail.get_connections(connections_url, origin_station, destination_station)
105
+ end
106
+
107
+ it "returns the response" do
108
+ irail.get_connections(connections_url, origin_station, destination_station).should eql response
109
+ end
110
+ end
111
+
112
+ describe :get_station_list do
113
+ let(:url) { mock("Url") }
114
+ let(:response) { mock("Response") }
115
+
116
+ before :each do
117
+ IRail::Request.stub(:get => response)
118
+ end
119
+
120
+ it "calls the station list url through a get request" do
121
+ IRail::Request.should_receive(:get).with(url)
122
+ irail.get_station_list(url)
123
+ end
124
+
125
+ it "returns the response" do
126
+ irail.get_station_list(url).should eql response
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,29 @@
1
+ require_relative '../../../../lib/ruby-irail/packages/nmbs/models/connection.rb'
2
+
3
+ describe IRail::NMBS::Connection do
4
+ let(:connection) { IRail::NMBS::Connection.new }
5
+
6
+ it "has an id" do
7
+ connection.should respond_to(:id)
8
+ end
9
+
10
+ it "has a departure" do
11
+ connection.should respond_to(:departure)
12
+ end
13
+
14
+ it "has an arrival" do
15
+ connection.should respond_to(:arrival)
16
+ end
17
+
18
+ it "has a trip" do
19
+ connection.should respond_to(:trip)
20
+ end
21
+
22
+ it "has a duration in seconds" do
23
+ connection.should respond_to(:duration)
24
+ end
25
+
26
+ it "says if there is a delay" do
27
+ connection.should respond_to(:delay)
28
+ end
29
+ end
@@ -0,0 +1,13 @@
1
+ require_relative '../../../../lib/ruby-irail/packages/nmbs/models/platform.rb'
2
+
3
+ describe IRail::NMBS::Platform do
4
+ let(:platform) { IRail::NMBS::Platform.new }
5
+
6
+ it "has an id" do
7
+ platform.should respond_to(:number)
8
+ end
9
+
10
+ it "gives information wether the platform is normal or not" do
11
+ platform.should respond_to(:normal?)
12
+ end
13
+ end
@@ -1,7 +1,7 @@
1
- require_relative '../../lib/ruby-irail/models/station.rb'
1
+ require_relative '../../../../lib/ruby-irail/packages/nmbs/models/station.rb'
2
2
 
3
- describe IRail::Station do
4
- let(:station) { IRail::Station.new }
3
+ describe IRail::NMBS::Station do
4
+ let(:station) { IRail::NMBS::Station.new }
5
5
 
6
6
  it "has an id" do
7
7
  station.should respond_to(:id)
@@ -0,0 +1,29 @@
1
+ require_relative '../../../../lib/ruby-irail/packages/nmbs/models/step.rb'
2
+
3
+ describe IRail::NMBS::Step do
4
+ let(:step) { IRail::NMBS::Step.new }
5
+
6
+ it "has a delay in seconds" do
7
+ step.should respond_to(:delay)
8
+ end
9
+
10
+ it "has a station" do
11
+ step.should respond_to(:station)
12
+ end
13
+
14
+ it "has a unix time" do
15
+ step.should respond_to(:unix_time)
16
+ end
17
+
18
+ it "has a vehicle" do
19
+ step.should respond_to(:vehicle)
20
+ end
21
+
22
+ it "has a platform" do
23
+ step.should respond_to(:platform)
24
+ end
25
+
26
+ it "has a direction station" do
27
+ step.should respond_to(:direction)
28
+ end
29
+ end
@@ -0,0 +1,33 @@
1
+ require_relative '../../../../lib/ruby-irail/packages/nmbs/models/stopover.rb'
2
+
3
+ describe IRail::NMBS::Stopover do
4
+ let(:stopover) { IRail::NMBS::Stopover.new }
5
+
6
+ it "has an id" do
7
+ stopover.should respond_to(:id)
8
+ end
9
+
10
+ it "has a station" do
11
+ stopover.should respond_to(:station)
12
+ end
13
+
14
+ it "has a vehicle" do
15
+ stopover.should respond_to(:vehicle)
16
+ end
17
+
18
+ it "has an arrival" do
19
+ stopover.should respond_to(:arrival)
20
+ end
21
+
22
+ it "has a departure" do
23
+ stopover.should respond_to(:departure)
24
+ end
25
+
26
+ it "has a direction station" do
27
+ stopover.should respond_to(:direction)
28
+ end
29
+
30
+ it "has a wait time in seconds" do
31
+ stopover.should respond_to(:wait_time)
32
+ end
33
+ end
@@ -0,0 +1,13 @@
1
+ require_relative '../../../../lib/ruby-irail/packages/nmbs/models/trip.rb'
2
+
3
+ describe IRail::NMBS::Trip do
4
+ let(:trip) { IRail::NMBS::Trip.new }
5
+
6
+ it "has a count of stopovers" do
7
+ trip.should respond_to(:stopover_count)
8
+ end
9
+
10
+ it "has stopovers" do
11
+ trip.should respond_to(:stopovers)
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ require_relative '../../../../lib/ruby-irail/packages/nmbs/models/unix_time.rb'
2
+
3
+ describe IRail::NMBS::UnixTime do
4
+ let(:unix_time) { IRail::NMBS::UnixTime.new }
5
+
6
+ it "has a format" do
7
+ unix_time.should respond_to(:format)
8
+ end
9
+
10
+ it "has a unixtime" do
11
+ unix_time.should respond_to(:time)
12
+ end
13
+ end
@@ -0,0 +1,9 @@
1
+ require_relative '../../../../lib/ruby-irail/packages/nmbs/models/vehicle.rb'
2
+
3
+ describe IRail::NMBS::Vehicle do
4
+ let(:vehicle) { IRail::NMBS::Vehicle.new }
5
+
6
+ it "has an id" do
7
+ vehicle.should respond_to(:id)
8
+ end
9
+ end
@@ -0,0 +1,80 @@
1
+ require_relative "../../../../lib/ruby-irail/packages/nmbs/parser/document_parser.rb"
2
+
3
+ describe IRail::NMBS::DocumentParser do
4
+ module Nokogiri
5
+ module XML
6
+ end
7
+ end
8
+
9
+ class IRail::NMBS::Station
10
+ end
11
+
12
+ class IRail::NMBS::Connection
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
+
20
+ before :each do
21
+ Nokogiri.stub(:XML => xml_payload)
22
+ xml_payload.stub(:xpath => stations)
23
+ IRail::NMBS::Station.stub(:from_xml)
24
+ end
25
+
26
+ it "gets the xml payload from the string passed as parameter" do
27
+ Nokogiri.should_receive(:XML).with(xml_string)
28
+ IRail::NMBS::DocumentParser.parse_stations(xml_string)
29
+ end
30
+
31
+ it "extracts the stations from the xml payload" do
32
+ xml_payload.should_receive(:xpath).with(IRail::NMBS::DocumentParser::STATION_XPATH)
33
+ IRail::NMBS::DocumentParser.parse_stations(xml_string)
34
+ end
35
+
36
+ it "creates a new station for all parsed station attributes" do
37
+ stations.each do |station|
38
+ IRail::NMBS::Station.should_receive(:from_xml).with(station.to_s)
39
+ end
40
+ IRail::NMBS::DocumentParser.parse_stations(xml_string)
41
+ end
42
+
43
+ it "returns as many stations as in the xml payload" do
44
+ IRail::NMBS::DocumentParser.parse_stations(xml_string).size.should eql stations.size
45
+ end
46
+ end
47
+
48
+ describe :'self.parse_connections' do
49
+ let(:xml_string) { mock("Xml string") }
50
+ let(:xml_payload) { mock("Xml payload") }
51
+ let(:connections) { [ mock("Connection"), mock("Connection"), mock("Connection") ] }
52
+
53
+ before :each do
54
+ Nokogiri.stub(:XML => xml_payload)
55
+ xml_payload.stub(:xpath => connections)
56
+ IRail::NMBS::Connection.stub(:from_xml)
57
+ end
58
+
59
+ it "gets the xml payload from the string passed as parameter" do
60
+ Nokogiri.should_receive(:XML).with(xml_string)
61
+ IRail::NMBS::DocumentParser.parse_connections(xml_string)
62
+ end
63
+
64
+ it "extracts the connections from the xml payload" do
65
+ xml_payload.should_receive(:xpath).with(IRail::NMBS::DocumentParser::CONNECTION_XPATH)
66
+ IRail::NMBS::DocumentParser.parse_connections(xml_string)
67
+ end
68
+
69
+ it "creates a new connection for all parsed connection attributes" do
70
+ connections.each do |connection|
71
+ IRail::NMBS::Connection.should_receive(:from_xml).with(connection.to_s)
72
+ end
73
+ IRail::NMBS::DocumentParser.parse_connections(xml_string)
74
+ end
75
+
76
+ it "returns as many connections as in the xml payload" do
77
+ IRail::NMBS::DocumentParser.parse_connections(xml_string).size.should eql connections.size
78
+ end
79
+ end
80
+ 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.1.0
4
+ version: 0.2.0
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-17 00:00:00.000000000Z
12
+ date: 2012-07-18 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70309396504580 !ruby/object:Gem::Requirement
16
+ requirement: &70337783379580 !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: *70309396504580
24
+ version_requirements: *70337783379580
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: httparty
27
- requirement: &70309396504160 !ruby/object:Gem::Requirement
27
+ requirement: &70337783379160 !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: *70309396504160
35
+ version_requirements: *70337783379160
36
36
  - !ruby/object:Gem::Dependency
37
- name: virtus
38
- requirement: &70309396503740 !ruby/object:Gem::Requirement
37
+ name: roxml
38
+ requirement: &70337783378700 !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: *70309396503740
46
+ version_requirements: *70337783378700
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: nokogiri
49
- requirement: &70309396503320 !ruby/object:Gem::Requirement
49
+ requirement: &70337783378280 !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: *70309396503320
57
+ version_requirements: *70337783378280
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:
@@ -73,16 +73,37 @@ files:
73
73
  - lib/ruby-irail.rb
74
74
  - lib/ruby-irail/api.rb
75
75
  - lib/ruby-irail/http/request.rb
76
- - lib/ruby-irail/models/station.rb
77
- - lib/ruby-irail/parser/document_parser.rb
78
- - lib/ruby-irail/parser/station_parser.rb
76
+ - lib/ruby-irail/packages/mivbstib/api.rb
77
+ - lib/ruby-irail/packages/mivbstib/models/station.rb
78
+ - lib/ruby-irail/packages/mivbstib/parser/document_parser.rb
79
+ - lib/ruby-irail/packages/mivbstib/parser/station_parser.rb
80
+ - lib/ruby-irail/packages/nmbs/api.rb
81
+ - lib/ruby-irail/packages/nmbs/models/connection.rb
82
+ - lib/ruby-irail/packages/nmbs/models/platform.rb
83
+ - lib/ruby-irail/packages/nmbs/models/station.rb
84
+ - lib/ruby-irail/packages/nmbs/models/step.rb
85
+ - lib/ruby-irail/packages/nmbs/models/stopover.rb
86
+ - lib/ruby-irail/packages/nmbs/models/trip.rb
87
+ - lib/ruby-irail/packages/nmbs/models/unix_time.rb
88
+ - lib/ruby-irail/packages/nmbs/models/vehicle.rb
89
+ - lib/ruby-irail/packages/nmbs/parser/document_parser.rb
79
90
  - lib/version.rb
80
91
  - ruby-irail.gemspec
81
92
  - spec/http/request_spec.rb
82
- - spec/irail_spec.rb
83
- - spec/models/station_spec.rb
84
- - spec/parser/document_parser_spec.rb
85
- - spec/parser/station_parser_spec.rb
93
+ - spec/packages/mivbstib/api_spec.rb
94
+ - spec/packages/mivbstib/models/station_spec.rb
95
+ - spec/packages/mivbstib/parser/document_parser_spec.rb
96
+ - spec/packages/mivbstib/parser/station_parser_spec.rb
97
+ - spec/packages/nmbs/api_spec.rb
98
+ - spec/packages/nmbs/models/connection_spec.rb
99
+ - spec/packages/nmbs/models/platform_spec.rb
100
+ - spec/packages/nmbs/models/station_spec.rb
101
+ - spec/packages/nmbs/models/step_spec.rb
102
+ - spec/packages/nmbs/models/stopover_spec.rb
103
+ - spec/packages/nmbs/models/trip_spec.rb
104
+ - spec/packages/nmbs/models/unix_time_spec.rb
105
+ - spec/packages/nmbs/models/vehicle_spec.rb
106
+ - spec/packages/nmbs/parser/document_parser_spec.rb
86
107
  homepage: https://github.com/mlainez/ruby-irail
87
108
  licenses: []
88
109
  post_install_message:
@@ -109,7 +130,17 @@ specification_version: 3
109
130
  summary: Ruby Irail is a wrapper around IRail API.
110
131
  test_files:
111
132
  - spec/http/request_spec.rb
112
- - spec/irail_spec.rb
113
- - spec/models/station_spec.rb
114
- - spec/parser/document_parser_spec.rb
115
- - spec/parser/station_parser_spec.rb
133
+ - spec/packages/mivbstib/api_spec.rb
134
+ - spec/packages/mivbstib/models/station_spec.rb
135
+ - spec/packages/mivbstib/parser/document_parser_spec.rb
136
+ - spec/packages/mivbstib/parser/station_parser_spec.rb
137
+ - spec/packages/nmbs/api_spec.rb
138
+ - spec/packages/nmbs/models/connection_spec.rb
139
+ - spec/packages/nmbs/models/platform_spec.rb
140
+ - spec/packages/nmbs/models/station_spec.rb
141
+ - spec/packages/nmbs/models/step_spec.rb
142
+ - spec/packages/nmbs/models/stopover_spec.rb
143
+ - spec/packages/nmbs/models/trip_spec.rb
144
+ - spec/packages/nmbs/models/unix_time_spec.rb
145
+ - spec/packages/nmbs/models/vehicle_spec.rb
146
+ - spec/packages/nmbs/parser/document_parser_spec.rb
@@ -1,50 +0,0 @@
1
- require_relative "../../lib/ruby-irail/parser/station_parser.rb"
2
-
3
- describe IRail::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(:standard_name) { mock("Standard Name") }
10
- let(:name) { mock("Name") }
11
-
12
- before :each do
13
- IRail::StationParser.stub(parse_station_id: id)
14
- IRail::StationParser.stub(parse_station_latitude: latitude)
15
- IRail::StationParser.stub(parse_station_longitude: longitude)
16
- IRail::StationParser.stub(parse_station_standard_name: standard_name)
17
- IRail::StationParser.stub(parse_station_name: name)
18
- end
19
-
20
- it "parses the id" do
21
- IRail::StationParser.should_receive(:parse_station_id).with(xml_payload)
22
- IRail::StationParser.parse(xml_payload)
23
- end
24
-
25
- it "parses the latitude" do
26
- IRail::StationParser.should_receive(:parse_station_latitude).with(xml_payload)
27
- IRail::StationParser.parse(xml_payload)
28
- end
29
-
30
- it "parses the longitude" do
31
- IRail::StationParser.should_receive(:parse_station_longitude).with(xml_payload)
32
- IRail::StationParser.parse(xml_payload)
33
- end
34
-
35
- it "parses the standard name" do
36
- IRail::StationParser.should_receive(:parse_station_standard_name).with(xml_payload)
37
- IRail::StationParser.parse(xml_payload)
38
- end
39
-
40
- it "parses the name" do
41
- IRail::StationParser.should_receive(:parse_station_name).with(xml_payload)
42
- IRail::StationParser.parse(xml_payload)
43
- end
44
-
45
- it "returns a hash containing the parsed attributes" do
46
- expected = { id: id, latitude: latitude, longitude: longitude, standard_name: standard_name, name: name}
47
- IRail::StationParser.parse(xml_payload).should eql expected
48
- end
49
- end
50
- end