Class: Phone::CallRailImporter

Inherits:
BaseService
  • Object
show all
Defined in:
app/services/phone/call_rail_importer.rb

Constant Summary collapse

CALLRAIL_SOURCE_ID =
4661

Instance Method Summary collapse

Instance Method Details

#build_callrail_call(call_detail_record) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/services/phone/call_rail_importer.rb', line 27

def build_callrail_call(call_detail_record)
  record = CallRailData.new(
    callrail_id: call_detail_record[:id],
    answered: call_detail_record[:answered],
    customer_number: PhoneNumber.parse_and_format(call_detail_record[:customer_phone_number]),
    customer_city: call_detail_record[:customer_city],
    customer_country: call_detail_record[:customer_country],
    customer_name: call_detail_record[:customer_name],
    customer_state: call_detail_record[:customer_state],
    tracking_phone_number: PhoneNumber.parse_and_format(call_detail_record[:tracking_phone_number]),
    start_time: call_detail_record[:start_time],
    campaign: call_detail_record[:campaign],
    keywords: call_detail_record[:keywords],
    gclid: call_detail_record[:gclid],
    landing_page_url: call_detail_record[:landing_page_url]
  )
end

#match_call_log_record(imported_calls) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'app/services/phone/call_rail_importer.rb', line 45

def match_call_log_record(imported_calls)
  imported_calls.each do |call_record|
    matching_calls = CallLog.where(from_number: call_record.customer_number).where(CallLog[:start_time].gt(call_record.start_time))
           .where(CallLog[:start_time].lt(call_record.start_time + 1.hour))
    if matching_calls.present?
      matching_calls.update_all(source_id: CALLRAIL_SOURCE_ID, campaign: call_record.campaign, callrail_id: call_record.id)
    end
  end
end

#processObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/services/phone/call_rail_importer.rb', line 4

def process
  call_summary = Api::Callrail.calls_summary(date_range = 'yesterday')
  call_summary.deep_symbolize_keys!
  total_number_of_calls = call_summary[:total_results][:total_calls]

  calls_to_import = []

  call_data = Api::Callrail.list_all_calls(total_number_of_calls, 1, 'yesterday')
  call_data.deep_symbolize_keys!
  calls = call_data[:calls]

  calls.each do |call|
    calls_to_import << build_callrail_call(call)
  end

  require 'activerecord-import/base'
  require 'activerecord-import/active_record/adapters/postgresql_adapter'
  imported_calls = CallRailData.import calls_to_import, on_duplicate_key_ignore: true

  match_call_log_record(imported_calls.results)
end