multiwoven-integrations 0.1.37 → 0.1.38

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 13fe187a421657cfb45280d87ae9fdd88dfb57b684290aba50869fc73bbd19c2
4
- data.tar.gz: 8d08feb79f2798e6955c482af4ec2ecaccc35ff723d60191c6efcfc85f173972
3
+ metadata.gz: be7368d653a73843a7d204b4dd7793c2ce7d5ba7f7e1e3b49818e15aff85367a
4
+ data.tar.gz: 21c792560111def1edad270dfcba87627818fbf4763c159053abf0114438e035
5
5
  SHA512:
6
- metadata.gz: b828ceef6251ca0859da2c5fe74e763c2a3f366cd461839456b74d4791eec94e482d7cfa1673ff76511f3e48dc8e7d09a0b216ff2f8a47585ff69cb628745a96
7
- data.tar.gz: 639488c9e265492f3e98b69cfd5010998c569a99c70bb11f2cbfbed92499f7b4c2eacf4f2febe0698cafebc6d039f3e18834eb6e8d010ff137f9603ac060ce6e
6
+ metadata.gz: b294140a41825ef76941958c46ec86a976e9073813b8a641663d249d518dd77d208d1c229bc20b7e76336196aa8abef7504ee92ac8a4100681b76fef05a2251e
7
+ data.tar.gz: f72981b9d03124e508d4cc1087ef13b876ee731d037bfaf4c4142bbc13c9a3227f248b0bdf07579f95591549c0800ce6439fdcd94a36510537589186512f9018
@@ -0,0 +1,117 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "stringio"
4
+
5
+ module Multiwoven
6
+ module Integrations
7
+ module Destination
8
+ module SalesforceConsumerGoodsCloud
9
+ include Multiwoven::Integrations::Core
10
+
11
+ API_VERSION = "59.0"
12
+
13
+ class Client < DestinationConnector
14
+ prepend Multiwoven::Integrations::Core::RateLimiter
15
+ def check_connection(connection_config)
16
+ connection_config = connection_config.with_indifferent_access
17
+ initialize_client(connection_config)
18
+ authenticate_client
19
+ success_status
20
+ rescue StandardError => e
21
+ failure_status(e)
22
+ end
23
+
24
+ def discover(_connection_config = nil)
25
+ catalog = build_catalog(load_catalog)
26
+ catalog.to_multiwoven_message
27
+ rescue StandardError => e
28
+ handle_exception("SALESFORCE:CONSUMER:GOODS:ClOUD:DISCOVER:EXCEPTION", "error", e)
29
+ end
30
+
31
+ def write(sync_config, records, action = "create")
32
+ @action = sync_config.stream.action || action
33
+ initialize_client(sync_config.destination.connection_specification)
34
+ process_records(records, sync_config.stream)
35
+ rescue StandardError => e
36
+ handle_exception("SALESFORCE:CONSUMER:GOODS:ClOUD:WRITE:EXCEPTION", "error", e)
37
+ end
38
+
39
+ private
40
+
41
+ def initialize_client(config)
42
+ config = config.with_indifferent_access
43
+ @client = Restforce.new(oauth_token: config[:access_token],
44
+ refresh_token: config[:refresh_token],
45
+ instance_url: config[:instance_url],
46
+ client_id: config[:client_id],
47
+ client_secret: config[:client_secret],
48
+ authentication_callback: proc { |x| log_debug(x.to_s) },
49
+ api_version: API_VERSION)
50
+ end
51
+
52
+ def process_records(records, stream)
53
+ write_success = 0
54
+ write_failure = 0
55
+ properties = stream.json_schema[:properties]
56
+ records.each do |record_object|
57
+ record = extract_data(record_object, properties)
58
+ process_record(stream, record)
59
+ write_success += 1
60
+ rescue StandardError => e
61
+ handle_exception("SALESFORCE:CONSUMER:GOODS:ClOUD:WRITE:EXCEPTION", "error", e)
62
+ write_failure += 1
63
+ end
64
+ tracking_message(write_success, write_failure)
65
+ end
66
+
67
+ def process_record(stream, record)
68
+ send_data_to_salesforce(stream.name, record)
69
+ end
70
+
71
+ def send_data_to_salesforce(stream_name, record = {})
72
+ method_name = "#{@action}!"
73
+ args = build_args(@action, stream_name, record)
74
+ @client.send(method_name, *args)
75
+ end
76
+
77
+ def build_args(action, stream_name, record)
78
+ case action
79
+ when :upsert
80
+ [stream_name, record[:external_key], record]
81
+ when :destroy
82
+ [stream_name, record[:id]]
83
+ else
84
+ [stream_name, record]
85
+ end
86
+ end
87
+
88
+ def authenticate_client
89
+ @client.authenticate!
90
+ end
91
+
92
+ def success_status
93
+ ConnectionStatus.new(status: ConnectionStatusType["succeeded"]).to_multiwoven_message
94
+ end
95
+
96
+ def failure_status(error)
97
+ ConnectionStatus.new(status: ConnectionStatusType["failed"], message: error.message).to_multiwoven_message
98
+ end
99
+
100
+ def load_catalog
101
+ read_json(CATALOG_SPEC_PATH)
102
+ end
103
+
104
+ def tracking_message(success, failure)
105
+ Multiwoven::Integrations::Protocol::TrackingMessage.new(
106
+ success: success, failed: failure
107
+ ).to_multiwoven_message
108
+ end
109
+
110
+ def log_debug(message)
111
+ Multiwoven::Integrations::Service.logger.debug(message)
112
+ end
113
+ end
114
+ end
115
+ end
116
+ end
117
+ end