Class: CTA::CustomerAlerts

Inherits:
Object
  • Object
show all
Defined in:
lib/cta_redux/customer_alerts.rb,
lib/cta_redux/api/customer_alerts.rb,
lib/cta_redux/faraday_middleware/customer_alerts_parser.rb

Defined Under Namespace

Classes: Alert, AlertsResponse, Parser, RouteStatus, RouteStatusResponse

Class Method Summary collapse

Class Method Details

.alerts!(options = {}) ⇒ CTA::CustomerAlerts::AlertsResponse

Returns alerts for given routes or stations

Examples:

CTA::CustomerAlerts.alerts!(:route => 8)

Options Hash (options):

  • :routes (Array<Integer> Array<String>, Integer, String)

    Routes to query for alerts. Not available with :station

  • :station (Integer, String)

    Station to query for alerts. Not available with :route

  • :active (true, false)

    Only return active alerts

  • :accessibility (true, false)

    Include alerts related to accessibility (elevators, etc)

  • :planned (true, false)

    Only return planned alerts

  • :recent_days (Integer)

    Only return alerts within the specified number of days

  • :before (Integer)

    Only return alerts starting prior to the specified number of days


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/cta_redux/customer_alerts.rb', line 52

def self.alerts!(options = {})
  allowed_keys = [:active, :accessibility, :planned, :routes, :station, :recent_days, :before]
  if options.keys.any? { |k| !allowed_keys.include?(k) }
    raise "Illegal argument!"
  end

  params = {}
  params.merge!({ :activeonly => options[:active] }) if options[:active]
  params.merge!({ :accessibility => options[:accessiblity] }) if options[:accessibility]
  params.merge!({ :planned => options[:planned] }) if options[:planned]

  routes = Array.wrap(options[:routes]).flatten.compact.uniq
  stations = Array.wrap(options[:station]).flatten.compact.uniq

  if stations.size > 1
    raise "Can only specify one station!"
  end

  if routes.any? && stations.any?
    raise "Cannot use route and station together!"
  end

  if options[:recent_days] && options[:before]
    raise "Cannot use recent_days and before together!"
  end

  params.merge!({ :stationid => stations.first }) if stations.any?
  params.merge!({ :routeid => routes.join(',') }) if routes.any?
  params.merge!({ :recentdays => options[:recent_days] }) if options[:recent_days]
  params.merge!({ :bystartdate => options[:before] }) if options[:before]

  connection.get('alerts.aspx', params)
end

.cacheObject

Returns the underlying cache object caching responses (if we're actually caching responses)


115
116
117
118
119
120
121
122
# File 'lib/cta_redux/customer_alerts.rb', line 115

def self.cache
  if self.cache_responses
    # This is ugly
    @cache || self.connection.builder.handlers.find { |x| x == FaradayMiddleware::Caching }.instance_variable_get(:@args).first
  else
    nil
  end
end

.cache=(cache) ⇒ Object

Note:

Setting the cache object resets the connection. If you're using the default SimpleCache strategy (built-in 60 second caching), then it will also clear the cache.

Sets the underlying cache object caching responses. Any object can be used that responds to #read, #write, and #fetch


128
129
130
131
# File 'lib/cta_redux/customer_alerts.rb', line 128

def self.cache=(cache)
  @cache = cache
  @connection = nil
end

.cache_responsestrue, false

Returns whether or not cta_redux is caching responses


102
103
104
# File 'lib/cta_redux/customer_alerts.rb', line 102

def self.cache_responses
  @cache_responses
end

.cache_responses=(should_cache) ⇒ Object

Sets whether or not cta_redux is caching responses


108
109
110
111
# File 'lib/cta_redux/customer_alerts.rb', line 108

def self.cache_responses=(should_cache)
  @cache_responses = should_cache
  @connection = nil
end

.connectionObject

Returns the connection object we use to talk to the CustomerAlerts API


6
7
8
9
10
11
12
13
14
15
# File 'lib/cta_redux/customer_alerts.rb', line 6

def self.connection
  @connection ||= Faraday.new do |faraday|
    faraday.url_prefix = 'http://www.transitchicago.com/api/1.0/'
    faraday.use CTA::CustomerAlerts::Parser, !!@debug
    if @cache_responses
      faraday.response :caching, (@cache || SimpleCache.new(Hash.new))
    end
    faraday.adapter Faraday.default_adapter
  end
end

.debugObject

Returns the debug status of the API. When in debug mode, all API responses will additionally return the parsed XML tree, and the original XML for inspection


88
89
90
# File 'lib/cta_redux/customer_alerts.rb', line 88

def self.debug
  !!@debug
end

.debug=(debug) ⇒ Object

Sets the debug status of the API. When in debug mode, all API responses will additionally return the parsed XML tree, and the original XML for inspection


95
96
97
98
# File 'lib/cta_redux/customer_alerts.rb', line 95

def self.debug=(debug)
  @debug = debug
  @connection = nil
end

.status!(options = {}) ⇒ CTA::CustomerAlerts::RouteStatusResponse

Returns an overview of system status.

Examples:

CTA::CustomerAlerts.status!(:routes => [8,22])

Options Hash (options):

  • :routes (Array<Integer> Array<String>, Integer, String)

    Routes to query for status

  • :stations (String, Integer)

    Station to query for status


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cta_redux/customer_alerts.rb', line 24

def self.status!(options = {})
  allowed_keys = [:routes, :station]
  if options.keys.any? { |k| !allowed_keys.include?(k) }
    raise "Illegal argument!"
  end

  routes   = Array.wrap(options[:routes]).flatten.compact.uniq.join(',')
  stations = Array.wrap(options[:station]).flatten.compact.uniq

  if stations.size > 1
    raise "Can only specify one station!"
  end

  connection.get('routes.aspx', { :type => options[:type], :routeid => routes, :stationid => stations.first })
end