Class: Multiwoven::Integrations::Destination::FacebookCustomAudience::Client
- Inherits:
-
DestinationConnector
- Object
- DestinationConnector
- Multiwoven::Integrations::Destination::FacebookCustomAudience::Client
- Defined in:
- lib/multiwoven/integrations/destination/facebook_custom_audience/client.rb
Overview
rubocop:disable Metrics/ClassLength
Instance Method Summary collapse
- #ad_account_exists?(response, ad_account_id) ⇒ Boolean
- #auth_headers(access_token) ⇒ Object
- #check_connection(connection_config) ⇒ Object
- #discover(_connection_config = nil) ⇒ Object
- #extract_data(response) ⇒ Object
- #extract_schema_and_data(data) ⇒ Object
- #write(sync_config, records, _action = "insert") ⇒ Object
Instance Method Details
#ad_account_exists?(response, ad_account_id) ⇒ Boolean
111 112 113 114 115 |
# File 'lib/multiwoven/integrations/destination/facebook_custom_audience/client.rb', line 111 def ad_account_exists?(response, ad_account_id) return if extract_data(response).any? { |ad_account| ad_account["id"] == "act_#{ad_account_id}" } raise ArgumentError, "Ad account not found in business account" end |
#auth_headers(access_token) ⇒ Object
103 104 105 106 107 108 109 |
# File 'lib/multiwoven/integrations/destination/facebook_custom_audience/client.rb', line 103 def auth_headers(access_token) { "Accept" => "application/json", "Authorization" => "Bearer #{access_token}", "Content-Type" => "application/json" } end |
#check_connection(connection_config) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/multiwoven/integrations/destination/facebook_custom_audience/client.rb', line 7 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) ad_account_exists?(response, connection_config[:ad_account_id]) ConnectionStatus.new(status: ConnectionStatusType["succeeded"]). else ConnectionStatus.new(status: ConnectionStatusType["failed"]). end rescue StandardError => e ConnectionStatus.new(status: ConnectionStatusType["failed"], message: e.). end |
#discover(_connection_config = nil) ⇒ Object
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 |
# File 'lib/multiwoven/integrations/destination/facebook_custom_audience/client.rb', line 25 def discover(_connection_config = nil) catalog_json = read_json(CATALOG_SPEC_PATH) streams = catalog_json["streams"].map do |stream| Multiwoven::Integrations::Protocol::Stream.new( audience_id: stream["audience_id"], url: stream["url"], name: stream["name"], json_schema: stream["json_schema"], request_method: stream["method"], action: stream["action"] ) end catalog = Multiwoven::Integrations::Protocol::Catalog.new( streams: streams ) catalog. rescue StandardError => e handle_exception( "FACEBOOK AUDIENCE:DISCOVER:EXCEPTION", "error", e ) end |
#extract_data(response) ⇒ Object
117 118 119 120 |
# File 'lib/multiwoven/integrations/destination/facebook_custom_audience/client.rb', line 117 def extract_data(response) response_body = response.body JSON.parse(response_body)["data"] if response_body end |
#extract_schema_and_data(data) ⇒ Object
99 100 101 |
# File 'lib/multiwoven/integrations/destination/facebook_custom_audience/client.rb', line 99 def extract_schema_and_data(data) [data.keys, data.values] end |
#write(sync_config, records, _action = "insert") ⇒ Object
52 53 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 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/multiwoven/integrations/destination/facebook_custom_audience/client.rb', line 52 def write(sync_config, records, _action = "insert") url = sync_config.stream.url connection_config = sync_config.destination.connection_specification.with_indifferent_access connection_config = connection_config.with_indifferent_access access_token = connection_config[:access_token] write_success = 0 write_failure = 0 records.each do |record| schema, data = extract_schema_and_data(record.with_indifferent_access[:data]) payload = { "payload" => { "schema" => schema, "data" => [data] } } response = Multiwoven::Integrations::Core::HttpClient.request( url, sync_config.stream.request_method, payload: payload, headers: auth_headers(access_token) ) if success?(response) write_success += 1 else write_failure += 1 end rescue StandardError => e logger.error( "FACEBOOK:RECORD:WRITE:FAILURE: #{e.}" ) write_failure += 1 end tracker = Multiwoven::Integrations::Protocol::TrackingMessage.new( success: write_success, failed: write_failure ) tracker. rescue StandardError => e # TODO: Handle rate limiting seperately handle_exception( "FACEBOOK:WRITE:EXCEPTION", "error", e ) end |