Class: AccessWatch::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Client

Returns a new instance of Client.



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

def initialize(options)
  @api_secret = options[:api_secret]
  @api_timeout = options[:api_timeout] || 1
  @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/client.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/client.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/client.rb', line 8

def api_secret
  @api_secret
end

#api_timeoutObject (readonly)

Returns the value of attribute api_timeout.



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

def api_timeout
  @api_timeout
end

Instance Method Details

#default_headersObject



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

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

#get(path, params = nil) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/access_watch/client.rb', line 24

def get(path, params = nil)
  uri = URI.parse(api_endpoint + path)
  uri.query = URI.encode_www_form(params) if params
  request = Net::HTTP::Get.new(uri.request_uri, default_headers)
  response = http(uri).request(request)
  response.body
end

#post(path, data) ⇒ Object



17
18
19
20
21
22
# File 'lib/access_watch/client.rb', line 17

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

#user_agentObject



41
42
43
44
45
46
# File 'lib/access_watch/client.rb', line 41

def user_agent
  return @user_agent if @user_agent
  result = "Access Watch/#{AccessWatch::VERSION} Ruby/#{RUBY_VERSION}"
  result += " Rails/#{Rails.version}" if defined?(Rails)
  @user_agent = result
end