Class: Multiwoven::Integrations::Destination::FacebookCustomAudience::Client

Inherits:
DestinationConnector
  • Object
show all
Defined in:
lib/multiwoven/integrations/destination/facebook_custom_audience/client.rb

Overview

rubocop:disable Metrics/ClassLength

Constant Summary collapse

MAX_CHUNK_SIZE =
10_000

Instance Method Summary collapse

Instance Method Details

#check_connection(connection_config) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/multiwoven/integrations/destination/facebook_custom_audience/client.rb', line 8

def check_connection(connection_config)
  connection_config = connection_config.with_indifferent_access
  access_token = connection_config[:access_token]
  response = Multiwoven::Integrations::Core::HttpClient.request(
    FACEBOOK_AUDIENCE_GET_ALL_ACCOUNTS,
    HTTP_GET,
    headers: auth_headers(access_token)
  )
  if success?(response)
    (response, connection_config[:ad_account_id])
    ConnectionStatus.new(status: ConnectionStatusType["succeeded"]).to_multiwoven_message
  else
    ConnectionStatus.new(status: ConnectionStatusType["failed"]).to_multiwoven_message
  end
rescue StandardError => e
  ConnectionStatus.new(status: ConnectionStatusType["failed"], message: e.message).to_multiwoven_message
end

#discover(_connection_config = nil) ⇒ Object



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
# File 'lib/multiwoven/integrations/destination/facebook_custom_audience/client.rb', line 26

def discover(_connection_config = nil)
  catalog_json = read_json(CATALOG_SPEC_PATH)

  streams = catalog_json["streams"].map do |stream|
    Multiwoven::Integrations::Protocol::Stream.new(
      url: stream["url"],
      name: stream["name"],
      json_schema: stream["json_schema"],
      request_method: stream["method"],
      action: stream["action"],
      batch_support: stream["batch_support"],
      batch_size: stream["batch_size"]
    )
  end

  catalog = Multiwoven::Integrations::Protocol::Catalog.new(
    streams: streams
  )

  catalog.to_multiwoven_message
rescue StandardError => e
  handle_exception(
    "FACEBOOK AUDIENCE:DISCOVER:EXCEPTION",
    "error",
    e
  )
end

#write(sync_config, records, _action = "insert") ⇒ Object



54
55
56
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
# File 'lib/multiwoven/integrations/destination/facebook_custom_audience/client.rb', line 54

def write(sync_config, records, _action = "insert")
  connection_config = sync_config.destination.connection_specification.with_indifferent_access
  access_token = connection_config[:access_token]
  url = generate_url(sync_config, connection_config)
  write_success = 0
  write_failure = 0
  records.each_slice(MAX_CHUNK_SIZE) do |chunk|
    payload = create_payload(chunk, sync_config.stream.json_schema.with_indifferent_access)
    response = Multiwoven::Integrations::Core::HttpClient.request(
      url,
      sync_config.stream.request_method,
      payload: payload,
      headers: auth_headers(access_token)
    )
    if success?(response)
      write_success += chunk.size
    else
      write_failure += chunk.size
    end
  rescue StandardError => e
    handle_exception("FACEBOOK:RECORD:WRITE:EXCEPTION", "error", e)
    write_failure += chunk.size
  end

  tracker = Multiwoven::Integrations::Protocol::TrackingMessage.new(
    success: write_success,
    failed: write_failure
  )
  tracker.to_multiwoven_message
rescue StandardError => e
  handle_exception("FACEBOOK:WRITE:EXCEPTION", "error", e)
end