smtuc 0.0.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: ff1d090978c1caec8da1062e3db14c9385485a0e
4
+ data.tar.gz: 8ac4932cebfd9f151194ae61b390609770c28153
5
+ SHA512:
6
+ metadata.gz: 2ad6bcb139a7915cf5aa86ef935f0e45feed2bc95672ce45ea0326549e50153a439d20c995176c4ce78ba1dc2a01e07ee6dcd9c57b5681fbe49a72df18374f7f
7
+ data.tar.gz: 54a0c55ac92ab4a32e7e19c98fad5bb4830b62666c4302afb411c66aa44ad2d1235715a4b34afad11005349ccc478bfc7e298851293122181d1500b36b5e7fe4
data/lib/smtuc/line.rb ADDED
@@ -0,0 +1,67 @@
1
+ class SMTUC
2
+ class Line
3
+ attr_accessor :id, :description, :directions
4
+
5
+ API_URL = 'http://coimbra.move-me.mobi/Lines'.freeze
6
+
7
+ # Directions are returned in a funky format that makes little sense, but
8
+ # looks like: 'R§Volta§Tovim - Est. Nova'
9
+ #
10
+ # So we define a regex that captures all 3 parts into capture groups:
11
+ # 1: R
12
+ # 2: Volta
13
+ # 3: Tovim - Est. Nova
14
+ DIRECTION_REGEX = /([a-zA-Z0-9_])§([a-zA-Z0-9_]*)§(.*)/
15
+
16
+ def initialize(id, description, directions = nil)
17
+ @id = id
18
+ @description = description
19
+ @directions = directions if directions
20
+ end
21
+
22
+ # Returns a list of all known lines
23
+ def self.all
24
+ response = Faraday.get(API_URL + '/GetLines?providerName=SMTUC')
25
+ lines = JSON.parse(response.body)
26
+ lines.map { |line| new_from_json(line) }
27
+ end
28
+
29
+ # Returns information about a specific line
30
+ def self.find(id)
31
+ # Because there's no known endpoint for specific line details, we
32
+ # fetch all of them and get the id/description information from that.
33
+ # This is a bit gross and maybe there's a simpler way I'm not seeing
34
+ line = all.select { |l| l.id == id }.first
35
+
36
+ # Augment the line object with directions from the directions endpoint.
37
+ response = Faraday.get(API_URL + '/GetDirections?lineCode=SMTUC_' + id.to_s)
38
+ line.directions = JSON.parse(response.body).map { |d| parse_directions(d) }
39
+
40
+ # Return the final object
41
+ line
42
+ end
43
+
44
+ # Initialize a Line object based on API information
45
+ def self.new_from_json(attributes)
46
+ # Key is typically SMTUC_<id>, so we retain just the id
47
+ id = attributes['Key'].gsub('SMTUC_', '')
48
+ # Line includes the id again in most cases, so we strip it out
49
+ description = attributes['Value'].gsub(id + ' ', '')
50
+ # Initialize the line object based on the json
51
+ new(id, description)
52
+ end
53
+
54
+ # Returns a parsed direction object based on the way directions are reported
55
+ def self.parse_directions(directions)
56
+ # Match the directions using the regex defined above
57
+ matches = DIRECTION_REGEX.match(directions)
58
+
59
+ # Return a final direction object that looks like:
60
+ # {
61
+ # direction: "volta",
62
+ # description: "Tovim - Est. Nova"
63
+ # }
64
+ { direction: matches[2].downcase, description: matches[3] }
65
+ end
66
+ end
67
+ end
data/lib/smtuc/stop.rb ADDED
@@ -0,0 +1,24 @@
1
+ class SMTUC
2
+ class Stop
3
+ attr_accessor :id, :name, :lat, :lon
4
+
5
+ # This is a bit hacky because the only way to grab all stops is to do a
6
+ # lat/lon search and expand the range as high as it will go. In theory,
7
+ # this will return every stop in the system.
8
+ API_URL = 'http://coimbra.move-me.mobi/Stops/GetStops?oLat=40.20343598944182&oLon=-8.417298279776674&meters=99999999999'.freeze
9
+
10
+ def initialize(attributes)
11
+ @id = attributes['Code'].gsub('SMTUC_', '')
12
+ @name = attributes['Name'].strip
13
+ @lat = attributes['CoordX']
14
+ @lon = attributes['CoordY']
15
+ end
16
+
17
+ # Returns a list of all known stops
18
+ def self.all
19
+ response = Faraday.get(API_URL)
20
+ lines = JSON.parse(response.body)
21
+ lines.map { |line| new(line) }
22
+ end
23
+ end
24
+ end
data/lib/smtuc.rb ADDED
@@ -0,0 +1,11 @@
1
+ # Load bundle gems
2
+ require 'rubygems'
3
+ require 'bundler/setup'
4
+ require 'faraday'
5
+ require 'json'
6
+
7
+ class SMTUC
8
+ end
9
+
10
+ require 'smtuc/line'
11
+ require 'smtuc/stop'
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smtuc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Fred Oliveira
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-06-08 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A non-official gem on top of a non-fficial API to SMTUC
14
+ email: fred@helloform.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/smtuc.rb
20
+ - lib/smtuc/line.rb
21
+ - lib/smtuc/stop.rb
22
+ homepage: https://github.com/fredoliveira/smtuc-gem
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.6.11
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: SMTUC, the gem!
46
+ test_files: []