Module: ThisData::TrackRequest

Defined in:
lib/this_data/track_request.rb

Defined Under Namespace

Classes: NoUserSpecifiedError, ThisDataTrackError

Instance Method Summary collapse

Instance Method Details

#thisdata_track(verb: ThisData::Verbs::LOG_IN, user: nil, authenticated: nil) ⇒ Object

Will pull request and user details from the controller, and send an event to ThisData. Arguments:

verb: (String, Required). Defaults to ThisData::Verbs::LOG_IN.
user: (Object, Optional). If you want to override the user record
  that we would usually fetch, you can pass it here.
  Unless a user is specified here we'll attempt to get the user record
    as specified in the ThisData gem configuration. This defaults to
    `current_user`.
  The object must respond to at least
    `ThisData.configuration.user_id_method`, which defaults to `id`.
authenticated: (Boolean, Optional, default nil). Used to indicate
  whether a user is authenticated or not. By default we assume that if
  there is a user specified then they are authenticated, but in some
  cases (like a log-in-denied event) you might want to track the user
  but tell us that they are not authenticated.
  In these situations you should set `authenticated` to false.

Returns the result of ThisData.track (an HTTPartyResponse)



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/this_data/track_request.rb', line 34

def thisdata_track(verb: ThisData::Verbs::LOG_IN, user: nil, authenticated: nil)
  # Fetch the user unless it's been overridden
  if user.nil?
    user = send(ThisData.configuration.user_method)
  end

  # Get a Hash of details for the user
  user_details = user_details(user)

  # If specified, set the authenticated state of the user
  unless authenticated.nil?
    user_details.merge!(authenticated: authenticated)
  end

  event = {
    verb:       verb,
    ip:         request.remote_ip,
    user_agent: request.user_agent,
    user:       user_details,
    session: {
      td_cookie_expected: ThisData.configuration.expect_js_cookie,
      td_cookie_id:       td_cookie_value,
    }
  }

  ThisData.track(event)
rescue => e
  ThisData.error "Could not track event:"
  ThisData.error e
  ThisData.error e.backtrace[0..5].join("\n")
  false
end

#thisdata_verify(user: nil) ⇒ Object

Will pull request and user details from the controller, and use ThisData’s verify API to determine how likely it is that the person who is currently logged in has had their account compromised.

Arguments:

user: (Object, Required). If you want to override the user record
  that we would usually fetch, you can pass it here.
  Unless a user is specified here we'll attempt to get the user record
    as specified in the ThisData gem configuration. This defaults to
    `current_user`.
  The object must respond to at least
    `ThisData.configuration.user_id_method`, which defaults to `id`.

Returns a Hash with risk information. See help.thisdata.com/docs/apiv1verify for details



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/this_data/track_request.rb', line 83

def thisdata_verify(user: nil)
  # Fetch the user unless it's been overridden
  if user.nil?
    user = send(ThisData.configuration.user_method)
  end
  # If it's still nil, raise an error
  raise NoUserSpecifiedError, "A user must be provided for verification" if user.nil?

  # Get a Hash of details for the user
  user_details = user_details(user)

  event = {
    ip:         request.remote_ip,
    user_agent: request.user_agent,
    user:       user_details,
    session: {
      td_cookie_expected: ThisData.configuration.expect_js_cookie,
      td_cookie_id:       td_cookie_value,
    }
  }

  ThisData.verify(event)
rescue => e
  ThisData.error "Could not verify current user:"
  ThisData.error e
  ThisData.error e.backtrace[0..5].join("\n")
  nil
end