Class: RTX::API::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/rtx/api/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#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

#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

#account_idObject

Returns the value of attribute account_id.


5
6
7
# File 'lib/rtx/api/client.rb', line 5

def 
  @account_id
end

#emailObject

Returns the value of attribute email.


5
6
7
# File 'lib/rtx/api/client.rb', line 5

def email
  @email
end

#expiresObject

Returns the value of attribute expires.


5
6
7
# File 'lib/rtx/api/client.rb', line 5

def expires
  @expires
end

#passwordObject

Returns the value of attribute password.


5
6
7
# File 'lib/rtx/api/client.rb', line 5

def password
  @password
end

#profile_idObject

Returns the value of attribute profile_id.


5
6
7
# File 'lib/rtx/api/client.rb', line 5

def profile_id
  @profile_id
end

#tokenObject

Returns the value of attribute token.


5
6
7
# File 'lib/rtx/api/client.rb', line 5

def token
  @token
end

Instance Method Details

#authenticateObject

[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: default_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

#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

#authenticated?Boolean

Returns:

  • (Boolean)
[View source]

14
15
16
# File 'lib/rtx/api/client.rb', line 14

def authenticated?
  !token.nil? && !.nil? && !expired?
end

#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

#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

#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

#expired?Boolean

Returns:

  • (Boolean)
[View source]

42
43
44
# File 'lib/rtx/api/client.rb', line 42

def expired?
  expires.nil? || DateTime.now > DateTime.parse(expires)
end

#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

#logoutObject

[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

#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

#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

#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

#resource_path(resource_name) ⇒ Object

[View source]

112
113
114
115
116
117
118
# File 'lib/rtx/api/client.rb', line 112

def resource_path(resource_name)
  path = API::Resources.allowed_resources[resource_name.to_sym]
  if !path
    raise API::Errors::InvalidResourceError.new("The resource provided (#{resource_name}) is not allowed")
  end
  path
end

#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, , profile_id)
  @token = token
  @expires = expires
  @account_id = 
  @profile_id = profile_id
end