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]

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

#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
26
27
28
# 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)
  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

Returns:

  • (Boolean)
[View source]

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

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

#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

#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

#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

#logoutObject

[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

#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

#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

#resource_exists?(resource_name) ⇒ Boolean

Returns:

  • (Boolean)
[View source]

91
92
93
94
95
96
97
98
# File 'lib/rtx/api/client.rb', line 91

def resource_exists?(resource_name)
  allowed_resources = API::Resources.allowed_resources
  if !allowed_resources.include? resource_name
    raise API::Errors::InvalidResourceError.new("The resource provided (#{resource_name}) is not allowed")
  end

  true
end