Class: NJTransit::Client
- Inherits:
-
Object
- Object
- NJTransit::Client
- Defined in:
- lib/njtransit/client.rb
Constant Summary collapse
- DEFAULT_AUTH_PATH =
"/api/BUSDV2/authenticateUser"
Instance Attribute Summary collapse
-
#auth_path ⇒ Object
readonly
Returns the value of attribute auth_path.
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#log_level ⇒ Object
readonly
Returns the value of attribute log_level.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Class Method Summary collapse
Instance Method Summary collapse
- #authenticate! ⇒ Object
- #bus ⇒ Object
- #bus_gtfs ⇒ Object
- #bus_gtfs_g2 ⇒ Object
- #clear_token! ⇒ Object
- #delete(path, params = {}) ⇒ Object
- #get(path, params = {}) ⇒ Object
-
#initialize(username:, password:, log_level: "silent", base_url: Configuration::DEFAULT_BASE_URL, timeout: Configuration::DEFAULT_TIMEOUT, auth_path: DEFAULT_AUTH_PATH) ⇒ Client
constructor
A new instance of Client.
- #patch(path, body = {}) ⇒ Object
- #post(path, body = {}) ⇒ Object
- #post_form(path, params = {}) ⇒ Object
- #post_form_raw(path, params = {}) ⇒ Object
- #put(path, body = {}) ⇒ Object
- #rail ⇒ Object
- #rail_gtfs ⇒ Object
- #token ⇒ Object
Constructor Details
#initialize(username:, password:, log_level: "silent", base_url: Configuration::DEFAULT_BASE_URL, timeout: Configuration::DEFAULT_TIMEOUT, auth_path: DEFAULT_AUTH_PATH) ⇒ Client
Returns a new instance of Client.
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/njtransit/client.rb', line 20 def initialize(username:, password:, log_level: "silent", base_url: Configuration::DEFAULT_BASE_URL, timeout: Configuration::DEFAULT_TIMEOUT, auth_path: DEFAULT_AUTH_PATH) @username = username @password = password @log_level = log_level @base_url = base_url @timeout = timeout @auth_path = auth_path @token = nil end |
Instance Attribute Details
#auth_path ⇒ Object (readonly)
Returns the value of attribute auth_path.
18 19 20 |
# File 'lib/njtransit/client.rb', line 18 def auth_path @auth_path end |
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
18 19 20 |
# File 'lib/njtransit/client.rb', line 18 def base_url @base_url end |
#log_level ⇒ Object (readonly)
Returns the value of attribute log_level.
18 19 20 |
# File 'lib/njtransit/client.rb', line 18 def log_level @log_level end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
18 19 20 |
# File 'lib/njtransit/client.rb', line 18 def password @password end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
18 19 20 |
# File 'lib/njtransit/client.rb', line 18 def timeout @timeout end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
18 19 20 |
# File 'lib/njtransit/client.rb', line 18 def username @username end |
Class Method Details
.clear_token_cache! ⇒ Object
116 117 118 |
# File 'lib/njtransit/client.rb', line 116 def self.clear_token_cache! @token_cache = {} end |
.token_cache ⇒ Object
112 113 114 |
# File 'lib/njtransit/client.rb', line 112 def self.token_cache @token_cache ||= {} end |
Instance Method Details
#authenticate! ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/njtransit/client.rb', line 79 def authenticate! cached = self.class.token_cache[base_url] if cached @token = cached return @token end response = form_connection.post(auth_path) do |req| req.body = { username: username, password: password } end result = parse_body(response.body) unless result.is_a?(Hash) && result["Authenticated"] == "True" = result.is_a?(Hash) && result["errorMessage"] ? result["errorMessage"] : "Authentication failed" raise AuthenticationError, end @token = result["UserToken"] self.class.token_cache[base_url] = @token @token end |
#bus ⇒ Object
31 32 33 |
# File 'lib/njtransit/client.rb', line 31 def bus @bus ||= Resources::Bus.new(self) end |
#bus_gtfs ⇒ Object
39 40 41 |
# File 'lib/njtransit/client.rb', line 39 def bus_gtfs @bus_gtfs ||= Resources::BusGTFS.new(self) end |
#bus_gtfs_g2 ⇒ Object
43 44 45 |
# File 'lib/njtransit/client.rb', line 43 def bus_gtfs_g2 @bus_gtfs_g2 ||= Resources::BusGTFS.new(self, api_prefix: "/api/GTFSG2") end |
#clear_token! ⇒ Object
107 108 109 110 |
# File 'lib/njtransit/client.rb', line 107 def clear_token! @token = nil self.class.token_cache.delete(base_url) end |
#delete(path, params = {}) ⇒ Object
71 72 73 |
# File 'lib/njtransit/client.rb', line 71 def delete(path, params = {}) request(:delete, path, params) end |
#get(path, params = {}) ⇒ Object
51 52 53 |
# File 'lib/njtransit/client.rb', line 51 def get(path, params = {}) request(:get, path, params) end |
#patch(path, body = {}) ⇒ Object
67 68 69 |
# File 'lib/njtransit/client.rb', line 67 def patch(path, body = {}) request(:patch, path, body) end |
#post(path, body = {}) ⇒ Object
55 56 57 |
# File 'lib/njtransit/client.rb', line 55 def post(path, body = {}) request(:post, path, body) end |
#post_form(path, params = {}) ⇒ Object
59 60 61 |
# File 'lib/njtransit/client.rb', line 59 def post_form(path, params = {}) request_form(:post, path, params) end |
#post_form_raw(path, params = {}) ⇒ Object
75 76 77 |
# File 'lib/njtransit/client.rb', line 75 def post_form_raw(path, params = {}) request_form_raw(:post, path, params) end |
#put(path, body = {}) ⇒ Object
63 64 65 |
# File 'lib/njtransit/client.rb', line 63 def put(path, body = {}) request(:put, path, body) end |
#rail ⇒ Object
35 36 37 |
# File 'lib/njtransit/client.rb', line 35 def rail @rail ||= Resources::Rail.new(self) end |
#rail_gtfs ⇒ Object
47 48 49 |
# File 'lib/njtransit/client.rb', line 47 def rail_gtfs @rail_gtfs ||= Resources::RailGTFS.new(self) end |
#token ⇒ Object
102 103 104 105 |
# File 'lib/njtransit/client.rb', line 102 def token authenticate! if @token.nil? @token end |