Class: Yoti::Request

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

Overview

Manage the API’s HTTPS requests

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRequest

Returns a new instance of Request.



19
20
21
# File 'lib/yoti/http/request.rb', line 19

def initialize
  @headers = {}
end

Instance Attribute Details

#encrypted_connect_tokenString

Returns the URL token received from Yoti Connect.

Returns:

  • (String)

    the URL token received from Yoti Connect



7
8
9
# File 'lib/yoti/http/request.rb', line 7

def encrypted_connect_token
  @encrypted_connect_token
end

#endpointString

Returns the API endpoint for the request.

Returns:

  • (String)

    the API endpoint for the request



14
15
16
# File 'lib/yoti/http/request.rb', line 14

def endpoint
  @endpoint
end

#http_methodString

The allowed methods are: GET, DELETE, POST, PUT, PATCH

Returns:

  • (String)

    the HTTP method used for the request



11
12
13
# File 'lib/yoti/http/request.rb', line 11

def http_method
  @http_method
end

#payloadHash

Returns the body sent with the request.

Returns:

  • (Hash)

    the body sent with the request



17
18
19
# File 'lib/yoti/http/request.rb', line 17

def payload
  @payload
end

Instance Method Details

#add_header(header, value) ⇒ Object

Adds a HTTP header to the request



24
25
26
# File 'lib/yoti/http/request.rb', line 24

def add_header(header, value)
  @headers[header] = value
end

#bodyHash

Makes a HTTP request after signing the headers

Returns:

  • (Hash)

    the body from the HTTP request

Raises:



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/yoti/http/request.rb', line 30

def body
  raise RequestError, 'The request requires a HTTP method.' unless @http_method
  raise RequestError, 'The payload needs to be a hash.' unless @payload.to_s.empty? || @payload.is_a?(Hash)

  res = Net::HTTP.start(uri.hostname, Yoti.configuration.api_port, use_ssl: https_uri?) do |http|
    signed_request = SignedRequest.new(unsigned_request, path, @payload).sign
    http.request(signed_request)
  end

  raise RequestError, "Unsuccessful Yoti API call: #{res.message}" unless res.code == '200'

  res.body
end