Class: AccessWatch::Client
- Inherits:
-
Object
- Object
- AccessWatch::Client
- Defined in:
- lib/access_watch/client.rb
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.
-
#api_timeout ⇒ Object
readonly
Returns the value of attribute api_timeout.
Instance Method Summary collapse
- #default_headers ⇒ Object
- #get(path, params = nil) ⇒ Object
-
#initialize(options) ⇒ Client
constructor
A new instance of Client.
- #post(path, data) ⇒ Object
- #user_agent ⇒ Object
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() @api_secret = [:api_secret] @api_timeout = [:api_timeout] || 1 @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/client.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/client.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/client.rb', line 8 def api_secret @api_secret end |
#api_timeout ⇒ Object (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_headers ⇒ Object
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_agent ⇒ Object
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 |