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
-
#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
Constructor Details
#initialize(email = ENV["RTX_USER_EMAIL"], password = ENV["RTX_USER_PASSWORD"]) ⇒ Client
Returns a new instance of Client.
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
#method_missing(method, *args, &block) ⇒ Object
44
45
46
47
48
49
50
51
52
|
# File 'lib/rtx/api/client.rb', line 44
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
#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
|
#email ⇒ Object
Returns the value of attribute email.
5
6
7
|
# File 'lib/rtx/api/client.rb', line 5
def email
@email
end
|
#expires ⇒ Object
Returns the value of attribute expires.
5
6
7
|
# File 'lib/rtx/api/client.rb', line 5
def expires
@expires
end
|
#password ⇒ Object
Returns the value of attribute password.
5
6
7
|
# File 'lib/rtx/api/client.rb', line 5
def password
@password
end
|
#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
|
#token ⇒ Object
Returns the value of attribute token.
5
6
7
|
# File 'lib/rtx/api/client.rb', line 5
def token
@token
end
|
Instance Method Details
#authenticate ⇒ Object
18
19
20
21
22
23
24
25
26
27
28
|
# 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
@token = response[:token]
@expires = response[:expires_at]
@account_id = response[:account_id]
@profile_id = response[:profile_id]
end
|
#authenticated? ⇒ Boolean
14
15
16
|
# File 'lib/rtx/api/client.rb', line 14
def authenticated?
!token.nil? && !account_id.nil? && !expired?
end
|
#collection(resource_name, attrs = {}) ⇒ Object
54
55
56
57
|
# File 'lib/rtx/api/client.rb', line 54
def collection(resource_name, attrs = {})
request = self.class.get("#{rtx_api_url}/#{resource_path(resource_name)}", options(:get, attrs))
handle_request(request)
end
|
#delete(resource_name, resource_id) ⇒ Object
82
83
84
85
86
|
# File 'lib/rtx/api/client.rb', line 82
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
|
#detail(resource_name, resource_id, attrs = {}) ⇒ Object
59
60
61
62
63
|
# File 'lib/rtx/api/client.rb', line 59
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
|
#expired? ⇒ Boolean
30
31
32
|
# File 'lib/rtx/api/client.rb', line 30
def expired?
expires.nil? || DateTime.now > DateTime.parse(expires)
end
|
#handle_request(request) ⇒ Object
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/rtx/api/client.rb', line 88
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
|
#logout ⇒ Object
34
35
36
37
38
39
40
41
42
|
# File 'lib/rtx/api/client.rb', line 34
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
|
#patch(resource_name, resource_id, attrs = {}) ⇒ Object
76
77
78
79
80
|
# File 'lib/rtx/api/client.rb', line 76
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
|
#post(resource_name, attrs = {}) ⇒ Object
65
66
67
68
|
# File 'lib/rtx/api/client.rb', line 65
def post(resource_name, attrs = {})
request = self.class.post("#{rtx_api_url}/#{resource_path(resource_name)}", options(:post, attrs))
handle_request(request)
end
|
#put(resource_name, resource_id, attrs = {}) ⇒ Object
70
71
72
73
74
|
# File 'lib/rtx/api/client.rb', line 70
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
|
#resource_path(resource_name) ⇒ Object