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
-
#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
-
#handle_request(request) ⇒ Object
-
#initialize(email = ENV["RTX_USER_EMAIL"], password = ENV["RTX_USER_PASSWORD"]) ⇒ Client
constructor
A new instance of Client.
-
#logout ⇒ Object
-
#method_missing(method, *args, &block) ⇒ Object
-
#patch(resource_name, resource_id, attrs = {}) ⇒ Object
-
#post(resource_name, attrs = {}) ⇒ Object
-
#put(resource_name, resource_id, attrs = {}) ⇒ Object
-
#resource_path(resource_name) ⇒ Object
-
#set_attributes(token, expires, account_id, profile_id) ⇒ Object
Constructor Details
permalink
#initialize(email = ENV["RTX_USER_EMAIL"], password = ENV["RTX_USER_PASSWORD"]) ⇒ Client
Returns a new instance of Client.
[View source]
7
8
9
10
11
12
|
# File 'lib/rtx/api/client.rb', line 7
def initialize(email = ENV["RTX_USER_EMAIL"], password = ENV["RTX_USER_PASSWORD"])
@email = email
@password = password
@token = nil
@expires = nil
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
permalink
#method_missing(method, *args, &block) ⇒ Object
[View source]
56
57
58
59
60
61
62
63
64
|
# File 'lib/rtx/api/client.rb', line 56
def method_missing(method, *args, &block)
if resource_path(method)
attrs = {}
if args.size > 0
attrs = args.last.is_a?(Hash) ? args.pop : {}
end
RTX::API::Collection.new(self, method, attrs)
end
end
|
Instance Attribute Details
permalink
#account_id ⇒ Object
Returns the value of attribute account_id.
5
6
7
|
# File 'lib/rtx/api/client.rb', line 5
def account_id
@account_id
end
|
Returns the value of attribute email.
5
6
7
|
# File 'lib/rtx/api/client.rb', line 5
def email
@email
end
|
Returns the value of attribute expires.
5
6
7
|
# File 'lib/rtx/api/client.rb', line 5
def expires
@expires
end
|
permalink
#password ⇒ Object
Returns the value of attribute password.
5
6
7
|
# File 'lib/rtx/api/client.rb', line 5
def password
@password
end
|
permalink
#profile_id ⇒ Object
Returns the value of attribute profile_id.
5
6
7
|
# File 'lib/rtx/api/client.rb', line 5
def profile_id
@profile_id
end
|
Returns the value of attribute token.
5
6
7
|
# File 'lib/rtx/api/client.rb', line 5
def token
@token
end
|
Instance Method Details
permalink
#authenticate ⇒ Object
[View source]
18
19
20
21
22
23
24
25
|
# File 'lib/rtx/api/client.rb', line 18
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.new("Authentication Login Error: #{response}")
end
set_attributes(response[:token], response[:expires_at], response[:account_id], response[:profile_id])
end
|
permalink
#authenticate_as(email, &block) ⇒ Object
[View source]
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/rtx/api/client.rb', line 27
def authenticate_as(email, &block)
authenticate if !authenticated?
response = collection(:users, email: email)
if response[:_total] == 1
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)
else
raise API::Errors::ClientError.new("User not found with associated email address: #{email}")
end
true
end
|
permalink
#authenticated? ⇒ Boolean
[View source]
14
15
16
|
# File 'lib/rtx/api/client.rb', line 14
def authenticated?
!token.nil? && !account_id.nil? && !expired?
end
|
permalink
#collection(resource_name, attrs = {}) ⇒ Object
[View source]
66
67
68
69
|
# File 'lib/rtx/api/client.rb', line 66
def collection(resource_name, attrs = {})
request = self.class.get("#{rtx_api_url}/#{resource_path(resource_name)}", options(:get, attrs))
handle_request(request)
end
|
permalink
#delete(resource_name, resource_id) ⇒ Object
[View source]
94
95
96
97
98
|
# File 'lib/rtx/api/client.rb', line 94
def delete(resource_name, resource_id)
raise API::Errors::RequestError.new("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
|
permalink
#detail(resource_name, resource_id, attrs = {}) ⇒ Object
[View source]
71
72
73
74
75
|
# File 'lib/rtx/api/client.rb', line 71
def detail(resource_name, resource_id, attrs = {})
raise API::Errors::RequestError.new("id was not provided") if resource_id.nil?
request = self.class.get("#{rtx_api_url}/#{resource_path(resource_name)}/#{resource_id}", options(:get, attrs))
handle_request(request)
end
|
permalink
#expired? ⇒ Boolean
[View source]
42
43
44
|
# File 'lib/rtx/api/client.rb', line 42
def expired?
expires.nil? || DateTime.now > DateTime.parse(expires)
end
|
permalink
#handle_request(request) ⇒ Object
[View source]
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/rtx/api/client.rb', line 100
def handle_request(request)
if !request.success?
raise API::Errors::RequestError.new("#{request.parsed_response}")
end
if request.parsed_response.nil?
return true
end
Oj.load(request.body, symbol_keys: true)
end
|
[View source]
46
47
48
49
50
51
52
53
54
|
# File 'lib/rtx/api/client.rb', line 46
def logout
if token
request = self.class.delete("#{rtx_api_url}/auth", options(:delete))
if request.code != 204
raise API::Errors::AuthenticationError.new("Authentication Logout Error: #{request}")
end
@token, @expires, @account_id, @profile_id = nil, nil, nil, nil
end
end
|
permalink
#patch(resource_name, resource_id, attrs = {}) ⇒ Object
[View source]
88
89
90
91
92
|
# File 'lib/rtx/api/client.rb', line 88
def patch(resource_name, resource_id, attrs = {})
raise API::Errors::RequestError.new("id was not provided") if resource_id.nil?
request = self.class.patch("#{rtx_api_url}/#{resource_path(resource_name)}/#{resource_id}", options(:patch, attrs))
handle_request(request)
end
|
permalink
#post(resource_name, attrs = {}) ⇒ Object
[View source]
77
78
79
80
|
# File 'lib/rtx/api/client.rb', line 77
def post(resource_name, attrs = {})
request = self.class.post("#{rtx_api_url}/#{resource_path(resource_name)}", options(:post, attrs))
handle_request(request)
end
|
permalink
#put(resource_name, resource_id, attrs = {}) ⇒ Object
[View source]
82
83
84
85
86
|
# File 'lib/rtx/api/client.rb', line 82
def put(resource_name, resource_id, attrs = {})
raise API::Errors::RequestError.new("id was not provided") if resource_id.nil?
request = self.class.put("#{rtx_api_url}/#{resource_path(resource_name)}/#{resource_id}", options(:put, attrs))
handle_request(request)
end
|
permalink
#set_attributes(token, expires, account_id, profile_id) ⇒ Object
[View source]
120
121
122
123
124
125
|
# File 'lib/rtx/api/client.rb', line 120
def set_attributes(token, expires, account_id, profile_id)
@token = token
@expires = expires
@account_id = account_id
@profile_id = profile_id
end
|