thisdata 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fef5e01bc1e5fc5a2ec1298351dd1ac7bfc06a47
4
- data.tar.gz: 07881c984c030b535c60b002b50b0a95e2eab107
3
+ metadata.gz: c769ec94d780ff1f93699730acb971cf09d0d580
4
+ data.tar.gz: 1d4a7fa9e5b57fc1e6d563d02dd50473f4a1f809
5
5
  SHA512:
6
- metadata.gz: 81a2cc15499d5777ae5c454e8bd80405339a25bcee6e5cd224dec41624088373a2e509099d89c67243f0961c850fab84e4b3ca0ac732f80514232f19d31419d3
7
- data.tar.gz: ec762b91037a20c3f0aa151f87e52db117d187fede934eb89b0a787325fdc15c9f0f5b8c3e5ba7c9bc04bf95db72f531cf7e61f6162754c0af988aa6e3c53cba
6
+ metadata.gz: b06b6d18354099b7dafb19eb982710606d1cba120fe9b0c99db8f3e7107f0c5a3b748b136eb4a554ad6ac3d73b6bab727a7169da9e146d80dd99b49944f1b01d
7
+ data.tar.gz: ad687196cfd094456dc13046e76ccc12ca60a92fbc718bf770e0d3c109e6e4f7afa3bffe4cc679338a3b17d56faf78998e737d0b7ebc8c770f1d208d2f61f4ed
data/CHANGELOG CHANGED
@@ -1,3 +1,12 @@
1
+ # 0.1.3
2
+
3
+ In the `ThisData::TrackRequest` module, make tracking a bit easier especially
4
+ in the case where `current_user` will return nil - e.g. when someone failed to
5
+ log in to that account.
6
+
7
+ Also update the documentation and default configuration options to better enable
8
+ people getting started with the ThisData gem.
9
+
1
10
  # 0.1.2
2
11
 
3
12
  Fix a typo in the configuration generator (`asyc` instead of `async`)
data/README.md CHANGED
@@ -20,83 +20,124 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- ### Ruby
23
+ Our API endpoint documentation, tutorials, and sample code can all be found at
24
+ http://help.thisdata.com
25
+
26
+ ### Plain Old Ruby
27
+
28
+ #### Configuration
24
29
 
25
30
  Configure ThisData as follows:
26
31
 
27
- ```
32
+ ```ruby
28
33
  ThisData.setup do |config|
29
- config.api_key = "API_KEY_HERE"
34
+ config.api_key = "API_KEY_HERE" # Don't commit your key to source control!
35
+ config.logger = Logger.new($stdout)
36
+ config.async = false
30
37
  end
31
38
  ```
32
39
 
40
+ See `this_data/configuration.rb` for more options, and some suggested
41
+ Ruby on Rails options in `this_data/generators/install_generator.rb`.
42
+
43
+ For example, in production you will probably want asynchronous and non-logging
44
+ behaviour.
45
+
46
+ #### Tracking an Event
47
+
33
48
  You can then track any event by calling `ThisData.track` and passing a Hash which
34
- contains an event. See examples and required fields on our API documentation:
35
- http://help.thisdata.com/docs/apiv1events
49
+ contains an event. In the following example, we're tracking a user logging in
50
+ to our app:
51
+
52
+ ```ruby
53
+ ThisData.track(
54
+ {
55
+ ip: request.remote_ip,
56
+ user_agent: request.user_agent,
57
+ verb: ThisData::Verbs::LOG_IN,
58
+ user: {
59
+ id: user.id.to_s,
60
+ name: user.name,
61
+ email: user.email
62
+ mobile: user.mobile
63
+ }
64
+ }
65
+ )
66
+ ```
36
67
 
37
- **Important!** You should not commit your API keys to source control. Where
38
- possible, use environment variables / a non-committed secrets file / something
39
- similar.
40
68
 
41
69
  ### Rails
42
70
 
71
+ #### Set Up
72
+
73
+ We have a generator which will set some nice configuration options for Ruby on
74
+ Rails users.
75
+
43
76
  Find your API key by going to [ThisData](https://thisdata.com) >
44
77
  Integrations > Login Intelligence API.
45
78
 
46
79
  Run:
47
80
 
48
- rails g this_data:install YOUR_API_KEY_HERE
81
+ ```ruby
82
+ rails g this_data:install YOUR_API_KEY_HERE
83
+ ```
49
84
 
50
85
  The generator will create a file in `config/initializers` called "this_data.rb".
51
86
  If you need to do any further configuration or customization of ThisData,
52
87
  that's the place to do it!
53
88
 
54
- The ThisData::TrackRequest module can be included in a ActionController, giving
55
- you a handy way to track requests.
89
+ #### Tracking
56
90
 
57
- e.g. in `app/controllers/application_controller.rb`
58
- ```
59
- class ApplicationController < ActionController::Base
60
- include ThisData::TrackRequest
91
+ **The recommended way to track events is as above - explicitly calling
92
+ `ThisData.track`.**
61
93
 
