class SMTUC::Line
Constants
- API_URL
- 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
Attributes
description[RW]
directions[RW]
id[RW]
Public Class Methods
all()
click to toggle source
Returns a list of all known lines
# 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)
click to toggle source
Returns information about a specific line
# 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(id, description, directions = nil)
click to toggle source
# File lib/smtuc/line.rb, line 16 def initialize(id, description, directions = nil) @id = id @description = description @directions = directions if directions end
new_from_json(attributes)
click to toggle source
Initialize a Line
object based on API information
# 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)
click to toggle source
Returns a parsed direction object based on the way directions are reported
# 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