Module: ThisData

Defined in:
lib/this_data/event.rb,
lib/this_data.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, Verbs

Constant Summary collapse

EVENTS_ENDPOINT =

API Endpoint Paths

'/events'
VERIFY_ENDPOINT =
'/verify'
RISK_LEVEL_GREEN =
'green'
RISK_LEVEL_ORANGE =
'orange'
RISK_LEVEL_RED =
'red'
VERSION =
"0.2.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject



33
34
35
# File 'lib/this_data.rb', line 33

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

Class Method Details

.default_configurationObject



37
38
39
# File 'lib/this_data.rb', line 37

def default_configuration
  configuration.defaults
end

.error(message, prefix: true) ⇒ Object



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

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

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



91
92
93
94
95
96
# File 'lib/this_data.rb', line 91

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:



29
30
31
# File 'lib/this_data.rb', line 29

def setup
  yield(configuration)
end

.track(event) ⇒ 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.
    


50
51
52
53
54
55
56
# File 'lib/this_data.rb', line 50

def track(event)
  if ThisData.configuration.async
    track_async(event)
  else
    track_with_response(event)
  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)


80
81
82
83
84
85
86
87
88
89
# File 'lib/this_data.rb', line 80

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) ⇒ 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



70
71
72
73
74
75
76
# File 'lib/this_data.rb', line 70

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

.warn(message, prefix: true) ⇒ Object



97
98
99
# File 'lib/this_data.rb', line 97

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