module Behave

Public Class Methods

api(path, method=:get, options={}) click to toggle source
# File lib/behave.rb, line 16
def self.api(path, method=:get, options={})
  # Convert body object to JSON string as the API only accepts JSON body
  options[:body] = options[:body].to_json if options.has_key? :body
  result = self.send method, path, options
  raise Error.new(result.code), result.message if result.code != 200
  # Empty response
  return {} unless result.parsed_response["data"]
  #
  # @TODO: refactor - ugly hack we use Utils.symbolize_keys to change the type of the keys (string -> symbol)
  #
  # If response is a single object (Hash)
  if result.parsed_response["data"].kind_of? Hash
    return Utils.symbolize_keys result.parsed_response["data"]
  # If response is an array of Hash
  elsif result.parsed_response["data"].kind_of? Array
    return result.parsed_response["data"].map {|h| Utils.symbolize_keys h}     
  end
end
identify(playerId, traits={}) click to toggle source
# File lib/behave.rb, line 45
def self.identify(playerId, traits={})
  api "/players/#{playerId}/identify", :post,
    body: {
      traits: traits
    }
end
init(token) click to toggle source
# File lib/behave.rb, line 12
def self.init(token)
  headers 'X-Behave-Api-Token' => token
end
track(playerId, event, context={}) { |result| ... } click to toggle source
# File lib/behave.rb, line 35
def self.track(playerId, event, context={})
  result = api "/players/#{playerId}/track", :post, 
    body: {
      verb: event,
      context: context
    }
  yield result if block_given?
  result
end