Class: ForestLiana::IntercomConversationsGetter

Inherits:
IntegrationBaseGetter 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
# File 'app/services/forest_liana/intercom_conversations_getter.rb', line 3

def initialize(params)
  @params = params
  @access_token = ForestLiana.integrations[:intercom][:access_token]
  @intercom = ::Intercom::Client.new(token: @access_token)
end

Instance Method Details

#countObject



9
10
11
# File 'app/services/forest_liana/intercom_conversations_getter.rb', line 9

def count
  @records.count
end

#performObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/services/forest_liana/intercom_conversations_getter.rb', line 23

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 = []
  rescue Intercom::UnexpectedError => exception
    FOREST_REPORTER.report exception
    FOREST_LOGGER.error "Cannot retrieve the Intercom conversations: #{exception.message}"
    @records = []
  end
end

#recordsObject



13
14
15
16
17
18
19
20
21
# File 'app/services/forest_liana/intercom_conversations_getter.rb', line 13

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
  end
end