Class: Itly::Plugin::Testing

Inherits:
Itly::Plugin show all
Defined in:
lib/itly/plugin/testing/testing.rb,
lib/itly/plugin/testing/version.rb,
lib/itly/plugin/testing/call_options.rb

Overview

Testing plugin class for Itly SDK

Defined Under Namespace

Classes: CallOptions

Constant Summary collapse

VERSION =
'0.1.1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(disabled: false) ⇒ Testing

Instantiate a new Plugin::Testing

Parameters:

  • disabled: (TrueClass/FalseClass) (defaults to: false)

    set to true to disable the plugin. Default to false



19
20
21
22
23
# File 'lib/itly/plugin/testing/testing.rb', line 19

def initialize(disabled: false)
  super()
  @calls = Concurrent::Hash.new
  @disabled = disabled
end

Instance Attribute Details

#disabledObject (readonly)

Returns the value of attribute disabled.



12
13
14
# File 'lib/itly/plugin/testing/testing.rb', line 12

def disabled
  @disabled
end

#loggerObject (readonly)

Returns the value of attribute logger.



12
13
14
# File 'lib/itly/plugin/testing/testing.rb', line 12

def logger
  @logger
end

Instance Method Details

#all(user_id: nil) ⇒ Array

Get all events that was sent to the track method

Parameters:

  • user_id: (String) (defaults to: nil)

    (optional) filter the events returned by the method by the user_id. Leave it nil to get all events

Returns:

  • (Array)

    array of EvItly::Event objects that was sent to the track method



55
56
57
58
59
# File 'lib/itly/plugin/testing/testing.rb', line 55

def all(user_id: nil)
  calls = @calls['track'].dup
  calls = calls.select { |call| call[:user_id] == user_id } unless user_id.nil?
  calls.collect { |call| call[:event] }
end

#first_of_type(class_name:, user_id: nil) ⇒ Itly::Event

Get the first event that was sent to the track method of the specified class name

Parameters:

  • class_name: (Event)

    the method return only an event of the specified class name

  • user_id: (String) (defaults to: nil)

    (optional) filter the events returned by the method by the user_id. Leave it nil to get all events

Returns:

  • (Itly::Event)

    object that was sent to the track method. nil if none was found



82
83
84
# File 'lib/itly/plugin/testing/testing.rb', line 82

def first_of_type(class_name:, user_id: nil)
  of_type(class_name: class_name, user_id: user_id).first
end

#idString

Get the plugin ID

Returns:

  • (String)

    plugin id



102
103
104
# File 'lib/itly/plugin/testing/testing.rb', line 102

def id
  'testing'
end

#load(options:) ⇒ Object

Initialize TestingApi client

Parameters:

  • options: (Itly::PluginOptions)

    plugins options



30
31
32
33
34
35
36
37
38
39
# File 'lib/itly/plugin/testing/testing.rb', line 30

def load(options:)
  super
  # Get options
  @logger = options.logger

  # Log
  logger&.info "#{id}: load()"

  logger&.info "#{id}: plugin is disabled!" if @disabled
end

#method_nameObject

Tracking methods

Accept any params, store them in the @calls instance variable



91
92
93
94
95
# File 'lib/itly/plugin/testing/testing.rb', line 91

%i[alias identify group track].each do |method_name|
  define_method method_name do |args|
    track_calls method_name.to_s, args
  end
end

#nameObject

Testing specific plugin options class for calls to plugin methods



16
17
18
19
20
21
22
23
# File 'lib/itly/plugin/testing/call_options.rb', line 16

%w[Identify Group Page Track Alias].each do |name|
  class_eval(
    <<-EVAL, __FILE__, __LINE__ + 1
      class #{name}Options < CallOptions         # class IdentifyOptions < CallOptions
      end                                        # end
    EVAL
  )
end

#of_type(class_name:, user_id: nil) ⇒ Array

Get events that was sent to the track method of the specified class name

Parameters:

  • class_name: (Event)

    the method return only events of the specified class name

  • user_id: (String) (defaults to: nil)

    (optional) filter the events returned by the method by the user_id. Leave it nil to get all events

Returns:

  • (Array)

    array of Itly::Event objects that was sent to the track method



69
70
71
72
# File 'lib/itly/plugin/testing/testing.rb', line 69

def of_type(class_name:, user_id: nil)
  calls = all user_id: user_id
  calls.select { |call| call.is_a? class_name }
end

#resetObject

Empty the cache



44
45
46
# File 'lib/itly/plugin/testing/testing.rb', line 44

def reset
  @calls.clear
end