Class: RTX::API::Client
- Inherits:
-
Object
show all
- Includes:
- HTTParty
- Defined in:
- lib/rtx/api/client.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#allowed_resource?(method) ⇒ Boolean
rubocop:enable Style/MissingRespondToMissing rubocop:enable Style/MethodMissingSuper rubocop:enable Lint/RedundantCopDisableDirective.
-
#authenticate ⇒ Object
-
#authenticate_as(email, &block) ⇒ Object
-
#authenticated? ⇒ Boolean
-
#collection(resource_name, attrs = {}) ⇒ Object
-
#delete(resource_name, resource_id) ⇒ Object
-
#detail(resource_name, resource_id, attrs = {}) ⇒ Object
-
#expired? ⇒ Boolean
-
#get(resource_name, attrs = {}, file_type = 'json') ⇒ Object
-
#handle_request(request, file_type = 'json') ⇒ Object
-
#initialize(email = ENV['RTX_USER_EMAIL'], password = ENV['RTX_USER_PASSWORD'], api_url = ENV['RTX_API_URL']) ⇒ Client
constructor
A new instance of Client.
-
#logout ⇒ Object
-
#method_missing(method, *args) ⇒ Object
rubocop:disable Lint/RedundantCopDisableDirective rubocop:disable Style/MissingRespondToMissing rubocop:disable Style/MethodMissingSuper.
-
#patch(resource_name, resource_id, attrs = {}) ⇒ Object
-
#post(resource_name, attrs = {}) ⇒ Object
-
#put(resource_name, resource_id, attrs = {}) ⇒ Object
-
#resource_path(resource_name, attrs = {}) ⇒ Object
-
#set_attributes(token, expires, account_id, profile_id) ⇒ Object
Constructor Details
#initialize(email = ENV['RTX_USER_EMAIL'], password = ENV['RTX_USER_PASSWORD'], api_url = ENV['RTX_API_URL']) ⇒ Client
Returns a new instance of Client.
11
12
13
14
15
16
17
18
|
# File 'lib/rtx/api/client.rb', line 11
def initialize(email = ENV['RTX_USER_EMAIL'], password = ENV['RTX_USER_PASSWORD'],
api_url = ENV['RTX_API_URL'])
@email = email
@password = password
@api_url = api_url
@token = nil
@expires = nil
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
rubocop:disable Lint/RedundantCopDisableDirective rubocop:disable Style/MissingRespondToMissing rubocop:disable Style/MethodMissingSuper
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/rtx/api/client.rb', line 79
def method_missing(method, *args)
return unless allowed_resource?(method)
attrs = {}
if args.size.positive?
attrs = args.last.is_a?(Hash) ? args.pop : {}
end
return RTX::API::CollectionV2.new(self, method, attrs) if method.to_s.start_with?('v2_')
RTX::API::Collection.new(self, method, attrs)
end
|
Instance Attribute Details
#account_id ⇒ Object
Returns the value of attribute account_id.
9
10
11
|
# File 'lib/rtx/api/client.rb', line 9
def account_id
@account_id
end
|
#api_url ⇒ Object
Returns the value of attribute api_url.
9
10
11
|
# File 'lib/rtx/api/client.rb', line 9
def api_url
@api_url
end
|
#email ⇒ Object
Returns the value of attribute email.
9
10
11
|
# File 'lib/rtx/api/client.rb', line 9
def email
@email
end
|
#expires ⇒ Object
Returns the value of attribute expires.
9
10
11
|
# File 'lib/rtx/api/client.rb', line 9
def expires
@expires
end
|
#password ⇒ Object
Returns the value of attribute password.
9
10
11
|
# File 'lib/rtx/api/client.rb', line 9
def password
@password
end
|
#profile_id ⇒ Object
Returns the value of attribute profile_id.
9
10
11
|
# File 'lib/rtx/api/client.rb', line 9
def profile_id
@profile_id
end
|
#token ⇒ Object
Returns the value of attribute token.
9
10
11
|
# File 'lib/rtx/api/client.rb', line 9
def token
@token
end
|
Instance Method Details
#allowed_resource?(method) ⇒ Boolean
rubocop:enable Style/MissingRespondToMissing rubocop:enable Style/MethodMissingSuper rubocop:enable Lint/RedundantCopDisableDirective
#authenticate ⇒ Object
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/rtx/api/client.rb', line 24
def authenticate
request = self.class.post("#{rtx_api_url}/auth", headers: ,
basic_auth: {
username: email,
password: password
})
response = Oj.load(request.body, symbol_keys: true)
if request.code != 201
raise API::Errors::AuthenticationError, "Authentication Login Error: #{response}"
end
set_attributes(response[:token], response[:expires_at], response[:account_id],
response[:profile_id])
end
|
#authenticate_as(email, &block) ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/rtx/api/client.rb', line 39
def authenticate_as(email, &block)
authenticate if !authenticated?
response = collection(:users, email: email)
unless response[:_total] == 1
raise API::Errors::ClientError,
"User not found with associated email address: #{email}"
end
user = response[:_embedded][:users][0]
auth_response = post(:auth_tokens, account_id: user[:account_id], user_id: user[:id])
client = self.class.new(auth_response[:email], auth_response[:token])
client.set_attributes(auth_response[:token], auth_response[:expires_at],
auth_response[:account_id], auth_response[:profile_id])
block.call(client)
true
end
|
#authenticated? ⇒ Boolean
20
21
22
|
# File 'lib/rtx/api/client.rb', line 20
def authenticated?
!token.nil? && !account_id.nil? && !expired?
end
|
#collection(resource_name, attrs = {}) ⇒ Object
106
107
108
109
110
|
# File 'lib/rtx/api/client.rb', line 106
def collection(resource_name, attrs = {})
request = self.class.get("#{rtx_api_url}/#{resource_path(resource_name, attrs)}",
options(:get, attrs))
handle_request(request)
end
|
#delete(resource_name, resource_id) ⇒ Object
148
149
150
151
152
153
154
155
|
# File 'lib/rtx/api/client.rb', line 148
def delete(resource_name, resource_id)
raise API::Errors::RequestError, 'id was not provided' if resource_id.nil?
request = self.class.delete(
"#{rtx_api_url}/#{resource_path(resource_name)}/#{resource_id}", options(:delete)
)
handle_request(request)
end
|
#detail(resource_name, resource_id, attrs = {}) ⇒ Object
112
113
114
115
116
117
118
119
120
|
# File 'lib/rtx/api/client.rb', line 112
def detail(resource_name, resource_id, attrs = {})
raise API::Errors::RequestError, 'id was not provided' if resource_id.nil?
request = self.class.get(
"#{rtx_api_url}/#{resource_path(resource_name, attrs)}/#{resource_id}",
options(:get, attrs)
)
handle_request(request)
end
|
#expired? ⇒ Boolean
58
59
60
|
# File 'lib/rtx/api/client.rb', line 58
def expired?
expires.nil? || DateTime.now > DateTime.parse(expires)
end
|
#get(resource_name, attrs = {}, file_type = 'json') ⇒ Object
157
158
159
160
161
162
|
# File 'lib/rtx/api/client.rb', line 157
def get(resource_name, attrs = {}, file_type = 'json')
request = self.class.get("#{rtx_api_url}/#{resource_path(resource_name, attrs)}",
options(:get, attrs))
handle_request(request, file_type)
end
|
#handle_request(request, file_type = 'json') ⇒ Object
164
165
166
167
168
169
170
171
172
173
174
175
|
# File 'lib/rtx/api/client.rb', line 164
def handle_request(request, file_type = 'json')
raise API::Errors::RequestError, request.parsed_response.to_s if !request.success?
return true if request.parsed_response.nil?
case file_type
when 'json'
Oj.load(request.body, symbol_keys: true)
when 'csv'
CSV.parse(request.body)
end
end
|
#logout ⇒ Object
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/rtx/api/client.rb', line 62
def logout
return unless token
request = self.class.delete("#{rtx_api_url}/auth", options(:delete))
if request.code != 204
raise API::Errors::AuthenticationError, "Authentication Logout Error: #{request}"
end
@token = nil
@expires = nil
@account_id = nil
@profile_id = nil
end
|
#patch(resource_name, resource_id, attrs = {}) ⇒ Object
138
139
140
141
142
143
144
145
146
|
# File 'lib/rtx/api/client.rb', line 138
def patch(resource_name, resource_id, attrs = {})
raise API::Errors::RequestError, 'id was not provided' if resource_id.nil?
request = self.class.patch(
"#{rtx_api_url}/#{resource_path(resource_name, attrs)}/#{resource_id}",
options(:patch, attrs)
)
handle_request(request)
end
|
#post(resource_name, attrs = {}) ⇒ Object
122
123
124
125
126
|
# File 'lib/rtx/api/client.rb', line 122
def post(resource_name, attrs = {})
request = self.class.post("#{rtx_api_url}/#{resource_path(resource_name, attrs)}",
options(:post, attrs))
handle_request(request)
end
|
#put(resource_name, resource_id, attrs = {}) ⇒ Object
128
129
130
131
132
133
134
135
136
|
# File 'lib/rtx/api/client.rb', line 128
def put(resource_name, resource_id, attrs = {})
raise API::Errors::RequestError, 'id was not provided' if resource_id.nil?
request = self.class.put(
"#{rtx_api_url}/#{resource_path(resource_name, attrs)}/#{resource_id}",
options(:put, attrs)
)
handle_request(request)
end
|
#resource_path(resource_name, attrs = {}) ⇒ Object
#set_attributes(token, expires, account_id, profile_id) ⇒ Object
189
190
191
192
193
194
|
# File 'lib/rtx/api/client.rb', line 189
def set_attributes(token, expires, account_id, profile_id)
@token = token
@expires = expires
@account_id = account_id
@profile_id = profile_id
end
|