class Smyte::Client

Attributes

api_key[R]
api_secret[R]
enabled[R]
logger[R]

Public Class Methods

new(options=nil) click to toggle source
# File lib/smyte/client.rb, line 9
def initialize(options=nil)
  options ||= {}
  [:api_key, :api_secret, :logger, :enabled].each do |key|
    if options.has_key?(key)
      value = options[key]
    else
      value = Smyte.send(key)
    end
    instance_variable_set("@#{key}", value)
  end
end

Public Instance Methods

action(event_name, payload) click to toggle source
# File lib/smyte/client.rb, line 21
def action(event_name, payload)
  return true if !enabled

  process(event_name, payload, "/v2/action")
  true # any success is just true - nothing else to know
end
classify(event_name, payload, options={}) click to toggle source
# File lib/smyte/client.rb, line 28
def classify(event_name, payload, options={})
  return Smyte::Classification.allowed if !enabled

  response = process(event_name, payload, "/v2/action/classify")
  Smyte::Classification.new(response, options)
end

Protected Instance Methods

connection() click to toggle source
# File lib/smyte/client.rb, line 37
def connection
  return @connection if @connection
  options = {
    url: 'https://api.smyte.com',
    headers: {
      'Content-Type'  => 'application/json'
    }
  }
  @connection = Faraday.new(options) do |faraday|
    # faraday.response :logger                # log requests to STDOUT
    faraday.adapter  Faraday.default_adapter
  end
  @connection.basic_auth(api_key, api_secret)
  @connection
end
process(event_name, payload, path) click to toggle source
# File lib/smyte/client.rb, line 53
def process(event_name, payload, path)
  payload ||= {}
  if payload.has_key?('name')
    payload['name'] = event_name
  else
    payload[:name] = event_name
  end

  response = connection.post do |req|
    req.url path
    req.body = payload.to_json
  end
  
  if response.success?
    return JSON.parse(response.body)
  else
    hash = JSON.parse(response.body) rescue nil
    error = "Smyte Error (#{response.status})"
    if hash && hash["message"]
      error << ": "
      error << hash["message"]
      error << "\n"
    end
    raise error
  end
end