Class: SFBATransitAPI::Connection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Connection.



10
11
12
13
14
15
# File 'lib/sfba_transit_api/connection.rb', line 10

def initialize(token, options={})
  self.token = token
  self.path_prefix = options.fetch(:path_prefix, PATH_PREFIX)

  @connection = Faraday.new(:url => options.fetch(:api_endpoint, API_ENDPOINT))
end

Instance Attribute Details

#path_prefixObject

Returns the value of attribute path_prefix.



8
9
10
# File 'lib/sfba_transit_api/connection.rb', line 8

def path_prefix
  @path_prefix
end

#tokenObject

Returns the value of attribute token.



8
9
10
# File 'lib/sfba_transit_api/connection.rb', line 8

def token
  @token
end

Instance Method Details

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



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/sfba_transit_api/connection.rb', line 17

def get(method, options={})
  path = "#{path_prefix}/#{method.to_s.camelize}.aspx"
  params = options_to_params(options)
  params["token"] = token

  response = request(path, params)

  if response.status != 200
    raise ResponseException, "Server responded with #{response.status}"
  end

  xml_doc = Nokogiri::XML(response.body)

  error_node = xml_doc.at_xpath("/transitServiceError")

  if error_node
    raise ResponseException, error_node.text
  end

  xml_doc
end

#options_to_params(options) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/sfba_transit_api/connection.rb', line 43

def options_to_params(options)
  options.inject({}) do |memo, pair|
    key = pair[0].to_s.camelize(:lower).sub(/idf/i, "IDF")
    value = pair[1]
    memo[key] = value
    memo
  end
end

#request(path, params) ⇒ Object



39
40
41
# File 'lib/sfba_transit_api/connection.rb', line 39

def request(path, params)
  @connection.get path, params
end