thisdata 0.1.0 → 0.1.1

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: 1bd194d89f48c0774d981d4c093ef12a1ed1b16e
4
- data.tar.gz: 39ea8f55aaa501587e46cdb586e41a29f711760c
3
+ metadata.gz: 824e016111d0ad0979a53296fbeaf9020e0d44fe
4
+ data.tar.gz: 88001169e0964ed9068c7d43f524df35e5740acb
5
5
  SHA512:
6
- metadata.gz: 0f9c4c7388e0b80d34e78f798fc5691b4db3f864e7ca84db4ddd11499b1395cd76eb33ea3fcc4ff2a4faaa2e48ac428e877e99fabe63a3d2aa84fb53329488ec
7
- data.tar.gz: fe572684f6b1790d900ee21fe12ba508c19b017196c621b7de68b1dc99ed1cb3bf7f3e4d9106ee8eddb9cc33ca2346bad2f9688b9b992a4b9f69de4872f4807b
6
+ metadata.gz: 6cf37586dfcb83cf7d6c12256f68767b5d5ac0c0de3e90fd7208d64a4413c5c60a6183325f94912adb8636436b902c5779e01c605738e4517198fb03831dc1fe
7
+ data.tar.gz: 681c7e15c374b83551ff7d511eafd6fcc691fedf3249c316860d4bdb3e3aaff5ce9a7416ccf35336979afa54f8cf4f7de44eb96b91d4c2be21fd1b10322b282a
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ # 0.1.1
2
+
3
+ Makes Track requests asynchronous by default.
4
+
1
5
  # 0.1.0
2
6
 
3
7
  Initial release.
data/README.md CHANGED
@@ -20,25 +20,37 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- Run:
23
+ ### Ruby
24
24
 
25
- rails g this_data:install YOUR_API_KEY_HERE
25
+ Configure ThisData as follows:
26
+
27
+ ```
28
+ ThisData.setup do |config|
29
+ config.api_key = "API_KEY_HERE"
30
+ end
31
+ ```
32
+
33
+ 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
36
+
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
+
41
+ ### Rails
26
42
 
27
- You can find your API key by going to [ThisData](https://thisdata.com) >
43
+ Find your API key by going to [ThisData](https://thisdata.com) >
28
44
  Integrations > Login Intelligence API.
29
45
 
46
+ Run:
47
+
48
+ rails g this_data:install YOUR_API_KEY_HERE
49
+
30
50
  The generator will create a file in `config/initializers` called "this_data.rb".
31
51
  If you need to do any further configuration or customization of ThisData,
32
52
  that's the place to do it!
33
53
 
34
- ## Ruby
35
-
36
- You can track any event by calling `ThisData.track` and passing a Hash which
37
- contains an event. See examples and required fields on our API documentation:
38
- http://help.thisdata.com/docs/apiv1events
39
-
40
- ## Rails
41
-
42
54
  The ThisData::TrackRequest module can be included in a ActionController, giving
43
55
  you a handy way to track requests.
44
56
 
@@ -67,7 +79,24 @@ class SessionsController < ApplicationController
67
79
  end
68
80
  ```
69
81
 
82
+ ### Stuck?
83
+
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
+
87
+ `config/initializers/this_data.rb`
88
+ ```
89
+ ThisData.setup do |config|
90
+ # ...
91
+
92
+ config.logger = Rails.logger # or Logger.new($stdout)
93
+ config.asyc = false
94
+ end
95
+ ```
96
+
97
+ Our documentation can be read at http://help.thisdata.com.
70
98
 
99
+ Reach out to developers@thisdata.com if you need any help.
71
100
 
72
101
  ## Development
73
102
 
@@ -77,4 +106,4 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
77
106
 
78
107
  ## Contributing
79
108
 
80
- Bug reports and pull requests are welcome on GitHub at https://github.com/revertio/thisdata-ruby.
109
+ Bug reports and pull requests are welcome on GitHub at https://github.com/thisdata/thisdata-ruby.
@@ -22,6 +22,9 @@ ThisData.setup do |config|
22
22
 
23
23
  # Define a Logger instance if you want to debug / track errors
24
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.asyc = false
25
28
  end
26
29
  EOS
27
30
  end
@@ -27,18 +27,17 @@ module ThisData
27
27
  configuration.defaults
28
28
  end
29
29
 
30
- # Creates a Client and tracks an event.
30
+ # Tracks an event. If `ThisData.configuration.async` is true, this action
31
+ # will be performed in a new Thread.
31
32
  # Event must be a Hash
33
+ # When performed asynchronously, true is always returned.
34
+ # Otherwise an HTTPRequest is returned.
32
35
  def track(event)
33
- Client.new.track(event)
34
- log("Tracked event!")
35
- rescue => e
36
- ThisData.error("Failed to track event:")
37
- ThisData.error(e)
38
- e.backtrace.each do |line|
39
- ThisData.error(line, prefix: false)
36
+ if ThisData.configuration.async
37
+ track_async(event)
38
+ else
39
+ track_with_response(event)
40
40
  end
41
- false
42
41
  end
43
42
 
44
43
  # A helper method to track a log-in event. Validates that the minimum
@@ -67,5 +66,35 @@ module ThisData
67
66
  log(message, level: 'error', prefix: prefix)
68
67
  end
69
68
 
69
+ private
70
+
71
+ # Creates a Client and tracks an event.
72
+ # Event must be a Hash.
73
+ # Rescues and logs all exceptions.
74
+ # Returns an HTTPResponse
75
+ def track_with_response(event)
76
+ response = Client.new.track(event)
77
+ log("Tracked event!")
78
+ response
79
+ rescue => e
80
+ ThisData.error("Failed to track event:")
81
+ ThisData.error(e)
82
+ e.backtrace.each do |line|
83
+ ThisData.error(line, prefix: false)
84
+ end
85
+ false
86
+ end
87
+
88
+ # Performs the track function within a new Thread, so it is non blocking.
89
+ # Returns the Thread created
90
+ def track_async(event)
91
+ Thread.new do
92
+ track_with_response(event)
93
+ end
94
+ rescue => e
95
+ ThisData.error("Cannot create Thread: #{e.inspect}")
96
+ false
97
+ end
98
+
70
99
  end
71
100
  end
@@ -18,6 +18,11 @@ module ThisData
18
18
  # > Login Intelligence API
19
19
  config_option :api_key
20
20
 
21
+ # When true, requests will be performed asynchronously. Setting this to
22
+ # false can help with debugging.
23
+ # Default: true
24
+ config_option :async
25
+
21
26
  # Log the events sent
22
27
  config_option :logger
23
28
 
@@ -41,6 +46,7 @@ module ThisData
41
46
 
42
47
  # set default attribute values
43
48
  @defaults = OpenStruct.new({
49
+ async: true,
44
50
  user_method: :current_user,
45
51
  user_id_method: :id,
46
52
  user_name_method: :name,
@@ -1,6 +1,10 @@
1
1
  # Include ThisData::TrackRequest in your ApplicationController to get a handy
2
2
  # track method which looks at the request and current_user variables to
3
3
  # generate an event.
4
+ #
5
+ # If you include this in a non-ActionController instance, you must respond to
6
+ # `request` and `ThisData.configuration.user_method`
7
+ #
4
8
  module ThisData
5
9
  module TrackRequest
6
10
  class ThisDataTrackError < StandardError; end
@@ -8,47 +12,29 @@ module ThisData
8
12
  # Will pull request and user details from the controller, and send an event
9
13
  # to ThisData.
10
14
  def thisdata_track(verb: ThisData::Verbs::LOG_IN)
11
- controller = controller_from_env
12
- user_details = user_details_from_controller(controller)
13
15
  event = {
14
- verb: verb,
15
- ip: request.remote_ip,
16
+ verb: verb,
17
+ ip: request.remote_ip,
16
18
  user_agent: request.user_agent,
17
- user: user_details
19
+ user: user_details
18
20
  }
19
21
 
20
22
  ThisData.track(event)
21
23
  rescue => e
22
24
  ThisData.error "Could not track event:"
23
25
  ThisData.error e
24
- ThisData.error e.backtrace.limit(5).join("\n")
26
+ ThisData.error e.backtrace[0..5].join("\n")
25
27
  false
26
28
  end
27
29
 
28
30
  private
29
31
 
30
- # Rails keeps a reference to the controller in the env variable.
31
- # Returns a Controller
32
- def controller_from_env
33
- unless controller = env["action_controller.instance"]
34
- raise ThisDataTrackError, "Could not get controller from environment"
35
- end
36
- controller
37
- end
38
-
39
- # Fetches the user record by calling the configured method on the
40
- # controller.
41
- # Will raise a NoMethodError if controller does not respond to the method.
42
- # Returns an object
43
- def user_from_controller(controller)
44
- user = controller.send(ThisData.configuration.user_method)
45
- end
46
-
47
- # Will fetch a user and return a Hash of details for that User.
32
+ # Will fetch a user and return a Hash of details for the User returned
33
+ # by `ThisData.configuration.user_method`.
48
34
  # Will raise a NoMethodError if controller does not return a user,
49
35
  # or we can't get a user id.
50
- def user_details_from_controller(controller)
51
- user = user_from_controller(controller)
36
+ def user_details
37
+ user = send(ThisData.configuration.user_method)
52
38
  {
53
39
  id: user.send(ThisData.configuration.user_id_method),
54
40
  name: value_if_configured(user, "user_name_method"),
@@ -1,3 +1,3 @@
1
1
  module ThisData
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
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.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - ThisData Ltd