Class: AccessWatch::Client
- Inherits:
-
Object
- Object
- AccessWatch::Client
- Defined in:
- lib/access_watch.rb
Constant Summary collapse
- HTTPS =
"https".freeze
- CERTIFICATE_AUTHORITIES_PATH =
File.("../../cacert.pem", __FILE__)
Instance Attribute Summary collapse
-
#api_endpoint ⇒ Object
readonly
Returns the value of attribute api_endpoint.
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#api_secret ⇒ Object
readonly
Returns the value of attribute api_secret.
Instance Method Summary collapse
- #default_headers ⇒ Object
-
#initialize(options) ⇒ Client
constructor
A new instance of Client.
- #post(path, data) ⇒ Object
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() @api_secret = [:api_secret] @api_key = [:api_key] or raise "AccessWatch api_key is mandatory." @api_endpoint = [:api_endpoint] || "https://access.watch/api/1.0/" end |
Instance Attribute Details
#api_endpoint ⇒ Object (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_key ⇒ Object (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_secret ⇒ Object (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_headers ⇒ Object
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 |