ruby-irail 0.1.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/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,36 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ruby-irail (0.1.0)
5
+ httparty
6
+ nokogiri
7
+ virtus
8
+
9
+ GEM
10
+ remote: http://rubygems.org/
11
+ specs:
12
+ backports (2.6.1)
13
+ diff-lcs (1.1.3)
14
+ httparty (0.8.3)
15
+ multi_json (~> 1.0)
16
+ multi_xml
17
+ multi_json (1.3.6)
18
+ multi_xml (0.5.1)
19
+ nokogiri (1.5.5)
20
+ rspec (2.11.0)
21
+ rspec-core (~> 2.11.0)
22
+ rspec-expectations (~> 2.11.0)
23
+ rspec-mocks (~> 2.11.0)
24
+ rspec-core (2.11.0)
25
+ rspec-expectations (2.11.1)
26
+ diff-lcs (~> 1.1.3)
27
+ rspec-mocks (2.11.1)
28
+ virtus (0.5.1)
29
+ backports (~> 2.6.1)
30
+
31
+ PLATFORMS
32
+ ruby
33
+
34
+ DEPENDENCIES
35
+ rspec
36
+ ruby-irail!
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Marc Lainez
2
+
3
+ MIT License
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
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # Ruby::Irail
2
+
3
+ IRail makes Belgian railway schedule easily available for anyone.
4
+
5
+ There are already several wrappers available in different languages. This is a Ruby wrapper for their API.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'ruby-irail'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install ruby-irail
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ # get a list of all belgian train stations
25
+
26
+ require "ruby-irail"
27
+ irail = IRail::API.new
28
+ irail.stations
29
+ ```
30
+
31
+ ## Contributing
32
+
33
+ 1. Fork it
34
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
35
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
36
+ 4. Push to the branch (`git push origin my-new-feature`)
37
+ 5. Create new Pull Request
38
+
39
+ ### Running the tests
40
+
41
+ All tests are in the specs folder. To run them simply run the following from the root folder of your fork:
42
+
43
+ ```ruby
44
+ rspec specs
45
+ ```
46
+
47
+ ### Contributors
48
+
49
+ Marc Lainez (@mlainez)
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ API_URL = "http://api.irail.be"
2
+ STATIONS_URI = "stations/"
3
+
4
+ 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
15
+
16
+ def get_station_list(station_list_url)
17
+ IRail::Request.get(station_list_url)
18
+ end
19
+
20
+ private
21
+ def build_station_list_url
22
+ [API_URL, STATIONS_URI].join('/')
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,8 @@
1
+ module IRail
2
+ module Request
3
+ def self.get(url)
4
+ request = HTTParty.get(url)
5
+ request.response.body
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,13 @@
1
+ require "virtus"
2
+
3
+ module IRail
4
+ class Station
5
+ include Virtus
6
+
7
+ attribute :id, String
8
+ attribute :latitude, Float
9
+ attribute :longitude, Float
10
+ attribute :standard_name, String
11
+ attribute :name, String
12
+ end
13
+ end
@@ -0,0 +1,14 @@
1
+ module IRail
2
+ class DocumentParser
3
+ STATION_XPATH = "//station"
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 = StationParser.parse(xml_station)
10
+ stations << Station.new(attributes)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,33 @@
1
+ module IRail
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
+ standard_name = parse_station_standard_name(xml_payload)
8
+ name = parse_station_name(xml_payload)
9
+ {:id => id, :latitude => latitude, :longitude => longitude,
10
+ :standard_name => standard_name, :name => name}
11
+ end
12
+
13
+ def self.parse_station_id(xml_payload)
14
+ xml_payload.attr('id')
15
+ end
16
+
17
+ def self.parse_station_latitude(xml_payload)
18
+ xml_payload.attr('locationX')
19
+ end
20
+
21
+ 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')
27
+ end
28
+
29
+ def self.parse_station_name(xml_payload)
30
+ xml_payload.content
31
+ end
32
+ end
33
+ end
data/lib/ruby-irail.rb ADDED
@@ -0,0 +1,11 @@
1
+ require "httparty"
2
+ require "nokogiri"
3
+
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
data/lib/version.rb ADDED
@@ -0,0 +1,3 @@
1
+ module IRail
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Marc Lainez"]
6
+ gem.email = ["ml@theotherguys.be"]
7
+ gem.description = %q{IRail makes Belgian railway schedule easily available for anyone. This is a Ruby wrapper for their API.}
8
+ gem.summary = %q{Ruby Irail is a wrapper around IRail API.}
9
+ gem.homepage = "https://github.com/mlainez/ruby-irail"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(spec)/})
14
+ gem.name = "ruby-irail"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = IRail::VERSION
17
+ gem.add_development_dependency "rspec"
18
+ gem.add_dependency "httparty"
19
+ gem.add_dependency "virtus"
20
+ gem.add_dependency "nokogiri"
21
+ end
@@ -0,0 +1,33 @@
1
+ require_relative "../../lib/ruby-irail/http/request.rb"
2
+
3
+ describe IRail::Request do
4
+ class HTTParty
5
+ end
6
+
7
+ describe :'self.get' do
8
+ let(:url) { mock("Url") }
9
+ let(:request) { mock("Request") }
10
+ let(:response) { mock("Response") }
11
+ let(:body) { mock("Body") }
12
+
13
+ before :each do
14
+ HTTParty.stub(:get => request)
15
+ request.stub(:response => response)
16
+ response.stub(:body => body)
17
+ end
18
+
19
+ it "calls the get method of HTTParty with the url as param" do
20
+ HTTParty.should_receive(:get).with(url)
21
+ IRail::Request.get(url)
22
+ end
23
+
24
+ it "gets the body from the response" do
25
+ response.should_receive(:body)
26
+ IRail::Request.get(url)
27
+ end
28
+
29
+ it "returns the response body" do
30
+ IRail::Request.get(url).should eql body
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,74 @@
1
+ require_relative "../lib/ruby-irail/api.rb"
2
+
3
+ describe IRail::API do
4
+ let(:irail) { IRail::API.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 :get_stations do
32
+ let(:station_list_url) { mock("Url") }
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::DocumentParser.stub(:parse_stations => stations)
40
+ end
41
+
42
+ it "builds the station list url" do
43
+ irail.should_receive(:build_station_list_url)
44
+ irail.get_stations
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
50
+ end
51
+
52
+ it "returns the parsed stations" do
53
+ irail.get_stations.should eql stations
54
+ end
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)
63
+ end
64
+
65
+ it "calls the station list url through a get request" do
66
+ IRail::Request.should_receive(:get).with(url)
67
+ irail.get_station_list(url)
68
+ end
69
+
70
+ it "returns the response" do
71
+ irail.get_station_list(url).should eql response
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,25 @@
1
+ require_relative '../../lib/ruby-irail/models/station.rb'
2
+
3
+ describe IRail::Station do
4
+ let(:station) { IRail::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 standard name" do
19
+ station.should respond_to(:standard_name)
20
+ end
21
+
22
+ it "has a name" do
23
+ station.should respond_to(:name)
24
+ end
25
+ end
@@ -0,0 +1,55 @@
1
+ require_relative "../../lib/ruby-irail/parser/document_parser.rb"
2
+
3
+ describe IRail::DocumentParser do
4
+ module Nokogiri
5
+ module XML
6
+ end
7
+ end
8
+
9
+ class StationParser
10
+ end
11
+
12
+ class 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::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::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::DocumentParser::STATION_XPATH)
34
+ IRail::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::StationParser.should_receive(:parse).with(xml_station)
40
+ end
41
+ IRail::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::Station.should_receive(:new).with(attributes)
47
+ end
48
+ IRail::DocumentParser.parse_stations(xml_string)
49
+ end
50
+
51
+ it "returns as many stations as in the xml payload" do
52
+ IRail::DocumentParser.parse_stations(xml_string).size.should eql stations.size
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,50 @@
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
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-irail
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Marc Lainez
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-17 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70309396504580 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70309396504580
25
+ - !ruby/object:Gem::Dependency
26
+ name: httparty
27
+ requirement: &70309396504160 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70309396504160
36
+ - !ruby/object:Gem::Dependency
37
+ name: virtus
38
+ requirement: &70309396503740 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *70309396503740
47
+ - !ruby/object:Gem::Dependency
48
+ name: nokogiri
49
+ requirement: &70309396503320 !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: *70309396503320
58
+ description: IRail makes Belgian railway schedule easily available for anyone. This
59
+ is a Ruby wrapper for their API.
60
+ email:
61
+ - ml@theotherguys.be
62
+ executables: []
63
+ extensions: []
64
+ extra_rdoc_files: []
65
+ files:
66
+ - .gitignore
67
+ - .rspec
68
+ - Gemfile
69
+ - Gemfile.lock
70
+ - LICENSE
71
+ - README.md
72
+ - Rakefile
73
+ - lib/ruby-irail.rb
74
+ - lib/ruby-irail/api.rb
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
79
+ - lib/version.rb
80
+ - ruby-irail.gemspec
81
+ - 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
86
+ homepage: https://github.com/mlainez/ruby-irail
87
+ licenses: []
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 1.8.10
107
+ signing_key:
108
+ specification_version: 3
109
+ summary: Ruby Irail is a wrapper around IRail API.
110
+ test_files:
111
+ - 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