emt_api 0.0.0.3 → 0.0.0.4
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 +4 -4
- data/README.md +59 -4
- data/lib/emt_api.rb +1 -0
- data/lib/emt_api/api.rb +17 -1
- data/lib/emt_api/line.rb +2 -2
- data/lib/emt_api/parser.rb +16 -0
- data/lib/emt_api/stop.rb +37 -0
- data/lib/emt_api/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2b88a0c69df58eb8e5ff9341922dd3e1f8c14ec7
|
4
|
+
data.tar.gz: f11991b1767a9987246224a3eda5dd94c9103b48
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e459255a76deaee063239de730c8896bdf731467964435aca5e1d89835fcdd67133097df178d9c70af8d145d06da447e0e829caf21daa3b98e78459236194de3
|
7
|
+
data.tar.gz: 01292adaf66ac1a4ba31dde2f077e9b6591ea63a8b613c478b3f2854f5a2e6a38aee7c0e9717645636c027e3f1cca5fd22027445ce4b8b5a0e119fb83f8f0db0
|
data/README.md
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
# EmtApi
|
2
2
|
|
3
|
+
[](https://badge.fury.io/rb/emt_api)
|
3
4
|
[](https://travis-ci.org/amiedes/emt_api)
|
4
5
|
[](https://coveralls.io/github/amiedes/emt_api)
|
5
6
|
|
6
|
-
|
7
|
+
`EmtApi` is a Ruby wrapper around the Madrid [EMT](http://www.emtmadrid.es/Home) API which provides an easier and more ruby-like access to the information related to Madrid city urban buses.
|
7
8
|
|
8
9
|
## Installation
|
9
10
|
|
@@ -16,7 +17,7 @@ gem 'emt_api'
|
|
16
17
|
And then execute:
|
17
18
|
|
18
19
|
```
|
19
|
-
$ bundle
|
20
|
+
$ bundle install
|
20
21
|
```
|
21
22
|
|
22
23
|
Or install it yourself as:
|
@@ -25,9 +26,61 @@ Or install it yourself as:
|
|
25
26
|
$ gem install emt_api
|
26
27
|
```
|
27
28
|
|
29
|
+
The EMT API requires you to have a `idClient` and `passKey` (called `EMT_CLIENT` and `EMT_SECRET` respectively from now on) to authenticate your requests. You can get these freely by filling [this form](http://opendata.emtmadrid.es/Formulario).
|
30
|
+
|
31
|
+
`emt_api` expects them to be accessible as environment variables. To do this on Mac OS X you'll have to edit your `.bash_profile` and add the following lines:
|
32
|
+
|
33
|
+
```bash
|
34
|
+
# ~/.bash_profile
|
35
|
+
export EMT_CLIENT="your idClient"
|
36
|
+
export EMT_SECRET="your passKey"
|
37
|
+
```
|
38
|
+
|
28
39
|
## Usage
|
29
40
|
|
30
|
-
|
41
|
+
### Get basic info about bus lines
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
require 'emt_api'
|
45
|
+
|
46
|
+
line = EmtApi::Line.find(1)
|
47
|
+
|
48
|
+
line.id # => "001"
|
49
|
+
line.label # => "1"
|
50
|
+
line.date_first # => #<Date: 2017-01-09 ((2457763j,0s,0n),+0s,2299161j)>
|
51
|
+
line.date_end # => #<Date: 2999-12-31 ((2816787j,0s,0n),+0s,2299161j)>
|
52
|
+
line.origin # => "Cristo Rey"
|
53
|
+
line.destination # => "Prosperidad"
|
54
|
+
|
55
|
+
all_lines = EmtApi::Line.all
|
56
|
+
|
57
|
+
all_lines.size # => 208
|
58
|
+
|
59
|
+
all_lines.first.id # => "001"
|
60
|
+
# etc.
|
61
|
+
```
|
62
|
+
|
63
|
+
### Get basic info about bus stops
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
require 'emt_api'
|
67
|
+
|
68
|
+
stop = EmtApi::Stop.find(1)
|
69
|
+
|
70
|
+
stop.id # => 1
|
71
|
+
stop.name # => "Av. Valdemarin - Altair"
|
72
|
+
stop.lines # => ["", "161/1/1"]
|
73
|
+
stop.wifi? # => false
|
74
|
+
stop.latitude # => 40.47004454502
|
75
|
+
stop.longitude # => -3.782887713069
|
76
|
+
|
77
|
+
all_stops = EmtApi::Stop.all
|
78
|
+
|
79
|
+
all_stops.size # => 4677
|
80
|
+
|
81
|
+
all_stops.first.id # => 1
|
82
|
+
# etc.
|
83
|
+
```
|
31
84
|
|
32
85
|
## Development
|
33
86
|
|
@@ -37,8 +90,10 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
37
90
|
|
38
91
|
## Contributing
|
39
92
|
|
40
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
93
|
+
Bug reports and pull requests are welcome on GitHub at [https://github.com/amiedes/emt_api](https://github.com/amiedes/emt_api).
|
41
94
|
|
42
95
|
## License
|
43
96
|
|
97
|
+
*Powered by EMT Madrid* - [Terms & Conditions](http://opendata.emtmadrid.es/Documentos/terminosycondiciones.aspx?lang=es-ES)
|
98
|
+
|
44
99
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/lib/emt_api.rb
CHANGED
data/lib/emt_api/api.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
require 'http'
|
2
|
-
|
2
|
+
require 'emt_api/response'
|
3
3
|
|
4
4
|
module EmtApi
|
5
5
|
class Api
|
6
6
|
GET_LIST_LINES_URL = 'https://openbus.emtmadrid.es:9443/emt-proxy-server/last/bus/GetListLines.php'
|
7
|
+
GET_NODE_LINES_URL = 'https://openbus.emtmadrid.es:9443/emt-proxy-server/last/bus/GetNodesLines.php'
|
7
8
|
EMT_CLIENT = ENV['EMT_CLIENT']
|
8
9
|
EMT_SECRET = ENV['EMT_SECRET']
|
9
10
|
|
@@ -21,5 +22,20 @@ module EmtApi
|
|
21
22
|
emt_response.valid_data? ? emt_response.data : nil
|
22
23
|
end
|
23
24
|
|
25
|
+
# Recupera todos los identificadores de parada, junto con su coordenada UTM,
|
26
|
+
# nombre y la relación de líneas/sentido que pasan por cada uno de ellos.
|
27
|
+
def self.get_nodes_lines(stop_id = nil)
|
28
|
+
|
29
|
+
http_response = HTTP.post(GET_NODE_LINES_URL, form: {
|
30
|
+
idClient: EMT_CLIENT,
|
31
|
+
passKey: EMT_SECRET,
|
32
|
+
Nodes: stop_id
|
33
|
+
})
|
34
|
+
|
35
|
+
emt_response = EmtApi::Response.new(http_response)
|
36
|
+
|
37
|
+
emt_response.valid_data? ? emt_response.data : nil
|
38
|
+
end
|
39
|
+
|
24
40
|
end
|
25
41
|
end
|
data/lib/emt_api/line.rb
CHANGED
data/lib/emt_api/parser.rb
CHANGED
@@ -8,6 +8,7 @@ module EmtApi
|
|
8
8
|
|
9
9
|
def self.parse_sentence(sentence)
|
10
10
|
sentence = remove_trailing_whitespaces(sentence)
|
11
|
+
sentence = add_spaces_between_separators(sentence)
|
11
12
|
capitalize_sentence(sentence)
|
12
13
|
end
|
13
14
|
|
@@ -15,6 +16,21 @@ module EmtApi
|
|
15
16
|
sentence.rstrip
|
16
17
|
end
|
17
18
|
|
19
|
+
def self.add_spaces_between_separators(sentence)
|
20
|
+
spaced_sentence = sentence
|
21
|
+
|
22
|
+
# Add space before the '-' character
|
23
|
+
spaced_sentence.gsub!(/(?!\s)\-/, ' -')
|
24
|
+
|
25
|
+
# Add space after the '-' character
|
26
|
+
spaced_sentence.gsub!(/\-(?!\s)/, '- ')
|
27
|
+
|
28
|
+
# Add space after the '.' used for abbreviations
|
29
|
+
spaced_sentence.gsub!(/\.(?!\s)/, '. ')
|
30
|
+
|
31
|
+
spaced_sentence
|
32
|
+
end
|
33
|
+
|
18
34
|
def self.capitalize_sentence(sentence)
|
19
35
|
sentence.split.map { |word| word.capitalize }.join(' ')
|
20
36
|
end
|
data/lib/emt_api/stop.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'byebug'
|
2
|
+
|
3
|
+
module EmtApi
|
4
|
+
class Stop
|
5
|
+
attr_accessor :id, :name, :lines, :latitude, :longitude
|
6
|
+
|
7
|
+
def initialize(stop_data = {})
|
8
|
+
@id = stop_data['node']
|
9
|
+
@name = EmtApi::Parser.parse_sentence stop_data['name']
|
10
|
+
@lines = stop_data['lines']
|
11
|
+
@wifi = (stop_data['Wifi'].to_i == 1)
|
12
|
+
@latitude = stop_data['latitude']
|
13
|
+
@longitude = stop_data['longitude']
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.all
|
17
|
+
response_data = EmtApi::Api.get_nodes_lines
|
18
|
+
stops = []
|
19
|
+
|
20
|
+
unless response_data.nil?
|
21
|
+
response_data.each { |stop| stops << EmtApi::Stop.new(stop) }
|
22
|
+
end
|
23
|
+
|
24
|
+
stops
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.find(id)
|
28
|
+
response_data = EmtApi::Api.get_nodes_lines(id)
|
29
|
+
response_data.nil? ? nil : EmtApi::Stop.new(response_data)
|
30
|
+
end
|
31
|
+
|
32
|
+
def wifi?
|
33
|
+
@wifi
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
data/lib/emt_api/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: emt_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.0.
|
4
|
+
version: 0.0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alberto Miedes Garcés
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-01-
|
11
|
+
date: 2017-01-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: http
|
@@ -130,6 +130,7 @@ files:
|
|
130
130
|
- lib/emt_api/line.rb
|
131
131
|
- lib/emt_api/parser.rb
|
132
132
|
- lib/emt_api/response.rb
|
133
|
+
- lib/emt_api/stop.rb
|
133
134
|
- lib/emt_api/version.rb
|
134
135
|
homepage: https://github.com/amiedes/emt_api
|
135
136
|
licenses:
|