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
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]
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/rtx/api/client.rb', line 40
def method_missing(method, *args, &block)
allowed_resources = API::Resources.allowed_resources
method_name = method.to_s
if allowed_resources.include? method_name
attrs = {}
if args.size > 0
attrs = args.last.is_a?(Hash) ? args.pop : {}
end
RTX::API::Collection.new(self, method_name, 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
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)
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
|
permalink
#authenticated? ⇒ Boolean
[View source]
14
15
16
|
# File 'lib/rtx/api/client.rb', line 14
def authenticated?
!token.nil? && !expires.nil? && !account_id.nil?
end
|
permalink
#delete(resource_name, resource_id) ⇒ Object
[View source]
72
73
74
75
76
77
|
# File 'lib/rtx/api/client.rb', line 72
def delete(resource_name, resource_id)
resource_exists?(resource_name)
raise API::Errors::RequestError.new("id was not provided") if resource_id.nil?
request = self.class.delete("#{rtx_api_url}/#{resource_name}/#{resource_id}", options(:delete))
handle_request(request)
end
|
permalink
#get(resource_name, attrs = {}) ⇒ Object
[View source]
53
54
55
56
57
|
# File 'lib/rtx/api/client.rb', line 53
def get(resource_name, attrs = {})
resource_exists?(resource_name)
request = self.class.get("#{rtx_api_url}/#{resource_name}", options(:get, attrs))
handle_request(request)
end
|
permalink
#handle_request(request) ⇒ Object
[View source]
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/rtx/api/client.rb', line 79
def handle_request(request)
if !request.success?
raise API::Errors::RequestError.new("#{response}")
end
if request.parsed_response.nil?
return true
end
Oj.load(request.body)
end
|
[View source]
30
31
32
33
34
35
36
37
38
|
# File 'lib/rtx/api/client.rb', line 30
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: #{response}")
end
@token, @expires, @account_id, @profile_id = nil, nil, nil, nil
end
end
|
permalink
#post(resource_name, attrs = {}) ⇒ Object
[View source]
59
60
61
62
63
|
# File 'lib/rtx/api/client.rb', line 59
def post(resource_name, attrs = {})
resource_exists?(resource_name)
request = self.class.post("#{rtx_api_url}/#{resource_name}", options(:post, attrs))
handle_request(request)
end
|
permalink
#put(resource_name, resource_id, attrs = {}) ⇒ Object
[View source]
65
66
67
68
69
70
|
# File 'lib/rtx/api/client.rb', line 65
def put(resource_name, resource_id, attrs = {})
resource_exists?(resource_name)
raise API::Errors::RequestError.new("id was not provided") if resource_id.nil?
request = self.class.put("#{rtx_api_url}/#{resource_name}/#{resource_id}", options(:put, attrs))
handle_request(request)
end
|