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 = {}) @extra_properties = [] attributes.each do |k, v| send("#{k}=", v) end validate_arguments 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 38 |
# File 'lib/amplitude_api/event.rb', line 26 def method_missing(method_name, *args) super if block_given? super unless method_name.to_s.end_with? "=" 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
117 118 119 120 121 |
# File 'lib/amplitude_api/event.rb', line 117 def ==(other) return false unless other.respond_to?(:to_h) to_h == other.to_h end |
#create_getter(attribute_name) ⇒ Object
46 47 48 49 50 |
# File 'lib/amplitude_api/event.rb', line 46 def create_getter(attribute_name) self.class.send(:define_method, attribute_name.to_sym) do instance_variable_get("@#{attribute_name}") end end |
#create_setter(attribute_name) ⇒ Object
40 41 42 43 44 |
# File 'lib/amplitude_api/event.rb', line 40 def create_setter(attribute_name) self.class.send(:define_method, "#{attribute_name}=".to_sym) do |value| instance_variable_set("@#{attribute_name}", 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
95 96 97 98 99 100 |
# File 'lib/amplitude_api/event.rb', line 95 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)
83 84 85 86 87 88 |
# File 'lib/amplitude_api/event.rb', line 83 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.
105 106 107 108 109 110 111 112 |
# File 'lib/amplitude_api/event.rb', line 105 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
52 53 54 |
# File 'lib/amplitude_api/event.rb', line 52 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
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/amplitude_api/event.rb', line 68 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
56 57 58 59 60 61 62 63 |
# File 'lib/amplitude_api/event.rb', line 56 def user_id=(value) @user_id = if value.respond_to?(:id) value.id else value || AmplitudeAPI::USER_WITH_NO_ACCOUNT end end |