module ThisData
A wrapper for the GET /events API
A wrapper for the GET /rules API
Note: No error handling is included in this wrapper
Include ThisData::TrackRequest
in your ApplicationController to get a handy track method which looks at the request and current_user variables to generate an event.
This module will also provide access to the verify API.
If you include this in a non-ActionController instance, you must respond to `request` and `ThisData.configuration.user_method`
We use verbs to distinguish between different types of events. The following is a a list of verbs which hold special semantic meaning to us. In addition to these you can send any verb you like.
Learn more about these verbs, and others, here:
http://help.thisdata.com/docs/verbs
Constants
- EVENTS_ENDPOINT
API Endpoint Paths
- RISK_LEVEL_GREEN
Risk level constants, defined at help.thisdata.com/docs/apiv1verify#what-does-the-risk_level-mean
- RISK_LEVEL_ORANGE
- RISK_LEVEL_RED
- RULES_ENDPOINT
- VERIFY_ENDPOINT
- VERSION
Attributes
Configuration
Object (instance of ThisData::Configuration
)
Public Class Methods
# File lib/this_data.rb, line 35 def configuration @configuration ||= ThisData::Configuration.new end
# File lib/this_data.rb, line 39 def default_configuration configuration.defaults end
# File lib/this_data.rb, line 103 def error(message, prefix: true) log(message, level: 'error', prefix: prefix) end
# File lib/this_data.rb, line 94 def log(message, level: 'info', prefix: true) if prefix message = "[ThisData] " + message.to_s end configuration.logger.send(level, message) if configuration.logger end
# File lib/this_data.rb, line 31 def setup yield(configuration) end
Tracks a user initiated event which has occurred within your app, e.g. a user logging in.
Performs asynchronously if ThisData.configuration
.async is true.
Parameters:
-
event (Required: Hash) a Hash containing details about the event.
See http://help.thisdata.com/v1.0/docs/apiv1events for a full & current list of available options.
# File lib/this_data.rb, line 52 def track(event, options={}) if ThisData.configuration.async track_async(event, options) else track_with_response(event, options) end end
A helper method to track a log-in event. Validates that the minimum required data is present.
# File lib/this_data.rb, line 83 def track_login(ip: '', user: {}, user_agent: nil) raise ArgumentError, "IP Address is required" unless ip.length raise ArgumentError, "User needs ID value" unless user[:id].to_s.length track({ verb: ThisData::Verbs::LOG_IN, ip: ip, user_agent: user_agent, user: user }) end
Verify asks ThisData's API “is this request really from this user?”, and returns a response with a risk score.
Note: this method does not perform error handling.
Parameters:
-
params (Required: Hash) a Hash containing details about the current
request & user. See http://help.thisdata.com/docs/apiv1verify for a full & current list of available options.
Returns a Hash
# File lib/this_data.rb, line 72 def verify(params, options={}) response = Client.new.post( ThisData::VERIFY_ENDPOINT, body: JSON.generate(params), query: options ) response.parsed_response end
# File lib/this_data.rb, line 100 def warn(message, prefix: true) log(message, level: 'warn', prefix: prefix) end
Private Class Methods
Performs the track function within a new Thread, so it is non blocking. Returns the Thread created
# File lib/this_data.rb, line 133 def track_async(event, options={}) Thread.new do track_with_response(event, options) end rescue => e ThisData.error("Cannot create Thread: #{e.inspect}") false end
Creates a Client
and tracks an event. Event
must be a Hash. Rescues and logs all exceptions. Returns an HTTPResponse
# File lib/this_data.rb, line 113 def track_with_response(event, options={}) response = Client.new.track(event, options) success = response && response.success? # HTTParty doesn't like `.try` if success log("Tracked event! #{response.response.inspect}") else warn("Track failure! #{response.response.inspect} #{response.body}") end response rescue => e ThisData.error("Failed to track event:") ThisData.error(e) e.backtrace.each do |line| ThisData.error(line, prefix: false) end false end