ztm_warszawa 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5faa3e86cba2f5cf8b58f48a21c8c18565cb7b04
4
+ data.tar.gz: a55e9d33bca875c913a42e3a9feb26233d9eee81
5
+ SHA512:
6
+ metadata.gz: c4d861d53500db71471b846691e313e89a1c288aaf84acdae1025ececd94df8252988cea1f8d75f2daa3ce82ff481a83eb068556c603f8271ade6c6649f21d88
7
+ data.tar.gz: b6beb1fac06737c10415dacd1d8d1c2e4c83e80abebb6a3fd5fc915d89d870797db348fba262b2494cf664226438747e80f96f7d2dd0c9ddc6ec695c6e5203d3
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.12.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ztm_warszawa.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Piotr Górni
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # ZtmWarszawa
2
+
3
+ This is a simple gem making querying the UM Warszawa's ZTM API easy. No-one has to reinvent the wheel anymore :)
4
+
5
+ Using this gem, you can easily:
6
+
7
+ - find out, which bus lines depart from a given bus stop
8
+ - get the hours of departure of a given bus line from a specified bus stop
9
+ - get the closest bus departures for a given bus stop
10
+
11
+ It also seems to work with other types of stops that are under ZTM's administration (e.g. railway stations, tram stations...)
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'ztm_warszawa'
19
+ ```
20
+
21
+ And then execute:
22
+
23
+ $ bundle
24
+
25
+ Or install it yourself as:
26
+
27
+ $ gem install ztm_warszawa
28
+
29
+ ## Dependencies
30
+
31
+ This gem depends on the `httparty` gem for making HTTP requests and the `addressable` gem for transcribing any Polish characters.
32
+
33
+ ## Usage
34
+
35
+ First, create the the appropriate object:
36
+
37
+ `ztm_api = ZtmWarszawa.new(api_key)`
38
+
39
+ The API key has to be a string. It can be changed later on by calling `ztm_api.api_key = (new api key)`. You can get an API key by registering [here](https://api.um.warszawa.pl/index.php?wcag=true&opc=8.8,2,0,0,).
40
+
41
+ Now, you can use the following methods:
42
+
43
+ - `get_stop_id(bus_stop_name)`: Queries the ZTM API to get the bus stop's ID from its name. The ID is required for all the latter operations.
44
+
45
+ - `get_bus_lines(bus_stop_id, bus_stop_no)`: Returns an array of bus lines (e.g. 527, 141...) that depart from a given bus stop.
46
+
47
+ - `get_closest_departures(server_response, script_starting_time, bus_line, number_of_departures)`: From the server's response from get_line_departure_hours(), find a given number of closest departures. The result is an array of hashes with "bus_line" and "time" entries.
48
+
49
+ ## License
50
+
51
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ztm_warszawa"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ class ZtmWarszawa
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,60 @@
1
+ require "ztm_warszawa/version"
2
+ require 'httparty'
3
+ require 'addressable/uri'
4
+
5
+ class ZtmWarszawa
6
+
7
+ attr_writer :api_key
8
+
9
+ def initialize(api_key)
10
+ @api_key = api_key
11
+ end
12
+
13
+ # Queries the ZTM API to get the bus stop's ID from its name.
14
+ # The ID is required for all the latter operations.
15
+ def get_stop_id(bus_stop_name)
16
+ raw_url = "https://api.um.warszawa.pl/api/action/dbtimetable_get?id=b27f4c17-5c50-4a5b-89dd-236b282bc499&name=#{bus_stop_name}&apikey=#{@api_key}"
17
+ url = Addressable::URI.parse(raw_url)
18
+ response = HTTParty.get(url.normalize).parsed_response["result"]
19
+ raise 'Bus stop not found' if response.empty?
20
+ bus_stop_id = response[0]["values"][0]["value"]
21
+ return bus_stop_id
22
+ end
23
+
24
+ # Returns an array of bus lines (e.g. 527, 141...) that depart from a given bus stop.
25
+ def get_bus_lines(bus_stop_id, bus_stop_no)
26
+ bus_lines = []
27
+
28
+ url = "https://api.um.warszawa.pl/api/action/dbtimetable_get/?id=88cd555f-6f31-43ca-9de4-66c479ad5942&busstopId=#{bus_stop_id}&busstopNr=#{bus_stop_no}&apikey=#{@api_key}"
29
+ response = HTTParty.get(url).parsed_response["result"]
30
+ raise 'No vehicles seem to depart from here.' if response.empty?
31
+
32
+ response.each do |bus_line_info|
33
+ bus_lines << bus_line_info["values"][0]["value"]
34
+ end
35
+ return bus_lines
36
+ end
37
+
38
+ # Returns the server response with all the hours of departure of a given bus line from a given bus stop (and its number).
39
+ def get_line_departure_hours(bus_stop_id, bus_stop_no, bus_line)
40
+ url = "https://api.um.warszawa.pl/api/action/dbtimetable_get?id=e923fa0e-d96c-43f9-ae6e-60518c9f3238&busstopId=#{bus_stop_id}&busstopNr=#{bus_stop_no}&line=#{bus_line}&apikey=#{@api_key}"
41
+ response = HTTParty.get(url).parsed_response["result"]
42
+ return response
43
+ end
44
+
45
+ # From the server's response from get_line_departure_hours(), find a given number of closest departures
46
+ def get_closest_departures(server_response, script_starting_time, bus_line, number_of_departures)
47
+ bus_data = server_response
48
+ closest_departures = []
49
+
50
+ bus_data.each do |bus|
51
+ diff = Time.parse(bus["values"][0]["value"]) - script_starting_time
52
+ if diff > 0
53
+ closest_departures << {"bus_line" => bus_line, "time" => bus["values"][0]["value"] }
54
+ end
55
+ end
56
+
57
+ return closest_departures.take(number_of_departures)
58
+ end
59
+
60
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ztm_warszawa/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ztm_warszawa"
8
+ spec.version = ZtmWarszawa::VERSION
9
+ spec.authors = ["Piotr Górni"]
10
+ spec.email = ["p137@gmx.com"]
11
+
12
+ spec.summary = %q{Ruby bindings for UM Warszawa ZTM's API.}
13
+ spec.description = %q{Use this gem to access the api.um.warszawa.pl public data. Only the API key is required.}
14
+ spec.homepage = ""
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency 'httparty'
23
+ spec.add_dependency 'addressable'
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.12"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ztm_warszawa
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Piotr Górni
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-09-26 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
+ - !ruby/object:Gem::Dependency
28
+ name: addressable
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.12'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.12'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description: Use this gem to access the api.um.warszawa.pl public data. Only the API
84
+ key is required.
85
+ email:
86
+ - p137@gmx.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".travis.yml"
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/setup
100
+ - lib/ztm_warszawa.rb
101
+ - lib/ztm_warszawa/version.rb
102
+ - ztm_warszawa.gemspec
103
+ homepage: ''
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.5.1
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: Ruby bindings for UM Warszawa ZTM's API.
127
+ test_files: []