Class: SMTUC::Line
- Inherits:
-
Object
- Object
- SMTUC::Line
- Defined in:
- lib/smtuc/line.rb
Constant Summary collapse
- API_URL =
'http://coimbra.move-me.mobi/Lines'.freeze
- DIRECTION_REGEX =
Directions are returned in a funky format that makes little sense, but looks like: ‘R§Volta§Tovim - Est. Nova’
So we define a regex that captures all 3 parts into capture groups: 1: R 2: Volta 3: Tovim - Est. Nova
/([a-zA-Z0-9_])§([a-zA-Z0-9_]*)§(.*)/
Instance Attribute Summary collapse
-
#description ⇒ Object
Returns the value of attribute description.
-
#directions ⇒ Object
Returns the value of attribute directions.
-
#id ⇒ Object
Returns the value of attribute id.
Class Method Summary collapse
-
.all ⇒ Object
Returns a list of all known lines.
-
.find(id) ⇒ Object
Returns information about a specific line.
-
.new_from_json(attributes) ⇒ Object
Initialize a Line object based on API information.
-
.parse_directions(directions) ⇒ Object
Returns a parsed direction object based on the way directions are reported.
Instance Method Summary collapse
-
#initialize(id, description, directions = nil) ⇒ Line
constructor
A new instance of Line.
Constructor Details
#initialize(id, description, directions = nil) ⇒ Line
Returns a new instance of Line.
16 17 18 19 20 |
# File 'lib/smtuc/line.rb', line 16 def initialize(id, description, directions = nil) @id = id @description = description @directions = directions if directions end |
Instance Attribute Details
#description ⇒ Object
Returns the value of attribute description.
3 4 5 |
# File 'lib/smtuc/line.rb', line 3 def description @description end |
#directions ⇒ Object
Returns the value of attribute directions.
3 4 5 |
# File 'lib/smtuc/line.rb', line 3 def directions @directions end |
#id ⇒ Object
Returns the value of attribute id.
3 4 5 |
# File 'lib/smtuc/line.rb', line 3 def id @id end |
Class Method Details
.all ⇒ Object
Returns a list of all known lines
23 24 25 26 27 |
# File 'lib/smtuc/line.rb', line 23 def self.all response = Faraday.get(API_URL + '/GetLines?providerName=SMTUC') lines = JSON.parse(response.body) lines.map { |line| new_from_json(line) } end |
.find(id) ⇒ Object
Returns information about a specific line
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/smtuc/line.rb', line 30 def self.find(id) # Because there's no known endpoint for specific line details, we # fetch all of them and get the id/description information from that. # This is a bit gross and maybe there's a simpler way I'm not seeing line = all.select { |l| l.id == id }.first # Augment the line object with directions from the directions endpoint. response = Faraday.get(API_URL + '/GetDirections?lineCode=SMTUC_' + id.to_s) line.directions = JSON.parse(response.body).map { |d| parse_directions(d) } # Return the final object line end |
.new_from_json(attributes) ⇒ Object
Initialize a Line object based on API information
45 46 47 48 49 50 51 52 |
# File 'lib/smtuc/line.rb', line 45 def self.new_from_json(attributes) # Key is typically SMTUC_<id>, so we retain just the id id = attributes['Key'].gsub('SMTUC_', '') # Line includes the id again in most cases, so we strip it out description = attributes['Value'].gsub(id + ' ', '') # Initialize the line object based on the json new(id, description) end |
.parse_directions(directions) ⇒ Object
Returns a parsed direction object based on the way directions are reported
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/smtuc/line.rb', line 55 def self.parse_directions(directions) # Match the directions using the regex defined above matches = DIRECTION_REGEX.match(directions) # Return a final direction object that looks like: # { # direction: "volta", # description: "Tovim - Est. Nova" # } { direction: matches[2].downcase, description: matches[3] } end |