class Bernard::Connection

Attributes

read_key[R]
uri[R]
write_key[R]

Public Class Methods

new(client:) click to toggle source
# File lib/bernard/connection.rb, line 3
def initialize(client:)
  unless client.uri && client.uri.kind_of?(URI)
    raise(Bernard::ArgumentError, 'could not create a connection with URI')
  end

  @uri = client.uri
  @write_key = client.write_key
  @read_key = client.read_key
end

Public Instance Methods

adapter() click to toggle source
# File lib/bernard/connection.rb, line 26
def adapter
  http = Net::HTTP.new(uri.host, uri.port)
  http.open_timeout = 5
  http.read_timeout = 5
  http.use_ssl = true
  http
end
post(payload) click to toggle source
# File lib/bernard/connection.rb, line 13
def post(payload)
  request = Net::HTTP::Post.new(uri.path)
  request['Authorization'] = write_key
  request['Content-Type'] = 'application/json'
  request.body = payload

  begin
    adapter.request(request)
  rescue Timeout::Error
    return false
  end
end