Module: ThisData

Defined in:
lib/this_data/rule.rb,
lib/this_data.rb,
lib/this_data/event.rb,
lib/this_data/verbs.rb,
lib/this_data/client.rb,
lib/this_data/version.rb,
lib/this_data/configuration.rb,
lib/this_data/track_request.rb,
lib/generators/this_data/install_generator.rb

Overview

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`

Defined Under Namespace

Modules: TrackRequest Classes: Client, Configuration, Event, InstallGenerator, Rule, Verbs

Constant Summary collapse

EVENTS_ENDPOINT =

API Endpoint Paths

'/events'
VERIFY_ENDPOINT =
'/verify'
RULES_ENDPOINT =
'/rules'
RISK_LEVEL_GREEN =
'green'
RISK_LEVEL_ORANGE =
'orange'
RISK_LEVEL_RED =
'red'
VERSION =
"0.2.2"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject



35
36
37
# File 'lib/this_data.rb', line 35

def configuration
  @configuration ||= ThisData::Configuration.new
end

Class Method Details

.default_configurationObject



39
40
41
# File 'lib/this_data.rb', line 39

def default_configuration
  configuration.defaults
end

.error(message, prefix: true) ⇒ Object



103
104
105
# File 'lib/this_data.rb', line 103

def error(message, prefix: true)
  log(message, level: 'error', prefix: prefix)
end

.log(message, level: 'info', prefix: true) ⇒ Object



94
95
96
97
98
99
# 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

.setup {|configuration| ... } ⇒ Object

Yields:



31
32
33
# File 'lib/this_data.rb', line 31

def setup
  yield(configuration)
end

.track(event, query: {}) ⇒ Object

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.
    


52
53
54
55
56
57
58
# File 'lib/this_data.rb', line 52

def track(event, query: {})
  if ThisData.configuration.async
    track_async(event, query: query)
  else
    track_with_response(event, query: query)
  end
end

.track_login(ip: '', user: {}, user_agent: nil) ⇒ Object

A helper method to track a log-in event. Validates that the minimum required data is present.

Raises:

  • (ArgumentError)


83
84
85
86
87
88
89
90
91
92
# File 'lib/this_data.rb', line 83

def (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(params, query: {}) ⇒ Object

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



72
73
74
75
76
77
78
79
# File 'lib/this_data.rb', line 72

def verify(params, query: {})
  response = Client.new.post(
    ThisData::VERIFY_ENDPOINT,
    body: JSON.generate(params),
    query: query
  )
  response.parsed_response
end

.warn(message, prefix: true) ⇒ Object



100
101
102
# File 'lib/this_data.rb', line 100

def warn(message, prefix: true)
  log(message, level: 'warn', prefix: prefix)
end