Class: SFBATransitAPI::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/sfba_transit_api/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, options = {}) ⇒ String

Initialize the client

Parameters:



12
13
14
# File 'lib/sfba_transit_api/client.rb', line 12

def initialize(token, options={})
  self.connection = Connection.new(token, options)
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



6
7
8
# File 'lib/sfba_transit_api/client.rb', line 6

def connection
  @connection
end

Instance Method Details

#get(method, options = {}) ⇒ Object



54
55
56
# File 'lib/sfba_transit_api/client.rb', line 54

def get(method, options={})
  self.connection.get(method, options)
end

#get_agenciesObject



16
17
18
19
20
# File 'lib/sfba_transit_api/client.rb', line 16

def get_agencies
  response = get(:get_agencies)

  parse(response)
end

#get_next_departures_by_stop_code(stopcode) ⇒ Object



42
43
44
45
46
# File 'lib/sfba_transit_api/client.rb', line 42

def get_next_departures_by_stop_code(stopcode)
  response = get(:get_next_departures_by_stop_code, stopcode: stopcode)

  parse(response)
end

#get_routes_for_agencies(agency_names) ⇒ Object



28
29
30
31
32
# File 'lib/sfba_transit_api/client.rb', line 28

def get_routes_for_agencies(agency_names)
  response = get(:get_routes_for_agencies, {agency_names: agency_names.join("|")})

  parse(response)
end

#get_routes_for_agency(agency_name) ⇒ Object



22
23
24
25
26
# File 'lib/sfba_transit_api/client.rb', line 22

def get_routes_for_agency(agency_name)
  response = get(:get_routes_for_agency, {agency_name: agency_name})

  parse(response)
end

#get_stops_for_route(route_info) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/sfba_transit_api/client.rb', line 34

def get_stops_for_route(route_info)
  route_idf = makeRouteIDF(route_info)

  response = get(:get_stops_for_route, {route_idf: route_idf})

  parse(response)
end

#makeRouteIDF(route_info) ⇒ Object



48
49
50
51
52
# File 'lib/sfba_transit_api/client.rb', line 48

def makeRouteIDF(route_info)
  route_idf = "#{route_info[:agency_name]}~#{route_info[:route_code]}"
  route_idf += "~#{route_info[:route_direction_code]}" if route_info[:route_direction_code]
  route_idf
end

#parse(doc) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/sfba_transit_api/client.rb', line 58

def parse(doc)
  agency_list_node = doc.at_xpath("//AgencyList")
  if agency_list_node
    parse_agencies(agency_list_node)
  else
    nil
  end
end

#parse_agencies(agency_list_node) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/sfba_transit_api/client.rb', line 67

def parse_agencies(agency_list_node)
  agency_list_node.xpath("./Agency").map do |agency_node|
    result = { "type" => "agency" }

    agency_node.attributes.each do |key, val|
      result[key.underscore] = to_boolean_if_possible(val.value)
    end

    route_list_node = agency_node.at_xpath("./RouteList")
    if route_list_node
      result["routes"] = parse_routes(route_list_node)
    end

    result
  end
end

#parse_departure_times(departure_time_list_node) ⇒ Object



140
141
142
143
144
# File 'lib/sfba_transit_api/client.rb', line 140

def parse_departure_times(departure_time_list_node)
  departure_time_list_node.xpath("./DepartureTime").map do |departure_time_node|
    departure_time_node.text ? departure_time_node.text.to_i : nil
  end
end

#parse_route_directions(route_direction_list_node) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/sfba_transit_api/client.rb', line 106

def parse_route_directions(route_direction_list_node)
  route_direction_list_node.xpath("./RouteDirection").map do |route_direction_node|
    result = { "type" => "route_direction" }

    route_direction_node.attributes.each do |key, val|
      result[key.underscore] = val.value
    end

    stop_list_node = route_direction_node.at_xpath("./StopList")
    if stop_list_node
      result["stops"] = parse_stops(stop_list_node)
    end

    result
  end
end

#parse_routes(route_list_node) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/sfba_transit_api/client.rb', line 84

def parse_routes(route_list_node)
  route_list_node.xpath("./Route").map do |route_node|
    result = { "type" => "route" }

    route_node.attributes.each do |key, val|
      result[key.underscore] = val.value
    end

    route_direction_list_node = route_node.at_xpath("./RouteDirectionList")
    if route_direction_list_node
      result["route_directions"] = parse_route_directions(route_direction_list_node)
    else
      stop_list_node = route_node.at_xpath("./StopList")
      if stop_list_node
        result["stops"] = parse_stops(stop_list_node)
      end
    end

    result
  end
end

#parse_stops(stop_list_node) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/sfba_transit_api/client.rb', line 123

def parse_stops(stop_list_node)
  stop_list_node.xpath("./Stop").map do |stop_node|
    result = { "type" => "stop" }

    stop_node.attributes.each do |key, val|
      result[key.underscore] = val.value
    end

    departure_time_list_node = stop_node.at_xpath("./DepartureTimeList")
    if departure_time_list_node
      result["departure_times"] = parse_departure_times(departure_time_list_node)
    end

    result
  end
end

#to_boolean_if_possible(value) ⇒ Object



146
147
148
149
150
151
152
153
154
# File 'lib/sfba_transit_api/client.rb', line 146

def to_boolean_if_possible(value)
  if value.downcase == "true"
    true
  elsif value.downcase == "false"
    false
  else
    value
  end
end