62
- ...
63
- end
64
- ```
94
+ However, we do provide a `ThisData::TrackRequest` module which, when included in
95
+ an ActionController, gives you a simple way to track requests.
65
96
 
66
- and in your sessions controller:
67
- ```
97
+ You include the module, then call `thisdata_track`. Easy!
98
+
99
+ e.g. in your sessions controller:
100
+
101
+ ```ruby
68
102
  class SessionsController < ApplicationController
103
+ include ThisData::TrackRequest
104
+
105
+ def create
106
+ if User.authenticate(params[:email], params[:password])
107
+ # Do the things one usually does for a successful auth
69
108
 
70
- def finalize
71
- if login_was_valid?
72
- # do login stuff
109
+ # And also track the login
73
110
  thisdata_track
74
111
  else
75
- thisdata_track('login-denied')
112
+ # Their credentials are wrong. Are they trying to access
113
+ # a valid account?
114
+ if attempted_user = User.find_by(email: params[:email])
115
+ thisdata_track(
116
+ verb: ThisData::Verbs::LOG_IN_DENIED,
117
+ user: attempted_user
118
+ )
119
+ else
120
+ # email and password were both incorrect
121
+ end
76
122
  end
77
123
  end
78
-
79
124
  end
80
125
  ```
81
126
 
82
- ### Stuck?
127
+ Note: as with many sensitive operations, taking different actions when an
128
+ account exists vs. when an account doesn't exist can lead to a information
129
+ disclosure through timing attacks.
83
130
 
84
- By default there is no logger configured, and requests are performed
85
- asynchronously. The following config settings can be helpful in debugging issues:
86
131
 
87
- `config/initializers/this_data.rb`
88
- ```
89
- ThisData.setup do |config|
90
- # ...
132
+ ### Stuck?
91
133
 
92
- config.logger = Rails.logger # or Logger.new($stdout)
93
- config.async = false
94
- end
95
- ```
134
+ The API endpoint validates the events you send, and will return errors in the
135
+ body of the response. Enabling logging will help you debug this.
96
136
 
97
- Our documentation can be read at http://help.thisdata.com.
137
+ Our documentation can be read at http://help.thisdata.com. Our API will return
138
+ error messages you can inspect if the payload is missing required attributes.
98
139
 
99
- Reach out to developers@thisdata.com if you need any help.
140
+ Reach out to developers@thisdata.com if you need any help, or open an issue!
100
141
 
101
142
  ## Development
102
143
 
@@ -7,24 +7,51 @@ module ThisData
7
7
  def create_configuration_file
8
8
  initializer "this_data.rb" do
9
9
  <<-EOS
10
+ # Specifies configuration options for the ThisData gem.
10
11
  ThisData.setup do |config|
12
+
13
+ # This is where your API key goes. You should almost certainly not have it
14
+ # committed to source control, but instead load it from a secret store.
15
+ # Default: nil
11
16
  config.api_key = "#{api_key}"
12
17
 
