Class: DuoUniversalRails::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/duo_universal_rails/request.rb

Direct Known Subclasses

AuthRequest, HealthCheckRequest, TokenRequest

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Request

Returns a new instance of Request.



5
6
7
# File 'lib/duo_universal_rails/request.rb', line 5

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



3
4
5
# File 'lib/duo_universal_rails/request.rb', line 3

def client
  @client
end

Instance Method Details

#default_headersObject



22
23
24
# File 'lib/duo_universal_rails/request.rb', line 22

def default_headers
  # {Authorization: "Bearer #{client.api_key}"}
end

#get_request(url, params: {}, headers: {}) ⇒ Object



9
10
11
# File 'lib/duo_universal_rails/request.rb', line 9

def get_request(url, params: {}, headers: {})
  handle_response client.connection.get(url, params, default_headers.merge(headers))
end

#handle_response(response) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/duo_universal_rails/request.rb', line 26

def handle_response(response)
  message = response.body["error"]

  case response.status 
  when 400
    raise Error, message
  when 401
    raise Error, message
  when 403
    raise Error, message
  when 404
    raise Error, message
  when 429
    raise Error, message
  when 500
    raise Error, message
  end

  response
end

#post_request(url, params: {}) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/duo_universal_rails/request.rb', line 13

def post_request(url, params: {})
  response = client.connection.post(url) do |req|
    req.params = params
  end

  handle_response response
end