Skip to content

Instantly share code, notes, and snippets.

@cheesepaulo
Created May 16, 2017 15:01
Show Gist options
  • Save cheesepaulo/574954684a953668f94f67554520fbb8 to your computer and use it in GitHub Desktop.
Save cheesepaulo/574954684a953668f94f67554520fbb8 to your computer and use it in GitHub Desktop.
require 'json'
require 'httpclient'
module SPTrans
class APIService
API_URL = "http://api.olhovivo.sptrans.com.br/v0"
TOKEN = "5e20a6e514afa82121420017cbaf867bc2fca8b13c30f76b3c179f42f10ae486"
def initialize
@client = HTTPClient.new
connect(@client)
end
def connect(client)
begin
@client.post("#{API_URL}/login/autenticar?token=#{TOKEN}", {})
rescue Exception => e
puts e.message
puts e.backtrace.inspect
end
end
def getUpcomingBus(stop_code)
# Recebe um ponto de parada e retorna uma lista com a previsão
# de chegada dos veículos até aquele ponto.
begin
response = @client.get "#{API_URL}/Previsao/Parada?codigoParada=#{stop_code}", {}
JSON.parse(response.body)
rescue Exception => e
puts e.message
puts e.backtrace.inspect
end
end
def getBusPosition(line_code)
begin
response = @client.get "#{API_URL}/Posicao?codigoLinha=#{line_code}", {}
JSON.parse(response.body)
rescue Exception => e
puts e.message
puts e.backtrace.inspect
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment