Class: MnoEnterprise::IntercomEventsListener
- Inherits:
-
Object
- Object
- MnoEnterprise::IntercomEventsListener
- Defined in:
- lib/mno_enterprise/intercom_events_listener.rb
Instance Attribute Summary collapse
-
#intercom ⇒ Object
Returns the value of attribute intercom.
Instance Method Summary collapse
- #info(key, current_user_id, description, subject_type, subject_id, metadata) ⇒ Object
-
#initialize ⇒ IntercomEventsListener
constructor
A new instance of IntercomEventsListener.
-
#tag_user(user) ⇒ Object
If a source is set, tag the user with it.
- #update_intercom_user(user, update_last_request_at = true) ⇒ Object
Constructor Details
#initialize ⇒ IntercomEventsListener
Returns a new instance of IntercomEventsListener.
9 10 11 12 13 14 15 16 |
# File 'lib/mno_enterprise/intercom_events_listener.rb', line 9 def initialize args = if MnoEnterprise.intercom_token {token: MnoEnterprise.intercom_token} else {app_id: MnoEnterprise.intercom_app_id, api_key: MnoEnterprise.intercom_api_key} end self.intercom = ::Intercom::Client.new(args) end |
Instance Attribute Details
#intercom ⇒ Object
Returns the value of attribute intercom.
7 8 9 |
# File 'lib/mno_enterprise/intercom_events_listener.rb', line 7 def intercom @intercom end |
Instance Method Details
#info(key, current_user_id, description, subject_type, subject_id, metadata) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/mno_enterprise/intercom_events_listener.rb', line 18 def info(key, current_user_id, description, subject_type, subject_id, ) u = User.find(current_user_id) begin intercom.users.find(user_id: current_user_id) rescue Intercom::ResourceNotFound self.update_intercom_user(u) end data = {created_at: Time.now.to_i, email: u.email, user_id: u.id, event_name: key.tr('_', '-')} case key when 'user_update', 'organization_update' self.update_intercom_user(u) # convert values to string data[:metadata] = Hash[ .collect {|k,v| [k, v.to_s] } ] when 'user_confirm' data[:event_name] = 'finished-sign-up' when 'dashboard_create' data[:event_name] = 'added-dashboard' when 'dashboard_delete' data[:event_name] = 'removed-dashboard' when 'widget_delete' data[:event_name] = 'removed-widget' when 'widget_create' data[:event_name] = 'added-widget' data[:metadata] = {widget: [:name]} when 'app_launch' data[:event_name] = 'launched-app-' + [:app_nid] when 'app_destroy' data[:event_name] = 'deleted-app-' + [:app_nid] data[:metadata] = {type: 'single', app_list: [:app_nid]} when 'app_add' data[:event_name] = 'added-app-' + [:app_nid] data[:metadata] = {type: 'single', app_list: [:app_nid]} end self.intercom.events.create(data) rescue Intercom::IntercomError => e Rails.logger.tagged('Intercom') { Rails.logger.warn 'Error while calling intercom ' + e.} end |
#tag_user(user) ⇒ Object
If a source is set, tag the user with it
89 90 91 92 93 |
# File 'lib/mno_enterprise/intercom_events_listener.rb', line 89 def tag_user(user) if user. && user.[:source].present? intercom..tag(name: user.[:source], users: [{user_id: user.id}]) end end |
#update_intercom_user(user, update_last_request_at = true) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/mno_enterprise/intercom_events_listener.rb', line 57 def update_intercom_user(user, update_last_request_at = true) data = { user_id: user.id, name: [user.name, user.surname].join(' '), email: user.email, created_at: user.created_at.to_i, last_seen_ip: user.last_sign_in_ip, custom_attributes: {}, update_last_request_at: update_last_request_at } data[:custom_attributes][:phone]= user.phone if user.phone data[:custom_attributes][:external_id]= user.external_id if user.external_id data[:companies] = user.organizations.map do |organization| { company_id: organization.id, name: organization.name, created_at: organization.created_at.to_i, custom_attributes: { industry: organization.industry, size: organization.size, credit_card_details: organization.credit_card?, app_count: organization.app_instances.count, app_list: organization.app_instances.map { |app| app.name }.to_sentence } } end intercom.users.create(data) tag_user(user) end |