ztm_warszawa 0.1.4 → 0.1.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3a09f75e478850781ed4613f9c2ea504f4e3d2c0
4
- data.tar.gz: 336b877fd3ee308266e5daa33b1c0848664ffa38
3
+ metadata.gz: ead520c5b30cebbcb503045bbfdad7b43f3eba3b
4
+ data.tar.gz: db7985726176205529c7449ec01e2e7af8caab61
5
5
  SHA512:
6
- metadata.gz: 24cbe504c66d951ab59668a24c14365151de7dce556d09f2e13829c15c3dbd04ef8f8f1af0e95f7dd226a175ee88bca8c2930c512a96462785ae0859f8df2ba3
7
- data.tar.gz: ab14856d78bebbc7e1206863881388850faaba43b2fc5c4e4d84e371f819bfbd713d93f81580f7dfce1e6fd1f25699f28ee1906222b6eb58c91f68a7f3e504bc
6
+ metadata.gz: 2e484701caabe832d7aa18f34b664c467759e41b3567d20939d17f014d1904c95db418fe9e9e1bbe2c78af4cdd12860fe21c7c034b94a0e6e8d39c1a4781ec4a
7
+ data.tar.gz: 2592fccfcdcc2fc961edd1be26a72092df33bacebb30899cf0cf4cfbaa01de7102c24777cda27f987691ef857b1df7e3fd79493430fd98ed43c5c6ff34533fe8
data/README.md CHANGED
@@ -7,6 +7,7 @@ Using this gem, you can easily:
7
7
  - find out, which bus lines depart from a given bus stop
8
8
  - get the hours of departure of a given bus line from a specified bus stop
9
9
  - get the closest bus departures for a given bus stop
10
+ - check for traffic alerts (buses/trams suspended due to traffic accidents etc.)
10
11
 
11
12
  It also seems to work with other types of stops that are under ZTM's administration (e.g. railway stations, tram stations...)
12
13
 
@@ -17,8 +18,13 @@ Add this line to your application's Gemfile:
17
18
  ```ruby
18
19
  gem 'ztm_warszawa'
19
20
  ```
21
+ or just require it with
20
22
 
21
- And then execute:
23
+ ```ruby
24
+ require 'ztm_warszawa'
25
+ ```
26
+
27
+ Then execute:
22
28
 
23
29
  $ bundle
24
30
 
@@ -34,21 +40,37 @@ This gem depends on the `httparty` gem for making HTTP requests and the `address
34
40
 
35
41
  First, create the the appropriate object:
36
42
 
37
- `ztm_api = ZtmWarszawa.new(api_key)`
43
+ `ztm_api = ZtmWarszawa.new`
38
44
 
39
- The API key has to be a string. It can be changed later on by calling `ztm_api.api_key = (new api key)`.
45
+ Most of the methods require setting an API key. You can set the API key by calling `ztm_api.api_key = (new api key)`. The API key has to be a string. If you try to use a function which requires the API key without specifying it, an NoApiKey exception will be raised.
40
46
 
41
47
  You can get an API key by registering [here](https://api.um.warszawa.pl/index.php?wcag=true&opc=8.8,2,0,0,).
42
48
 
43
49
  Now, you can use the following methods:
44
50
 
45
- - `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.
51
+ ##### Methods which require setting the API key:
52
+
53
+ - `get_stop_id(bus_stop_name)`
54
+
55
+ Queries the ZTM API to get the bus stop's ID from its name. The ID is required for all the latter operations. Raises an "BusStopNotFound" exception if the bus stop hasn't been found.
56
+
57
+ - `get_bus_lines(bus_stop_id, bus_stop_no)`
58
+
59
+ Returns an array of bus lines (e.g. 527, 141...) that depart from a given bus stop. Raises an "NoDepartures" exception when the server's response is empty (which could mean no buses depart from this stop).
60
+
61
+ - `get_line_departure_hours(bus_stop_id, bus_stop_no, bus_line)`
62
+
63
+ Returns the server response with all the hours of departure of a given bus line from a given bus stop. Night bus lines (e.g. N37, N85...) are not supported due to some server-side errors (and an "NightLineError" exception is raised)
64
+
65
+ `get_closest_departures(server_response, script_starting_time, bus_line, number_of_departures)`
66
+
67
+ Given the server's response from `get_line_departure_hours()`, finds a given number of closest departures. The result is an array of hashes with "bus_line" and "time" entries.
46
68
 
