Sha256: f5ee29030e4b9b1211c58f9bc646ab6e175f9b55b76fb615b12d9860bf523f1f

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

require_relative 'event_manager'
require_relative 'config'
require_relative 'sn_exception'
require_relative 'utils'
require 'json'

class SecureNativeSDK
  def initialize(api_key, options: SecureNativeOptions.new)
    if api_key == nil
      raise SecureNativeSDKException.new
    end

    @api_key = api_key
    @options = options
    @event_manager = EventManager.new(@api_key, options: @options)
  end

  def api_key
    @api_key
  end

  def version
    Config::SDK_VERSION
  end

  def track(event)
    validate_event(event)
    @event_manager.send_async(event, Config::TRACK_EVENT)
  end

  def verify(event)
    validate_event(event)
    res = @event_manager.send_sync(event, Config::VERIFY_EVENT)
    if res.status_code == 200
      return JSON.parse(res.body)
    end
    nil
  end

  def flow(event) # Note: For future purposes
    validate_event(event)
    @event_manager.send_async(event, Config::FLOW_EVENT)
  end

  def verify_webhook(hmac_header, body)
    Utils.verify_signature(@api_key, body, hmac_header)
  end

  def flush
    @event_manager.flush
  end

  private

  def validate_event(event)
    unless event.params.nil?
      if event.params.length > Config::MAX_ALLOWED_PARAMS
        event.params = event.params[0, Config::MAX_ALLOWED_PARAMS]
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
securenative-0.1.5 lib/securenative/secure_native_sdk.rb