Class: Cta

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/cta.rb

Class Method Summary collapse

Class Method Details

.get_directions(params = {}) ⇒ Object

A list of cardinal directions for a given route



21
22
23
24
# File 'lib/cta.rb', line 21

def self.get_directions(params = {})
  response = Cta.send('/getdirections', params)
  return response[:error] ? response : response[:response]['dir']
end

.get_next(params = {}) ⇒ Object

Next time for a stop



39
40
41
42
43
# File 'lib/cta.rb', line 39

def self.get_next(params = {})
  response = Cta.get_predictions(params)
  newest = response.kind_of?(Array) ? response.first : response
  (((Time.parse newest['prdtm']) - (Cta.get_time)) / 60).ceil
end

.get_predictions(params = {}) ⇒ Object

A list of predicted times for a stop



33
34
35
36
# File 'lib/cta.rb', line 33

def self.get_predictions(params = {})
  response = Cta.send('/getpredictions', params)
  return response[:error] ? response : response[:response]['prd']
end

.get_routes(params = {}) ⇒ Object

A list of all bus routes



15
16
17
18
# File 'lib/cta.rb', line 15

def self.get_routes(params = {})
  response = Cta.send('/getroutes', params)
  return response[:error] ? response : response[:response]['route']
end

.get_stops(params = {}) ⇒ Object

A list of stops for a given route



27
28
29
30
# File 'lib/cta.rb', line 27

def self.get_stops(params = {})
  response = Cta.send('/getstops', params)
  return response[:error] ? response : response[:response]['stop']
end

.get_time(params = {}) ⇒ Object

The current system time for the CTA API



9
10
11
12
# File 'lib/cta.rb', line 9

def self.get_time(params = {})
  response = Cta.send('/gettime')
  return response[:error] ? response : (Time.parse response[:response]['tm'])
end

.send(route, params = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cta.rb', line 45

def self.send(route, params = {})
  return { :success => false, :error => "No API key provided." } if @key.nil?
  
  params.merge!({ :key => @key })
  response = get(route, { :query => params })
  
  return { :success => false, :error => "Could not reach CTA servers." } if response.code != 200
  
  return { :success => false, :error => response['bustime_response']['error']['msg'] } if response['bustime_response']['error']
  
  { :success => true, :response => response['bustime_response'] }
end

.set_key(key) ⇒ Object



58
59
60
# File 'lib/cta.rb', line 58

def self.set_key(key)
  @key = key
end