Class: SFBATransitAPI::Connection
- Inherits:
-
Object
- Object
- SFBATransitAPI::Connection
- Defined in:
- lib/sfba_transit_api/connection.rb
Instance Attribute Summary collapse
-
#path_prefix ⇒ Object
Returns the value of attribute path_prefix.
-
#token ⇒ Object
Returns the value of attribute token.
Instance Method Summary collapse
- #get(method, options = {}) ⇒ Object
-
#initialize(token, options = {}) ⇒ Connection
constructor
A new instance of Connection.
- #options_to_params(options) ⇒ Object
- #request(path, params) ⇒ Object
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, ={}) self.token = token self.path_prefix = .fetch(:path_prefix, PATH_PREFIX) @connection = Faraday.new(:url => .fetch(:api_endpoint, API_ENDPOINT)) end |
Instance Attribute Details
#path_prefix ⇒ Object
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 |
#token ⇒ Object
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, ={}) path = "#{path_prefix}/#{method.to_s.camelize}.aspx" params = () 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 () .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 |