Class: SecureNative::SecureNativeHttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/securenative/http/securenative_http_client.rb

Constant Summary collapse

AUTHORIZATION_HEADER =
'Authorization'
VERSION_HEADER =
'SN-Version'
USER_AGENT_HEADER =
'User-Agent'
USER_AGENT_HEADER_VALUE =
'SecureNative-ruby'
CONTENT_TYPE_HEADER =
'Content-Type'
CONTENT_TYPE_HEADER_VALUE =
'application/json'

Instance Method Summary collapse

Constructor Details

#initialize(securenative_options) ⇒ SecureNativeHttpClient

Returns a new instance of SecureNativeHttpClient.



18
19
20
# File 'lib/securenative/http/securenative_http_client.rb', line 18

def initialize(securenative_options)
  @options = securenative_options
end

Instance Method Details

#_headersObject



22
23
24
25
26
27
28
29
# File 'lib/securenative/http/securenative_http_client.rb', line 22

def _headers
  {
      CONTENT_TYPE_HEADER => CONTENT_TYPE_HEADER_VALUE,
      USER_AGENT_HEADER => USER_AGENT_HEADER_VALUE,
      VERSION_HEADER => VersionUtils.version,
      AUTHORIZATION_HEADER => @options.api_key
  }
end

#post(path, body) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/securenative/http/securenative_http_client.rb', line 31

def post(path, body)
  uri = URI.parse("#{@options.api_url}/#{path}")
  headers = _headers

  client = Net::HTTP.new(uri.host, uri.port)
  client.use_ssl = true
  client.verify_mode = OpenSSL::SSL::VERIFY_NONE

  request = Net::HTTP::Post.new(uri.request_uri, headers)
  request.body = body

  res = nil
  begin
    res = client.request(request)
  rescue StandardError => e
    SecureNativeLogger.error("Failed to send request; #{e}")
    return res
  end
  res
end