Class: NJTransit::Client

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

Constant Summary collapse

DEFAULT_AUTH_PATH =
"/api/BUSDV2/authenticateUser"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_pathObject (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_urlObject (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_levelObject (readonly)

Returns the value of attribute log_level.



18
19
20
# File 'lib/njtransit/client.rb', line 18

def log_level
  @log_level
end

#passwordObject (readonly)

Returns the value of attribute password.



18
19
20
# File 'lib/njtransit/client.rb', line 18

def password
  @password
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



18
19
20
# File 'lib/njtransit/client.rb', line 18

def timeout
  @timeout
end

#usernameObject (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_cacheObject



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"
    message = result.is_a?(Hash) && result["errorMessage"] ? result["errorMessage"] : "Authentication failed"
    raise AuthenticationError, message
  end

  @token = result["UserToken"]
  self.class.token_cache[base_url] = @token
  @token
end

#busObject



31
32
33
# File 'lib/njtransit/client.rb', line 31

def bus
  @bus ||= Resources::Bus.new(self)
end

#bus_gtfsObject



39
40
41
# File 'lib/njtransit/client.rb', line 39

def bus_gtfs
  @bus_gtfs ||= Resources::BusGTFS.new(self)
end

#bus_gtfs_g2Object



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

#railObject



35
36
37
# File 'lib/njtransit/client.rb', line 35

def rail
  @rail ||= Resources::Rail.new(self)
end

#rail_gtfsObject



47
48
49
# File 'lib/njtransit/client.rb', line 47

def rail_gtfs
  @rail_gtfs ||= Resources::RailGTFS.new(self)
end

#tokenObject



102
103
104
105
# File 'lib/njtransit/client.rb', line 102

def token
  authenticate! if @token.nil?
  @token
end