transport-opendata 0.8.2

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e23c345fa314c42131c8df416d56daf3dba9e53b
4
+ data.tar.gz: 9d27cceeac918eaffcb2ebd8da5d70f6f191db7c
5
+ SHA512:
6
+ metadata.gz: 6f39b76d4a53fbbcce470fc92896fe4da70a65502778fcebee421229d65c22e0c13a46be9ea7b48ffcb70423c4708c660f7d1cdf3ed7d45d09cbf5920800a456
7
+ data.tar.gz: 34a2fa0d4426cfe0d6eb1f665a9ce3a81d3524f69816d97facdecd3aa77fb80609ece124373b3849a14d21b3ab6b76c49196495d47433437e7f4c1b812cc783d
data/lib/checkpoint.rb ADDED
@@ -0,0 +1,22 @@
1
+ #
2
+ # Checkpoint
3
+ #
4
+
5
+ require_relative 'location'
6
+ require_relative 'prognosis'
7
+
8
+ module Transport
9
+ class Checkpoint
10
+ attr_reader :station, :arrival, :departure, :platform, :prognosis
11
+
12
+ def initialize(checkpoint)
13
+ return unless checkpoint
14
+
15
+ @station = Location.new checkpoint['station']
16
+ @arrival = DateTime.parse(checkpoint['arrival']) if checkpoint['arrival']
17
+ @departure = DateTime.parse(checkpoint['departure']) if checkpoint['departure']
18
+ @platform = checkpoint['platform']
19
+ @prognosis = Prognosis.new checkpoint['prognosis']
20
+ end
21
+ end
22
+ end
data/lib/connection.rb ADDED
@@ -0,0 +1,25 @@
1
+ #
2
+ # Connection
3
+ #
4
+
5
+ require_relative 'checkpoint'
6
+ require_relative 'service'
7
+ require_relative 'section'
8
+ require_relative 'duration'
9
+
10
+ module Transport
11
+ class Connection
12
+ attr_reader :from, :to, :duration, :service, :products, :capacity1st, :capacity2nd, :sections
13
+
14
+ def initialize(connection)
15
+ @from = Checkpoint.new connection['from']
16
+ @to = Checkpoint.new connection['to']
17
+ @duration = Duration.new connection['duration']
18
+ @service = Service.new connection['service']
19
+ @products = connection['products']
20
+ @capacity1st = connection['capacity1st']
21
+ @capacity2nd = connection['capacity2nd']
22
+ @sections = connection['sections'].map { |section| Section .new section }
23
+ end
24
+ end
25
+ end
data/lib/coordinate.rb ADDED
@@ -0,0 +1,15 @@
1
+ #
2
+ # Coordinate
3
+ #
4
+
5
+ module Transport
6
+ class Coordinate
7
+ attr_reader :type, :x, :y
8
+
9
+ def initialize(coordinate)
10
+ @type = coordinate['type'].to_sym
11
+ @x = coordinate['x']
12
+ @y = coordinate['y']
13
+ end
14
+ end
15
+ end
data/lib/duration.rb ADDED
@@ -0,0 +1,24 @@
1
+ #
2
+ # Duration
3
+ #
4
+
5
+ module Transport
6
+ class Duration
7
+ attr_reader :seconds, :minutes, :hours, :days
8
+
9
+ def initialize(duration)
10
+ @days, @hours, @minutes, @seconds = duration.split(/d|:/)
11
+ @seconds = @seconds.to_i
12
+ @minutes = @minutes.to_i
13
+ @hours = @hours.to_i
14
+ @days = @days.to_i
15
+ end
16
+
17
+ def to_s
18
+ result = ""
19
+ result += "#{@days}d" if @days > 0
20
+ result += "#{@hours}h" if @hours > 0 || @days > 0
21
+ result += "#{@minutes}m"
22
+ end
23
+ end
24
+ end
data/lib/error.rb ADDED
@@ -0,0 +1,13 @@
1
+ #
2
+ # Error
3
+ #
4
+
5
+ module Transport
6
+ class Error
7
+ attr_reader :message
8
+
9
+ def initialize(error)
10
+ @message = error['message']
11
+ end
12
+ end
13
+ end
data/lib/journey.rb ADDED
@@ -0,0 +1,26 @@
1
+ #
2
+ # Journey
3
+ #
4
+
5
+ require_relative 'checkpoint'
6
+
7
+ module Transport
8
+ class Journey
9
+ attr_reader :stop, :name, :category, :categoryCode, :number, :operator, :to, :passList, :capacity1st, :capacity2nd
10
+
11
+ def initialize(journey)
12
+ return unless journey
13
+
14
+ @stop = Checkpoint.new journey['stop']
15
+ @name = journey['name']
16
+ @category = journey['category']
17
+ @categoryCode = journey['categoryCode']
18
+ @number = journey['number'].to_i
19
+ @operator = journey['operator']
20
+ @to = journey['to']
21
+ @passList = journey['passList'].map { |checkpoint| Checkpoint.new checkpoint }
22
+ @capacity1st = journey['capacity1st']
23
+ @capacity2nd = journey['capacity2nd']
24
+ end
25
+ end
26
+ end
data/lib/location.rb ADDED
@@ -0,0 +1,20 @@
1
+ #
2
+ # Locations
3
+ #
4
+
5
+ require_relative 'coordinate'
6
+
7
+ module Transport
8
+ class Location
9
+ attr_reader :id, :type, :name, :score, :coordinate, :distance
10
+
11
+ def initialize(location)
12
+ @id = location['id']
13
+ @type = location['type']
14
+ @name = location['name']
15
+ @score = location['score']
16
+ @coordinate = Coordinate.new location['coordinate']
17
+ @distance = location['distance']
18
+ end
19
+ end
20
+ end
data/lib/prognosis.rb ADDED
@@ -0,0 +1,19 @@
1
+ #
2
+ # Prognosis
3
+ #
4
+
5
+ module Transport
6
+ class Prognosis
7
+ attr_reader :platform, :departure, :arrival, :capacity1st, :capacity2nd
8
+
9
+ def initialize(prognosis)
10
+ return unless prognosis
11
+
12
+ @platform = prognosis['platform']
13
+ @departure = DateTime.parse prognosis['departure'] if prognosis['departure']
14
+ @arrival = DateTime.parse prognosis['arrival'] if prognosis['arrival']
15
+ @capacity1st = prognosis['capacity1st']
16
+ @capacity2nd = prognosis['capacity2nd']
17
+ end
18
+ end
19
+ end
data/lib/section.rb ADDED
@@ -0,0 +1,16 @@
1
+ #
2
+ # Section
3
+ #
4
+
5
+ module Transport
6
+ class Section
7
+ attr_reader :journey, :walk, :departure, :arrival
8
+
9
+ def initialize(section)
10
+ @journey = Journey.new section['journey']
11
+ @walk = section['walk']
12
+ @departure = Checkpoint.new section['departure']
13
+ @arrival = Checkpoint.new section['arrival']
14
+ end
15
+ end
16
+ end
data/lib/service.rb ADDED
@@ -0,0 +1,14 @@
1
+ #
2
+ # Service
3
+ #
4
+
5
+ module Transport
6
+ class Service
7
+ attr_reader :regular, :irregular
8
+
9
+ def initialize(service)
10
+ @regular = service['regular']
11
+ @irregular = service['irregular']
12
+ end
13
+ end
14
+ end
data/lib/stop.rb ADDED
@@ -0,0 +1,20 @@
1
+ #
2
+ # Stop
3
+ #
4
+
5
+ module Transport
6
+ class Stop
7
+ attr_reader :station, :name, :category, :number, :operator, :to
8
+
9
+ def initialize(stop)
10
+ return unless stop
11
+
12
+ @station = Location.new stop['station']
13
+ @name = stop['name']
14
+ @category = stop['category']
15
+ @number = stop['number']
16
+ @operator = stop['operator']
17
+ @to = stop['to']
18
+ end
19
+ end
20
+ end
data/lib/timetable.rb ADDED
@@ -0,0 +1,50 @@
1
+ #
2
+ # Locations
3
+ #
4
+
5
+ require 'httparty'
6
+ require_relative 'transport_factory'
7
+
8
+ module Transport
9
+ class Timetable
10
+ include HTTParty
11
+
12
+ attr_accessor :parse
13
+
14
+ base_uri 'transport.opendata.ch'
15
+
16
+ # default_params :output => 'json'
17
+
18
+ def initialize
19
+ @version = 'v1'
20
+ @parse = true
21
+ @options = { query: { } }
22
+ end
23
+
24
+ def search_station station_partial
25
+ parse_filter(self.class.get(restful_url('locations'), @options.merge(query: { query: station_partial})))
26
+ end
27
+
28
+ def search_connection from, to
29
+ parse_filter(self.class.get(restful_url('connections'), @options.merge(query: { from: from, to: to})))
30
+ end
31
+
32
+ def board_for station
33
+ parse_filter(self.class.get(restful_url('stationboard'), @options.merge(query: { station: station, limit: 15})))
34
+ end
35
+
36
+ private
37
+ def restful_url page
38
+ "/#{@version}/#{page}"
39
+ end
40
+
41
+ def parse_filter unparsed_result
42
+ if @parse
43
+ TransportFactory::create(unparsed_result)
44
+ else
45
+ unparsed_result
46
+ end
47
+ end
48
+
49
+ end
50
+ end
@@ -0,0 +1,5 @@
1
+ #
2
+ # Transport Opendata
3
+ #
4
+
5
+ require_relative 'timetable'
@@ -0,0 +1,41 @@
1
+ #
2
+ # Transport Factory
3
+ #
4
+
5
+ require 'httparty'
6
+ require_relative 'location'
7
+ require_relative 'connection'
8
+ require_relative 'journey'
9
+ require_relative 'error'
10
+
11
+ module Transport
12
+ class TransportFactory
13
+
14
+ def self.create(json)
15
+ json.each do |type, data|
16
+ case type
17
+ when 'stations'
18
+ return data.map do |station|
19
+ Location.new station
20
+ end
21
+ when 'connections'
22
+ return data.map do |connection|
23
+ Connection.new connection
24
+ end
25
+ when 'station'
26
+ next
27
+ when 'stationboard'
28
+ return data.map do |journey|
29
+ Journey.new journey
30
+ end
31
+ when 'errors'
32
+ return data.map do |error|
33
+ Error.new error
34
+ end
35
+ else
36
+ raise ArgumentError.new('Unknown Type: ' + type)
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: transport-opendata
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.8.2
5
+ platform: ruby
6
+ authors:
7
+ - Thomas Bruderer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-12-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Access to the Swiss Public Transport API of transport.opendata.ch
28
+ email: apophis@apophis.ch
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/checkpoint.rb
34
+ - lib/connection.rb
35
+ - lib/coordinate.rb
36
+ - lib/duration.rb
37
+ - lib/error.rb
38
+ - lib/journey.rb
39
+ - lib/location.rb
40
+ - lib/prognosis.rb
41
+ - lib/section.rb
42
+ - lib/service.rb
43
+ - lib/stop.rb
44
+ - lib/timetable.rb
45
+ - lib/transport-opendata.rb
46
+ - lib/transport_factory.rb
47
+ homepage: https://github.com/FreeApophis/TransportOpendata
48
+ licenses:
49
+ - MIT
50
+ metadata: {}
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubyforge_project:
67
+ rubygems_version: 2.4.8
68
+ signing_key:
69
+ specification_version: 4
70
+ summary: Swiss Public Transport API
71
+ test_files: []