Class: Algometrics::Client
- Inherits:
-
Object
- Object
- Algometrics::Client
- Extended by:
- Forwardable
- Defined in:
- lib/algometrics/client.rb
Constant Summary collapse
- DEFAULT_CONFIG =
{ url: "https://algometrics.io", api_version: "/v1" }
Instance Attribute Summary collapse
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#api_version ⇒ Object
readonly
Returns the value of attribute api_version.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Class Method Summary collapse
Instance Method Summary collapse
- #connection ⇒ Object
-
#initialize(opts = {}) ⇒ Client
constructor
A new instance of Client.
- #track(event:, actor:, status: Algometrics::SUCCESS) ⇒ Object
- #user_agent ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Client
Returns a new instance of Client.
26 27 28 29 30 31 32 33 |
# File 'lib/algometrics/client.rb', line 26 def initialize(opts = {}) @api_key = opts[:api_key] @api_version = opts[:api_version] || DEFAULT_CONFIG[:api_version] @url = URI.join((opts[:url] || DEFAULT_CONFIG[:url]), @api_version).to_s @adapter = opts[:adapter] || Faraday.default_adapter end |
Instance Attribute Details
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
21 22 23 |
# File 'lib/algometrics/client.rb', line 21 def adapter @adapter end |
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
21 22 23 |
# File 'lib/algometrics/client.rb', line 21 def api_key @api_key end |
#api_version ⇒ Object (readonly)
Returns the value of attribute api_version.
21 22 23 |
# File 'lib/algometrics/client.rb', line 21 def api_version @api_version end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
21 22 23 |
# File 'lib/algometrics/client.rb', line 21 def url @url end |
Class Method Details
.logger ⇒ Object
80 81 82 |
# File 'lib/algometrics/client.rb', line 80 def self.logger @logger ||= Logger.new(STDOUT) end |
.logger=(new_logger) ⇒ Object
84 85 86 |
# File 'lib/algometrics/client.rb', line 84 def self.logger=(new_logger) @logger = new_logger end |
Instance Method Details
#connection ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/algometrics/client.rb', line 35 def connection @connection ||= Faraday::Connection.new( url: url, request: { open_timeout: 30, timeout: 30 } ) do |f| f.adapter adapter f.use Algometrics::Middleware::ResponseHandler end.tap do |transport| transport.headers[:user_agent] = user_agent transport.headers[:content_type] = 'application/json' transport.headers[:x_api_key] = api_key end end |
#track(event:, actor:, status: Algometrics::SUCCESS) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/algometrics/client.rb', line 56 def track(event:, actor:, status: Algometrics::SUCCESS) unless valid_actor?(actor) Algometrics::Client.logger.error("Algometrics client error: invalid actor: '#{actor}' " \ "actor type and id must be of the following format: /\\A[\\w\\- ]+\\z/") return end unless valid_event_name?(event) Algometrics::Client.logger.error("Algometrics client error: invalid event name: '#{event}' " \ "event name must be of the following format: /\\A[\\w\\- ]+\\z/") return end actor = parse_actor(actor) data = { event: event, actor: actor, status: [Algometrics::SUCCESS, Algometrics::FAILURE].include?(status) ? status : Algometrics::SUCCESS } connection.post("#{api_version}/events", data.to_json) end |
#user_agent ⇒ Object
52 53 54 |
# File 'lib/algometrics/client.rb', line 52 def user_agent "algometrics-gem #{Algometrics::VERSION}" end |