13
- # user_method will be called on a controller when using TrackRequest
18
+ # Define a Logger instance if you want to debug or track errors
19
+ # This tells ThisData to log in the development environment.
20
+ # Comment it out to use the default behaviour across all environments.
21
+ # Default: nil
22
+ config.logger = Rails.logger if Rails.env.development?
23
+
24
+ # When true, will perform track events asynchronously.
25
+ # It is true by default, but here we explicitly tell ThisData to make it
26
+ # synchronous in test and development mode, to aide getting started.
27
+ # Comment it out to use the default behaviour across all environments.
28
+ # Default: true
29
+ config.async = !(Rails.env.test? || Rails.env.development?)
30
+
31
+
32
+ # These configuration options are used when for the TrackRequest module.
33
+
34
+ # user_method will be called on a controller to get a user object.
35
+ # Default: :current_user
14
36
  # config.user_method = :current_user
15
37
 
16
38
  # The following methods will be called on the object returned by user_method,
17
39
  # to capture details about the user
40
+
41
+ # Required. This method should return a unique ID for a user.
42
+ # Default: :id
18
43
  # config.user_id_method = :id
44
+
45
+ # Optional. This method should return the user's name.
46
+ # Default: :name
19
47
  # config.user_name_method = :name
48
+ # This method should return the user's email.
49
+ # Default: :email
20
50
  # config.user_email_method = :email
51
+ # This method should return the user's mobile phone number.
52
+ # Default: :mobile
21
53
  # config.user_mobile_method = :mobile
22
54
 
23
- # Define a Logger instance if you want to debug / track errors
24
- # config.logger = Rails.logger unless Rails.env.production?
25
-
26
- # Set this to false if you want ThisData.track to perform in the same thread
27
- # config.async = false
28
55
  end
29
56
  EOS
30
57
  end
@@ -74,7 +74,11 @@ module ThisData
74
74
  # Returns an HTTPResponse
75
75
  def track_with_response(event)
76
76
  response = Client.new.track(event)
77
- log("Tracked event!")
77
+ if response.try(:success?)
78
+ log("Tracked event! #{response.response.inspect}")
79
+ else
80
+ warn("Track failure! #{response.response.inspect} #{response.body}")
81
+ end
78
82
  response
79
83
  rescue => e
80
84
  ThisData.error("Failed to track event:")
@@ -11,12 +11,27 @@ module ThisData
11
11
 
12
12
  # Will pull request and user details from the controller, and send an event
13
13
  # to ThisData.
14
- def thisdata_track(verb: ThisData::Verbs::LOG_IN)
14
+ # Arguments:
15
+ # verb: (String, Required). Defaults to ThisData::Verbs::LOG_IN.
16
+ # user: (Object, Optional). If you want to override the user record
17
+ # that we would usually fetch, you can pass it here.
18
+ # Unless a user is specified here we'll attempt to get the user record
19
+ # as specified in the ThisData gem configuration. This defaults to
20
+ # `current_user`.
21
+ # The object must respond to at least
22
+ # `ThisData.configuration.user_id_method`, which defaults to `id`.
23
+ #
24
+ # Returns the result of ThisData.track (an HTTPartyResponse)
25
+ def thisdata_track(verb: ThisData::Verbs::LOG_IN, user: nil)
26
+ if user.nil?
27
+ user = send(ThisData.configuration.user_method)
28
+ end
29
+
15
30
  event = {
16
31
  verb: verb,
17
32
  ip: request.remote_ip,
18
33
  user_agent: request.user_agent,
19
- user: user_details
34
+ user: user_details(user)
20
35
  }
21
36
 
22
37
  ThisData.track(event)
@@ -29,12 +44,11 @@ module ThisData
29
44
 
30
45
  private
31
46
 
32
- # Will fetch a user and return a Hash of details for the User returned
33
- # by `ThisData.configuration.user_method`.
34
- # Will raise a NoMethodError if controller does not return a user,
35
- # or we can't get a user id.
36
- def user_details
37
- user = send(ThisData.configuration.user_method)
47
+ # Will return a Hash of details for a user using the methods specified
48
+ # in the gem configuration.
49
+ # Will raise a NoMethodError if user does not respond the
50
+ # specified `user_id_method`. A user id is required for all events.
51
+ def user_details(user)
38
52
  {
39
53
  id: user.send(ThisData.configuration.user_id_method),
40
54
  name: value_if_configured(user, "user_name_method"),
@@ -1,3 +1,3 @@
1
1
  module ThisData
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: thisdata
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - ThisData Ltd
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-02-25 00:00:00.000000000 Z
12
+ date: 2016-02-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: httparty