Class: ForestLiana::IntercomConversationsGetter

Inherits:
Object
  • Object
show all
Defined in:
app/services/forest_liana/intercom_conversations_getter.rb

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ IntercomConversationsGetter

Returns a new instance of IntercomConversationsGetter.



3
4
5
6
7
8
9
# File 'app/services/forest_liana/intercom_conversations_getter.rb', line 3

def initialize(params)
  @params = params
  @app_id = ForestLiana.integrations[:intercom][:app_id]
  @api_key = ForestLiana.integrations[:intercom][:api_key]

  @intercom = ::Intercom::Client.new(app_id: @app_id, api_key: @api_key)
end

Instance Method Details

#countObject



11
12
13
# File 'app/services/forest_liana/intercom_conversations_getter.rb', line 11

def count
  @records.count
end

#performObject



28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/services/forest_liana/intercom_conversations_getter.rb', line 28

def perform
  begin
    resource = collection.find(@params[:id])
    @records = @intercom.conversations.find_all(
      email: resource.email,
      type: 'user',
      display_as: 'plaintext',
    ).entries
  rescue Intercom::ResourceNotFound
    @records = []
  end
end

#recordsObject



15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/services/forest_liana/intercom_conversations_getter.rb', line 15

def records
  @records[pagination].map do |conversation|
    if conversation.assignee.is_a?(::Intercom::Admin)
      admins = @intercom.admins.all.detect(id: conversation.assignee.id)
      conversation.assignee = admins.first
    end

    conversation.link = link(conversation)

    conversation
  end
end