class Algometrics::Client

Constants

DEFAULT_CONFIG

Attributes

adapter[R]
api_key[R]
api_version[R]
url[R]

Public Class Methods

logger() click to toggle source
# File lib/algometrics/client.rb, line 80
def self.logger
  @logger ||= Logger.new(STDOUT)
end
logger=(new_logger) click to toggle source
# File lib/algometrics/client.rb, line 84
def self.logger=(new_logger)
  @logger = new_logger
end
new(opts = {}) click to toggle source
# 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

Public Instance Methods

connection() click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# File lib/algometrics/client.rb, line 52
def user_agent
  "algometrics-gem #{Algometrics::VERSION}"
end

Private Instance Methods

parse_actor(actor) click to toggle source
# File lib/algometrics/client.rb, line 90
def parse_actor(actor)
  case actor
  when String
    type, id = actor.split("#")
    { type: type, id: id }
  when Hash
    actor
  end
end
valid_actor?(actor) click to toggle source
# File lib/algometrics/client.rb, line 109
def valid_actor?(actor)
  case actor
  when String
    validate_actor_string(actor)
  when Hash
    validate_actor_hash(actor)
  else
    false
  end
end
valid_event_name?(str) click to toggle source
# File lib/algometrics/client.rb, line 120
def valid_event_name?(str)
  !(str =~ /\A[\w\- ]+\z/).nil?
end
validate_actor_hash(hash) click to toggle source
# File lib/algometrics/client.rb, line 104
def validate_actor_hash(hash)
  return false unless ([:type, :id] & hash.keys).count > 0
  validate_actor_string("#{hash[:type]}##{hash[:id]}")
end
validate_actor_string(str) click to toggle source
# File lib/algometrics/client.rb, line 100
def validate_actor_string(str)
  !(str =~ /\A[\w\- ]+#[\w\- ]+\z/).nil?
end