Class: AmplitudeAPI::Event
- Inherits:
-
Object
- Object
- AmplitudeAPI::Event
- Defined in:
- lib/amplitude_api/event.rb
Overview
AmplitudeAPI::Event
Instance Method Summary collapse
-
#==(other) ⇒ true, false
Compares +to_hash+ for equality.
- #create_getter(attribute_name) ⇒ Object
- #create_setter(attribute_name) ⇒ Object
-
#extra_properties ⇒ Hash
Returns optional properties (not belong to the API, are assigned by the user) This way, if the API is updated with new properties, the gem will be able to work with the new specification until the code is modified.
-
#initialize(attributes = {}) ⇒ Event
constructor
Create a new Event.
- #method_missing(method_name, *args) ⇒ Object
-
#optional_properties ⇒ Hash
Returns optional properties (belong to the API but are optional).
-
#reserved_event?(type) ⇒ true, false
Returns true if the event type matches one reserved by Amplitude API.
- #respond_to_missing?(method_name, *args) ⇒ Boolean
-
#to_hash ⇒ Hash
(also: #to_h)
Used for serialization and comparison.
- #user_id=(value) ⇒ Object
Constructor Details
#initialize(attributes = {}) ⇒ Event
Create a new Event
See (Amplitude HTTP API Documentation)[https://developers.amplitude.com/docs/http-api-v2] for a list of valid parameters and their types.
18 19 20 21 22 23 24 |
# File 'lib/amplitude_api/event.rb', line 18 def initialize(attributes = {}) attributes.each do |k, v| send("#{k}=", v) if respond_to?("#{k}=") end validate_arguments @extra_properties = [] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/amplitude_api/event.rb', line 26 def method_missing(method_name, *args) super if block_given? property_name = method_name.to_s.delete_suffix("=") @extra_properties << property_name create_setter property_name create_getter property_name send("#{property_name}=".to_sym, *args) end |
Instance Method Details
#==(other) ⇒ true, false
Compares +to_hash+ for equality
116 117 118 119 120 |
# File 'lib/amplitude_api/event.rb', line 116 def ==(other) return false unless other.respond_to?(:to_h) to_h == other.to_h end |
#create_getter(attribute_name) ⇒ Object
45 46 47 48 49 |
# File 'lib/amplitude_api/event.rb', line 45 def create_getter(attribute_name) self.class.send(:define_method, attribute_name.to_sym) do instance_variable_get("@" + attribute_name.to_s) end end |
#create_setter(attribute_name) ⇒ Object
39 40 41 42 43 |
# File 'lib/amplitude_api/event.rb', line 39 def create_setter(attribute_name) self.class.send(:define_method, "#{attribute_name}=".to_sym) do |value| instance_variable_set("@" + attribute_name.to_s, value) end end |
#extra_properties ⇒ Hash
Returns optional properties (not belong to the API, are assigned by the user) This way, if the API is updated with new properties, the gem will be able to work with the new specification until the code is modified
94 95 96 97 98 99 |
# File 'lib/amplitude_api/event.rb', line 94 def extra_properties @extra_properties.map do |prop| val = send(prop) val ? [prop.to_sym, val] : nil end.compact.to_h end |
#optional_properties ⇒ Hash
Returns optional properties (belong to the API but are optional)
82 83 84 85 86 87 |
# File 'lib/amplitude_api/event.rb', line 82 def optional_properties AmplitudeAPI::Config.optional_properties.map do |prop| val = prop == :time ? formatted_time : send(prop) val ? [prop, val] : nil end.compact.to_h end |
#reserved_event?(type) ⇒ true, false
Returns true if the event type matches one reserved by Amplitude API.
104 105 106 107 108 109 110 111 |
# File 'lib/amplitude_api/event.rb', line 104 def reserved_event?(type) ["[Amplitude] Start Session", "[Amplitude] End Session", "[Amplitude] Revenue", "[Amplitude] Revenue (Verified)", "[Amplitude] Revenue (Unverified)", "[Amplitude] Merged User"].include?(type) end |
#respond_to_missing?(method_name, *args) ⇒ Boolean
51 52 53 |
# File 'lib/amplitude_api/event.rb', line 51 def respond_to_missing?(method_name, *args) @extra_properties.include?(method_name) || @extra_properties.include?("#{method_name}=") || super end |
#to_hash ⇒ Hash Also known as: to_h
Used for serialization and comparison
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/amplitude_api/event.rb', line 67 def to_hash event = { event_type: event_type, event_properties: formatted_event_properties, user_properties: formatted_user_properties } event[:user_id] = user_id if user_id event[:device_id] = device_id if device_id event.merge(optional_properties).merge(revenue_hash).merge(extra_properties) end |
#user_id=(value) ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/amplitude_api/event.rb', line 55 def user_id=(value) @user_id = if value.respond_to?(:id) value.id else value || AmplitudeAPI::USER_WITH_NO_ACCOUNT end end |