Class: AccessWatch::Client

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

Constant Summary collapse

HTTPS =
"https".freeze
CERTIFICATE_AUTHORITIES_PATH =
File.expand_path("../../cacert.pem", __FILE__)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Client

Returns a new instance of Client.



10
11
12
13
14
# File 'lib/access_watch.rb', line 10

def initialize(options)
  @api_secret = options[:api_secret]
  @api_key = options[:api_key] or raise "AccessWatch api_key is mandatory."
  @api_endpoint = options[:api_endpoint] || "https://access.watch/api/1.0/"
end

Instance Attribute Details

#api_endpointObject (readonly)

Returns the value of attribute api_endpoint.



8
9
10
# File 'lib/access_watch.rb', line 8

def api_endpoint
  @api_endpoint
end

#api_keyObject (readonly)

Returns the value of attribute api_key.



8
9
10
# File 'lib/access_watch.rb', line 8

def api_key
  @api_key
end

#api_secretObject (readonly)

Returns the value of attribute api_secret.



8
9
10
# File 'lib/access_watch.rb', line 8

def api_secret
  @api_secret
end

Instance Method Details

#default_headersObject



34
35
36
37
38
39
40
# File 'lib/access_watch.rb', line 34

def default_headers
  {
    "Api-Key" => api_key,
    "Accept" => "application/json",
    "Content-Type" => "application/json",
  }
end

#post(path, data) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/access_watch.rb', line 19

def post(path, data)
  uri = URI(api_endpoint + path)
  http = Net::HTTP.new(uri.host, uri.port)

  if uri.scheme == HTTPS
    http.verify_mode = OpenSSL::SSL::VERIFY_PEER
    http.ca_file = CERTIFICATE_AUTHORITIES_PATH
    http.use_ssl = true
  end

  post = Net::HTTP::Post.new(uri.path, default_headers)
  post.body = data.to_json
  http.request(post)
end