kvvliveapi 0.1.1

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: 3aafbe5a2805b5304dd284a3b2660fb71d6c3b9d
4
+ data.tar.gz: 8a2f42baf316bb138a48870bad767c730d2b14f3
5
+ SHA512:
6
+ metadata.gz: 207aadb37d57d87fd2bc93d0aaf6968132e1e2526d8cc5fa0b109d66ed8a7d51d924dd54978d5daab415249eb206f3c21c12839e04ec0297f1848c3d21087106
7
+ data.tar.gz: 26fa63cdfeeae3862d5eca95babba29c2e25c14d3c554c33a24c4e228810b5fe3922b03b720d0bd3a950b1e5f2ad942b03c0ffaf0120daa1e22796ff1f9022df
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ doc/
2
+ Gemfile.lock
3
+ *.gem
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.4.2
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,22 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'kvvliveapi/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'kvvliveapi'
7
+ s.version = KVVLiveAPI::VERSION
8
+ s.date = '2018-02-05'
9
+ s.summary = "Inofficial ruby bindings for the KVV (Karlsruher Verkehrsverbund) live API."
10
+ s.description = "Allows to retrieve live information about train and bus depatures as well as information about stops."
11
+ s.authors = ["Julian Schuh"]
12
+ s.email = 'rubygems.dev@jlzmail.de'
13
+ s.files = `git ls-files -z`.split("\x0").reject do |f|
14
+ f.match(%r{^(test|spec|features)/})
15
+ end
16
+ s.require_paths = ['lib']
17
+ s.homepage = 'https://github.com/julianschuh/kvvliveapi'
18
+ s.license = 'MIT'
19
+
20
+ s.add_runtime_dependency 'faraday', '~> 0.14.0'
21
+ s.add_runtime_dependency 'activesupport', '~> 5.1'
22
+ end
data/lib/kvvliveapi.rb ADDED
@@ -0,0 +1,83 @@
1
+ require 'faraday'
2
+ require 'json'
3
+ require 'uri'
4
+ require 'cgi'
5
+
6
+ require 'active_support/all'
7
+
8
+ require 'kvvliveapi/version'
9
+ require 'kvvliveapi/constants'
10
+ require 'kvvliveapi/stop'
11
+ require 'kvvliveapi/depature'
12
+
13
+ # Main module for accessing the KVV live API.
14
+ # All API functions can be called directly on the class object.
15
+ module KVVLiveAPI
16
+ class << self
17
+
18
+ # Retrieves a list of stops whose names match a given string
19
+ #
20
+ # * +name+ - name or fragment of the name of the Stop that is searched
21
+ def stops_by_name(name)
22
+ stops('stops/byname/' + CGI.escape(name))
23
+ end
24
+
25
+ # Retrieves a list of stops close to a given set of coordinates
26
+ #
27
+ # * +lat+ - latitude
28
+ # * +long+ - longitude
29
+ def stops_by_coordinates(lat, lon)
30
+ stops('stops/bylatlon/' + ('%.6f' % lat) + '/' + ('%.6f' % lon))
31
+ end
32
+
33
+ # Retrieves a single stop object by its ID
34
+ #
35
+ # * +stop_id+ - ID of the Stop to retrieve
36
+ def stops_by_id(stop_id)
37
+ [Stop.from_json(query('stops/bystop/' + CGI.escape(stop_id)))]
38
+ end
39
+
40
+ # Retrieves a list of upcoming depatures for a specified Stop ID
41
+ #
42
+ # * +stop_id+ - ID of the Stop for which upcoming depatures should
43
+ # be retrieved
44
+ def depatures_by_stop(stop_id)
45
+ departures('departures/bystop/' + stop_id)
46
+ end
47
+
48
+ # Retrieves a list of upcoming depatures for a specified rouute
49
+ # at a specified Stop ID
50
+ #
51
+ # * +route+ - Route for which upcoming depatures should be retrieved
52
+ # * +stop_id+ - ID of the Stop for which upcoming depatures should
53
+ # be retrieved
54
+ def depatures_by_route(route, stop_id)
55
+ departures('departures/byroute/' + CGI.escape(route) + '/' + CGI.escape(stop_id))
56
+ end
57
+
58
+ private
59
+
60
+ def stops(api_path)
61
+ query(api_path)['stops'].map do |stop|
62
+ Stop.from_json(stop)
63
+ end
64
+ end
65
+
66
+ def departures(api_path)
67
+ query(api_path)['departures'].map do |stop|
68
+ Departure.from_json(stop)
69
+ end
70
+ end
71
+
72
+ def query(path, params = {})
73
+ params.merge!({ key: API_KEY })
74
+
75
+ uri = URI.parse(API_BASE + path).tap do |u|
76
+ u.query = URI.encode_www_form(params)
77
+ end
78
+
79
+ response = Faraday.get(uri)
80
+ JSON.parse!(response.body)
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,7 @@
1
+ module KVVLiveAPI
2
+ # Publicly available key needed to access the API
3
+ API_KEY = '377d840e54b59adbe53608ba1aad70e8'.freeze
4
+
5
+ # Base URL for all API calls
6
+ API_BASE = 'https://live.kvv.de/webapp/'.freeze
7
+ end
@@ -0,0 +1,84 @@
1
+ module KVVLiveAPI
2
+ # Instances of this class represent the depature
3
+ # of a vehicle of a specific route with a
4
+ # specified destination at a specified time.
5
+ #
6
+ # It contins additional information about the
7
+ # depature and vehicle.
8
+ class Departure
9
+ class << self
10
+ def from_json(json)
11
+ new(json['route'],
12
+ json['destination'],
13
+ json['direction'],
14
+ json['time'],
15
+ json['lowfloor'],
16
+ json['realtime'],
17
+ json['traction'],
18
+ json['stopPosition'])
19
+ end
20
+ end
21
+
22
+ # The route for which the depature is valid, e.g. 2 or S7
23
+ attr_reader :route
24
+
25
+ # Final destination of the vehicle
26
+ attr_reader :destination
27
+
28
+ # Number representing the direction
29
+ attr_reader :direction
30
+
31
+ # Indicates the accessibility of the used vehicle
32
+ attr_reader :lowfloor
33
+
34
+ # Indicates if the depature is a realtime estimation
35
+ attr_reader :realtime
36
+
37
+ # Specified if single or double traction is used
38
+ attr_reader :traction
39
+
40
+ # Depature time
41
+ attr_reader :time
42
+
43
+ # Number representing the "sub-stop", as in some cases
44
+ # a single stops might have different sub-stops for
45
+ # different kinds if vehicles
46
+ attr_reader :stop_position
47
+
48
+ def initialize(route, destination, direction, time, lowfloor, realtime, traction, stop_position)
49
+ @route = route
50
+ @destination = destination
51
+ @direction = direction
52
+ @lowfloor = lowfloor
53
+ @realtime = realtime
54
+ @traction = traction
55
+ @time = convert_timestr(time)
56
+ @stop_position = stop_position
57
+ end
58
+
59
+ def to_s
60
+ @route + ' (-> ' + @destination + ') @ ' + @time.getlocal.strftime('%H:%M') + ', Stop ' + @stop_position.to_s
61
+ end
62
+
63
+ private
64
+
65
+ def convert_timestr(time)
66
+ timestr = time.to_s
67
+ now = Time.now.getlocal
68
+
69
+ return now.getutc if timestr == 'sofort' || timestr == '0'
70
+
71
+ if (mtch = /^([1-9]) min$/.match(timestr))
72
+ return now.advance(minutes: mtch[1].to_i).getutc
73
+ end
74
+
75
+ if (mtch = /^([0-2]?[0-9]):([0-5][0-9])$/.match(timestr))
76
+ resulting_time = now.change(hour: mtch[1].to_i, min: mtch[2].to_i)
77
+ resulting_time += 1.day if resulting_time < now
78
+ return resulting_time.getutc
79
+ end
80
+
81
+ nil
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,34 @@
1
+ module KVVLiveAPI
2
+ # Instances of this class represent a stop operated
3
+ # by the KVV
4
+ class Stop
5
+ class << self
6
+ def from_json(json)
7
+ new(json['name'], json['id'], json['lat'], json['lon'])
8
+ end
9
+ end
10
+
11
+ # Name of the stop
12
+ attr_reader :name
13
+
14
+ # ID used to reference the stop
15
+ attr_reader :stop_id
16
+
17
+ # latitute of the location of the stop
18
+ attr_reader :lat
19
+
20
+ # longitude of the location of the stop
21
+ attr_reader :lon
22
+
23
+ def initialize(name, stop_id, lat, lon)
24
+ @name = name
25
+ @stop_id = stop_id
26
+ @lat = lat
27
+ @lon = lon
28
+ end
29
+
30
+ def to_s
31
+ @name + '(' + @stop_id.to_s + ')'
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,4 @@
1
+ module KVVLiveAPI
2
+ # Currently installed version of the Gem
3
+ VERSION = '0.1.1'.freeze
4
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kvvliveapi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Julian Schuh
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-02-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.14.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.14.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.1'
41
+ description: Allows to retrieve live information about train and bus depatures as
42
+ well as information about stops.
43
+ email: rubygems.dev@jlzmail.de
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".ruby-version"
50
+ - Gemfile
51
+ - kvvliveapi.gemspec
52
+ - lib/kvvliveapi.rb
53
+ - lib/kvvliveapi/constants.rb
54
+ - lib/kvvliveapi/depature.rb
55
+ - lib/kvvliveapi/stop.rb
56
+ - lib/kvvliveapi/version.rb
57
+ homepage: https://github.com/julianschuh/kvvliveapi
58
+ licenses:
59
+ - MIT
60
+ metadata: {}
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 2.6.13
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: Inofficial ruby bindings for the KVV (Karlsruher Verkehrsverbund) live API.
81
+ test_files: []