47
- - `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.
69
+ ##### Methods which don't require setting the API key:
48
70
 
49
- - `get_line_departure_hours(bus_stop_id, bus_stop_no, bus_line)`: Returns the server response with all the hours of departure of a given bus line from a given bus stop. Night bus lines (e.g. N37, N85...) are not supported due to some server-side errors.
71
+ - `check_alerts()`
50
72
 
51
- - `get_closest_departures(server_response, script_starting_time, bus_line, number_of_departures)`: Given the server's response from `get_line_departure_hours()`, finds a given number of closest departures. The result is an array of hashes with "bus_line" and "time" entries.
73
+ Checks the ZTM website's RSS feed for any traffic alerts (buses/trams suspended due to traffic accidents etc.); returns an array of hashes with "title", "description" and "link" keys.
52
74
 
53
75
  ## License
54
76
 
@@ -1,33 +1,43 @@
1
1
  require "ztm_warszawa/version"
2
2
  require 'httparty'
3
3
  require 'addressable/uri'
4
+ require 'rss'
5
+
6
+ class NoDepartures < StandardError; end
7
+ class BusStopNotFound < StandardError; end
8
+ class NightLineError < StandardError; end
9
+ class NoApiKey < StandardError; end
10
+
11
+
4
12
 
5
13
  class ZtmWarszawa
6
14
 
7
15
  attr_writer :api_key
8
16
 
9
- def initialize(api_key)
10
- @api_key = api_key
11
- end
17
+ #def initialize(api_key)
18
+ #@api_key = api_key
19
+ #end
12
20
 
13
21
  # Queries the ZTM API to get the bus stop's ID from its name.
14
22
  # The ID is required for all the latter operations.
15
23
  def get_stop_id(bus_stop_name)
24
+ raise NoApiKey, "No API key specified." if @api_key.nil?
16
25
  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
26
  url = Addressable::URI.parse(raw_url)
18
27
  response = HTTParty.get(url.normalize).parsed_response["result"]
19
- raise 'Bus stop not found' if response.empty?
28
+ raise BusStopNotFound, 'Bus stop not found' if response.empty?
20
29
  bus_stop_id = response[0]["values"][0]["value"]
21
30
  return bus_stop_id
22
31
  end
23
32
 
24
33
  # Returns an array of bus lines (e.g. 527, 141...) that depart from a given bus stop.
25
34
  def get_bus_lines(bus_stop_id, bus_stop_no)
35
+ raise NoApiKey, "No API key specified." if @api_key.nil?
26
36
  bus_lines = []
27
37
 
28
38
  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
39
  response = HTTParty.get(url).parsed_response["result"]
30
- raise 'No vehicles seem to depart from here.' if response.empty?
40
+ raise NoDepartures, 'No vehicles seem to depart from here.' if response.empty?
31
41
 
32
42
  response.each do |bus_line_info|
33
43
  bus_lines << bus_line_info["values"][0]["value"]
@@ -37,7 +47,8 @@ class ZtmWarszawa
37
47
 
38
48
  # Returns the server response with all the hours of departure of a given bus line from a given bus stop.
39
49
  def get_line_departure_hours(bus_stop_id, bus_stop_no, bus_line)
40
- raise 'Night bus lines are not supported due to some server-side errors.' if bus_line.start_with? "N"
50
+ raise NoApiKey, "No API key specified." if @api_key.nil?
51
+ raise NightLineError, 'Night bus lines are not supported due to some server-side errors.' if bus_line.start_with? "N"
41
52
  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}"
42
53
  response = HTTParty.get(url).parsed_response["result"]
43
54
  return response
@@ -58,4 +69,15 @@ class ZtmWarszawa
58
69
  return closest_departures.take(number_of_departures)
59
70
  end
60
71
 
72
+ def check_alerts
73
+ alerts = []
74
+ alert_page = HTTParty.get("http://ztm.waw.pl/rss.php?l=1&IDRss=6")
75
+ alert_feed = RSS::Parser.parse(alert_page.body)
76
+
77
+ alert_feed.items.each do |item|
78
+ alerts << {"title" => item.title, "link" => item.link, "description" => item.description}
79
+ end
80
+ return alerts
81
+ end
82
+
61
83
  end
@@ -1,3 +1,3 @@
1
1
  class ZtmWarszawa
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ztm_warszawa
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Górni
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-09-28 00:00:00.000000000 Z
11
+ date: 2016-10-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty