itly-sdk 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Itly
4
+ ##
5
+ # Loggers class, provide default usual loggers for convenience
6
+ #
7
+ class Loggers
8
+ ##
9
+ # Logger to log into 'itly.log' file on the current directory
10
+ #
11
+ # @return [Logger] the logger
12
+ #
13
+ def self.itly_dot_log
14
+ Logger.new 'itly.log'
15
+ end
16
+
17
+ ##
18
+ # Logger to log to standard out
19
+ #
20
+ # @return [Logger] the logger
21
+ #
22
+ def self.std_out
23
+ Logger.new $stdout
24
+ end
25
+
26
+ ##
27
+ # No logger
28
+ #
29
+ # @return [NilClass] nothing
30
+ #
31
+ def self.nil_logger
32
+ nil
33
+ end
34
+
35
+ ##
36
+ # Shorthand to filter variables in a log message
37
+ #
38
+ # Check if the variable has a value, and return a list for the log message
39
+ #
40
+ # @param [Hash] vars: list of variables
41
+ # @return [String] log message
42
+ #
43
+ def self.vars_to_log(vars)
44
+ vars.collect do |name, value|
45
+ next if value.nil?
46
+
47
+ if value.is_a?(Hash) || value.is_a?(Array)
48
+ "#{name}: #{value}" if value.any?
49
+ else
50
+ "#{name}: #{value}"
51
+ end
52
+ end.compact.join ', '
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,114 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'logger'
4
+
5
+ # Itly main class
6
+ class Itly
7
+ attr_reader :options
8
+
9
+ ##
10
+ # Options class for Itly object initialization
11
+ #
12
+ # Properties:
13
+ #
14
+ # +disabled+: A True/False specifying whether the Itly SDK does any work.
15
+ # When true, all calls to the Itly SDK will be no-ops. Useful in local or development environments.
16
+ #
17
+ # Defaults to false.
18
+ #
19
+ # +environment+: A Symbol specifying the environment the Itly SDK is running in.
20
+ # Can be +Itly::Options::Environment::DEVELOPMENT+ or +Itly::Options::Environment::PRODUCTION+
21
+ #
22
+ # Environment determines which Access Token is used to load the underlying analytics provider libraries.
23
+ # The option also determines safe defaults for handling event validation errors. In production,
24
+ # when the SDK detects an invalid event, it will log an error but still let the event through.
25
+ # In development, the SDK will throw an exception to alert you that something is wrong.
26
+ #
27
+ # Defaults to +DEVELOPMENT+.
28
+ #
29
+ # +plugins+: Pass the list of Plugins object that will receive all events to be tracked.
30
+ #
31
+ # Example:
32
+ # my_plugin = MyPlugin.new api_key: 'abc123'
33
+ # itly = Itly.new
34
+ # itly.load do |options|
35
+ # options.plugins = [my_plugin]
36
+ # end
37
+ #
38
+ # +validation+: Configures the Itly SDK's behavior when events or traits fail validation.
39
+ # Value can be one of the following:
40
+ # - +Itly::Options::Validation::DISABLED+: Disables validation altogether.
41
+ # - +Itly::Options::Validation::TRACK_INVALID+: Specifies whether events that failed validation
42
+ # should still be tracked. Defaults to false in development, true in production.
43
+ # - +Itly::Options::Validation::ERROR_ON_INVALID+: Specifies whether the SDK should throw
44
+ # an exception when validation fails. Defaults to true in development, false in production.
45
+ #
46
+ # Defaults to +ERROR_ON_INVALID+ if the environment is set to +DEVELOPMENT+, or +TRACK_INVALID+
47
+ # if the environment is set to +PRODUCTION+.
48
+ #
49
+ # +logger+: Allow to set a custom Logger. Must be a object of the Logger class or child class, and can be nil.
50
+ # Deflault to nil, to disable Logging.
51
+ #
52
+ class Options
53
+ attr_accessor :disabled, :logger, :plugins, :environment
54
+ attr_writer :validation
55
+
56
+ ##
57
+ # Create a new Options object with default values
58
+ #
59
+ def initialize(
60
+ environment: Itly::Options::Environment::DEVELOPMENT,
61
+ disabled: false,
62
+ plugins: [],
63
+ validation: nil,
64
+ logger: nil
65
+ )
66
+ @environment = environment
67
+ @disabled = disabled
68
+ @plugins = plugins
69
+ @validation = validation
70
+ @logger = logger
71
+ end
72
+
73
+ ##
74
+ # Returns the options that are passed to plugin #load
75
+ #
76
+ # @return [Itly::PluginOptions] plugin options object
77
+ #
78
+ def for_plugin
79
+ ::Itly::PluginOptions.new environment: environment, logger: logger
80
+ end
81
+
82
+ ##
83
+ # Return the current validation behavior
84
+ #
85
+ # @return [Itly::Options::Validation] validation behavior
86
+ #
87
+ def validation
88
+ if @validation.nil?
89
+ if development?
90
+ Itly::Options::Validation::ERROR_ON_INVALID
91
+ else
92
+ Itly::Options::Validation::TRACK_INVALID
93
+ end
94
+ else
95
+ @validation
96
+ end
97
+ end
98
+ end
99
+
100
+ # Shortcut methods
101
+ private
102
+
103
+ def enabled?
104
+ !options.disabled
105
+ end
106
+
107
+ def validation_enabled?
108
+ options.validation != Itly::Options::Validation::DISABLED
109
+ end
110
+
111
+ def logger
112
+ options.logger
113
+ end
114
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Itly
4
+ ##
5
+ # Options class for Itly object initialization
6
+ #
7
+ class Options
8
+ ##
9
+ # This module contains values for the field +environment+ of the +Option+ object
10
+ #
11
+ module Environment
12
+ DEVELOPMENT = :development
13
+ PRODUCTION = :production
14
+ end
15
+
16
+ def development?
17
+ @environment == Itly::Options::Environment::DEVELOPMENT
18
+ end
19
+
20
+ def production?
21
+ @environment == Itly::Options::Environment::PRODUCTION
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Itly
4
+ ##
5
+ # Options class for Itly object initialization
6
+ #
7
+ class Options
8
+ ##
9
+ # This module contains values for the field +validation+ of the +Option+ object
10
+ #
11
+ module Validation
12
+ DISABLED = 0
13
+ TRACK_INVALID = 1
14
+ ERROR_ON_INVALID = 2
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,116 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Itly main class
4
+ class Itly
5
+ ##
6
+ # Parent class for all plugins
7
+ #
8
+ # When creating a custom plugin, you need to create a child class of Itly::Plugin
9
+ #
10
+ class Plugin
11
+ ##
12
+ # Called when the Itly SDK is being loaded and is ready to load your plugin.
13
+ #
14
+ # @param [Itly::PluginOptions] options: The option for the plugin
15
+ #
16
+ def load(options:); end
17
+
18
+ ##
19
+ # Identify a user in your application and associate all future events with
20
+ # their identity, or to set their traits.
21
+ #
22
+ # @param [String] user_id: the id of the user in your application
23
+ # @param [Hash] properties: the properties containing user's traits to pass to your application
24
+ # @param [Itly::PluginCallOptions] options: plugin specific options.
25
+ #
26
+ def identify(user_id:, properties: nil, options: nil); end
27
+
28
+ def post_identify(user_id:, properties:, validation_results:); end
29
+
30
+ ##
31
+ # Associate a user with their group (for example, their department or company),
32
+ # or to set the group's traits.
33
+ #
34
+ # @param [String] user_id: the id of the user in your application
35
+ # @param [String] group_id: the id of the group in your application
36
+ # @param [Hash] properties: the properties to pass to your application
37
+ # @param [Itly::PluginCallOptions] options: plugin specific option.
38
+ #
39
+ def group(user_id:, group_id:, properties: nil, options: nil); end
40
+
41
+ def post_group(user_id:, group_id:, properties:, validation_results:); end
42
+
43
+ ##
44
+ # Let record page views, along with optional extra information about the page viewed by the user.
45
+ #
46
+ # @param [String] user_id: the id of the user in your application
47
+ # @param [String] category: the category of the page
48
+ # @param [String] name: the name of the page.
49
+ # @param [Hash] properties: the properties to pass to your application
50
+ # @param [Itly::PluginCallOptions] options: plugin specific option.
51
+ #
52
+ def page(user_id:, category: nil, name: nil, properties: nil, options: nil); end
53
+
54
+ def post_page(user_id:, category:, name:, properties:, validation_results:); end
55
+
56
+ ##
57
+ # Track an event, call the event's corresponding function.
58
+ #
59
+ # See +Itly#track+ for more information
60
+ #
61
+ # @param [String] user_id: the id of the user in your application
62
+ # @param [Event] event: the events to track
63
+ # @param [Itly::PluginCallOptions] options: plugin specific option.
64
+ #
65
+ def track(user_id:, event:, options: nil); end
66
+
67
+ def post_track(user_id:, event:, validation_results:); end
68
+
69
+ ##
70
+ # Associate one user ID with another (typically a known user ID with an anonymous one).
71
+ #
72
+ # @param [String] user_id: The ID that the user will be identified by going forward.
73
+ # @param [String] previous_id: The ID the user has been identified by so far.
74
+ # @param [Itly::PluginCallOptions] options: plugin specific option.
75
+ #
76
+ def alias(user_id:, previous_id:, options: nil); end
77
+
78
+ def post_alias(user_id:, previous_id:); end
79
+
80
+ ##
81
+ # Flush data
82
+ #
83
+ def flush; end
84
+
85
+ ##
86
+ # Stop all processes and free resources.
87
+ #
88
+ def shutdown; end
89
+
90
+ ##
91
+ # Reset the SDK's (and all plugins') state. This method is usually called when a user logs out.
92
+ #
93
+ def reset; end
94
+
95
+ ##
96
+ # Validate an Event
97
+ #
98
+ # See +Itly#validate+ for more information
99
+ #
100
+ # Your plugin can return a +Itly::ValidationResponse+ object to provide success status
101
+ # and validation message; otherwise it can return +nil+ which will be interpreted as valid=true
102
+ #
103
+ def validate(event:); end
104
+
105
+ ##
106
+ # Get the plugin ID, which is the underscored class name. Use only the child class in case of nested classes
107
+ #
108
+ # @return [String] plugin id
109
+ #
110
+ def id
111
+ name = (self.class.name || 'UnknownPluginClass').gsub('::', '-')
112
+ name = (name || 'UnknownPluginClass').gsub(/([A-Z]+)/, '_\1').gsub(/-_/, '-').sub(/^_/, '').sub(/^itly-/i, '')
113
+ name.downcase
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Itly
4
+ ##
5
+ # PluginCallOptions virtual class
6
+ # Parent class of plugin specific options
7
+ #
8
+ class PluginCallOptions
9
+ ##
10
+ # Inspect the object
11
+ #
12
+ # @return [String] the object description
13
+ #
14
+ def inspect
15
+ to_s
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Itly main class
4
+ class Itly
5
+ ##
6
+ # PluginOptions class for Itly Plugins #load methods
7
+ #
8
+ # +environment+: A Symbol specifying the environment the Itly SDK is running in. #
9
+ # +logger+: Allow to set a custom Logger.
10
+ #
11
+ class PluginOptions
12
+ attr_reader :environment, :logger
13
+
14
+ ##
15
+ # Create a new PluginOptions object
16
+ #
17
+ def initialize(environment:, logger:)
18
+ @environment = environment
19
+ @logger = logger
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Itly main class
4
+ class Itly
5
+ ##
6
+ # Manage list of Plugins
7
+ #
8
+ module Plugins
9
+ private
10
+
11
+ # Yield the block with each instanciated plugin
12
+ def run_on_plugins
13
+ raise 'Need a block' unless block_given?
14
+
15
+ options.plugins.collect do |plugin|
16
+ yield plugin
17
+ rescue StandardError => e
18
+ logger&.error "Itly Error in #{plugin.class.name}. #{e.class.name}: #{e.message}"
19
+ raise e if options.development?
20
+
21
+ nil
22
+ end.compact
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Itly
4
+ ##
5
+ # Contains the result of a validation
6
+ #
7
+ # +valid+: [True/False] indicating if the validation succeeded or failed
8
+ # +plugin_id+: [String] an id identifying your plugin
9
+ # +message+: [String] the message you want to appear in the logs in case of error
10
+ #
11
+ class ValidationResponse
12
+ attr_reader :valid, :plugin_id, :message
13
+
14
+ ##
15
+ # Create a new ValidationResponse object
16
+ #
17
+ def initialize(valid:, plugin_id:, message: '')
18
+ @valid = valid
19
+ @plugin_id = plugin_id
20
+ @message = message
21
+ end
22
+
23
+ ##
24
+ # Describe the object
25
+ #
26
+ # @return [String] the object description
27
+ #
28
+ def to_s
29
+ "#<#{self.class.name}: valid: #{valid}, plugin_id: #{plugin_id}, message: #{message}>"
30
+ end
31
+
32
+ ##
33
+ # Inspect the object
34
+ #
35
+ # @return [String] the object description
36
+ #
37
+ def inspect
38
+ to_s
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Itly main class
4
+ class Itly
5
+ VERSION = '0.1.0'
6
+ end