class Koko::Tracker::Client

Public Class Methods

new(attrs = {}) click to toggle source

public: Creates a new client

attrs - Hash

:auth           - String of your authorization key
:max_queue_size - Fixnum of the max calls to remain queued (optional)
:on_error       - Proc which handles error calls from the API
# File lib/koko/tracker/client.rb, line 16
def initialize attrs = {}
  symbolize_keys! attrs

  @auth = attrs[:auth]
  @options = attrs

  check_auth!
end

Public Instance Methods

track_content(attrs) click to toggle source

public: Track content

attrs - Hash (see docs.koko.ai/#track-endpoints)

# File lib/koko/tracker/client.rb, line 28
def track_content attrs
  symbolize_keys! attrs

  timestamp = attrs[:created_at] || Time.now
  check_timestamp! timestamp
  attrs[:created_at] = timestamp.iso8601

  handle_response(Request.new(path: '/track/content').post(@auth, attrs)).body
end
track_flag(attrs) click to toggle source

public: Track flag

attrs - Hash (see docs.koko.ai/#track-endpoints)

# File lib/koko/tracker/client.rb, line 41
def track_flag attrs
  symbolize_keys! attrs

  timestamp = attrs[:created_at] || Time.now
  check_timestamp! timestamp
  attrs[:created_at] = timestamp.iso8601

  handle_response(Request.new(path: '/track/flag').post(@auth, attrs)).body
end
track_moderation(attrs) click to toggle source

public: Track moderation

attrs - Hash (see docs.koko.ai/#track-endpoints)

# File lib/koko/tracker/client.rb, line 54
def track_moderation attrs
  symbolize_keys! attrs

  timestamp = attrs[:created_at] || Time.now
  check_timestamp! timestamp
  attrs[:created_at] = timestamp.iso8601

  handle_response(Request.new(path: '/track/moderation').post(@auth, attrs)).body
end

Private Instance Methods

check_auth!() click to toggle source

private: Checks that the auth is properly initialized

# File lib/koko/tracker/client.rb, line 67
def check_auth!
  fail ArgumentError, 'Auth must be initialized' if @auth.nil?
end
check_timestamp!(timestamp) click to toggle source

private: Checks the timstamp option to make sure it is a Time.

# File lib/koko/tracker/client.rb, line 87
def check_timestamp!(timestamp)
  fail ArgumentError, 'Timestamp must be a Time' unless timestamp.is_a? Time
end
handle_response(response) click to toggle source

private: Ensure response is valid

# File lib/koko/tracker/client.rb, line 72
def handle_response(response)
  unless response.valid?
    if response.status >= 400 && response.status < 500
      raise ArgumentError.new("Invalid request: #{response.body}")
    elsif response.status >= 500 && response.status < 600
      raise RuntimeError.new(response.body)
    else
      raise RuntimeError.new("Unaccepted http code: #{response.status}")
    end
  end

  response
end