multiwoven-integrations 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +3 -0
  3. data/.rubocop.yml +34 -0
  4. data/.ruby-version +1 -0
  5. data/CHANGELOG.md +5 -0
  6. data/CODE_OF_CONDUCT.md +84 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +43 -0
  9. data/Rakefile +12 -0
  10. data/lib/multiwoven/integrations/config.rb +13 -0
  11. data/lib/multiwoven/integrations/core/base_connector.rb +44 -0
  12. data/lib/multiwoven/integrations/core/constants.rb +34 -0
  13. data/lib/multiwoven/integrations/core/destination_connector.rb +12 -0
  14. data/lib/multiwoven/integrations/core/http_client.rb +34 -0
  15. data/lib/multiwoven/integrations/core/source_connector.rb +12 -0
  16. data/lib/multiwoven/integrations/core/utils.rb +68 -0
  17. data/lib/multiwoven/integrations/destination/klaviyo/client.rb +119 -0
  18. data/lib/multiwoven/integrations/destination/klaviyo/config/catalog.json +124 -0
  19. data/lib/multiwoven/integrations/destination/klaviyo/config/meta.json +15 -0
  20. data/lib/multiwoven/integrations/destination/klaviyo/config/spec.json +22 -0
  21. data/lib/multiwoven/integrations/protocol/protocol.json +189 -0
  22. data/lib/multiwoven/integrations/protocol/protocol.rb +179 -0
  23. data/lib/multiwoven/integrations/rollout.rb +17 -0
  24. data/lib/multiwoven/integrations/service.rb +57 -0
  25. data/lib/multiwoven/integrations/source/bigquery/client.rb +86 -0
  26. data/lib/multiwoven/integrations/source/bigquery/config/meta.json +15 -0
  27. data/lib/multiwoven/integrations/source/bigquery/config/spec.json +28 -0
  28. data/lib/multiwoven/integrations/source/redshift/client.rb +100 -0
  29. data/lib/multiwoven/integrations/source/redshift/config/meta.json +15 -0
  30. data/lib/multiwoven/integrations/source/redshift/config/spec.json +74 -0
  31. data/lib/multiwoven/integrations/source/snowflake/client.rb +84 -0
  32. data/lib/multiwoven/integrations/source/snowflake/config/meta.json +15 -0
  33. data/lib/multiwoven/integrations/source/snowflake/config/spec.json +87 -0
  34. data/lib/multiwoven/integrations.rb +41 -0
  35. data/multiwoven-integrations.gemspec +54 -0
  36. data/sig/multiwoven/integrations.rbs +6 -0
  37. metadata +291 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0405b7b4bcdbb582f2ead9390d6f6355c61beed39ab7de49f8b360d301b2dba1
4
+ data.tar.gz: ca128ddab86cde7520c7a2eeff0fdb2cfbfed19cc6ce58139766d36167311ac3
5
+ SHA512:
6
+ metadata.gz: ee4413e21079c1db27b45ba01cc2e8e139f2b020b9d10b8c632187e75e561def707ec7beb6c1c99159984298774969de5c103b56ebd1ca480329f469f18507b1
7
+ data.tar.gz: 52b7e3969cf10815d3acf88f09437f266678eccff09a9b523a3071adb879460ecf3369f4fd6a7f0b7a9f07b7f9c681099a515d6919a11dfa5a7ad55bcea1ea8e
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,34 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 300
14
+
15
+ Style/ClassAndModuleChildren:
16
+ Enabled: false
17
+
18
+ Style/Documentation:
19
+ Enabled: false
20
+
21
+ Metrics/ModuleLength:
22
+ Max: 500
23
+
24
+ Metrics/BlockLength:
25
+ Max: 120
26
+
27
+ Metrics/AbcSize:
28
+ Max: 30
29
+
30
+ Metrics/MethodLength:
31
+ Max: 50
32
+
33
+ Metrics/CyclomaticComplexity:
34
+ Max: 50
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.2.0
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-12-01
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at subinthattaparambil@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Multiwoven
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # Multiwoven Integrations
2
+
3
+ [![CI](https://github.com/Multiwoven/multiwoven-integrations/actions/workflows/ci.yml/badge.svg)](https://github.com/Multiwoven/multiwoven-integrations/actions/workflows/ci.yml)
4
+ [![Maintainability](https://api.codeclimate.com/v1/badges/d841270f1f7a966043c1/maintainability)](https://codeclimate.com/repos/657d0a2a60265a2f2155ffca/maintainability)
5
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/d841270f1f7a966043c1/test_coverage)](https://codeclimate.com/repos/657d0a2a60265a2f2155ffca/test_coverage)
6
+
7
+ TODO: Delete this and the text below, and describe your gem
8
+
9
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/multiwoven/integrations`. To experiment with that code, run `bin/console` for an interactive prompt.
10
+
11
+ ## Installation
12
+
13
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
14
+
15
+ Install the gem and add to the application's Gemfile by executing:
16
+
17
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
18
+
19
+ If bundler is not being used to manage dependencies, install the gem by executing:
20
+
21
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/multiwoven-integrations. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/multiwoven-integrations/blob/main/CODE_OF_CONDUCT.md).
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the Multiwoven::Integrations project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/multiwoven-integrations/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Multiwoven
4
+ module Integrations
5
+ class Config
6
+ attr_accessor :logger
7
+
8
+ def initialize(params = {})
9
+ @logger = params[:logger]
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Multiwoven
4
+ module Integrations::Core
5
+ class BaseConnector
6
+ include Integrations::Protocol
7
+ include Utils
8
+ include Constants
9
+
10
+ def connector_spec
11
+ @connector_spec ||= begin
12
+ spec_json = keys_to_symbols(read_json(CONNECTOR_SPEC_PATH)).to_json
13
+ ConnectorSpecification.from_json(spec_json)
14
+ end
15
+ end
16
+
17
+ def meta_data
18
+ @meta_data ||= read_json(META_DATA_PATH).to_h
19
+ end
20
+
21
+ def check_connection(_connection_config)
22
+ raise "Not implemented"
23
+ # returns Protocol.ConnectionStatus
24
+ end
25
+
26
+ def discover(_connection_config)
27
+ raise "Not implemented"
28
+ end
29
+
30
+ private
31
+
32
+ def read_json(file_path)
33
+ # TODO: Rethink. Hack to find connector folder
34
+ connector_folder = self.class.to_s.split("::")[2..3].map(&:downcase).join("/")
35
+ file_path = File.join(
36
+ INTEGRATIONS_PATH, "#{connector_folder}/",
37
+ file_path
38
+ )
39
+ file_contents = File.read(file_path)
40
+ JSON.parse(file_contents)
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Multiwoven
4
+ module Integrations::Core
5
+ module Constants
6
+ # CONFIG
7
+ INTEGRATIONS_PATH = File.join(
8
+ Gem.loaded_specs["multiwoven-integrations"].full_gem_path,
9
+ "/lib/multiwoven/integrations"
10
+ )
11
+ META_DATA_PATH = "config/meta.json"
12
+ CONNECTOR_SPEC_PATH = "config/spec.json"
13
+ CATALOG_SPEC_PATH = "config/catalog.json"
14
+ SNOWFLAKE_DRIVER_PATH = "/opt/snowflake/snowflakeodbc/lib/universal/libSnowflake.dylib"
15
+
16
+ # CONNECTORS
17
+ KLAVIYO_AUTH_ENDPOINT = "https://a.klaviyo.com/api/lists/"
18
+ KLAVIYO_AUTH_PAYLOAD = {
19
+ data: {
20
+ type: "list",
21
+ attributes: {
22
+ name: "THIS IS REQUIRED"
23
+ }
24
+ }
25
+ }.freeze
26
+
27
+ # HTTP
28
+ HTTP_GET = "GET"
29
+ HTTP_POST = "POST"
30
+ HTTP_PUT = "PUT"
31
+ HTTP_DELETE = "DELETE"
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Multiwoven
4
+ module Integrations::Core
5
+ class DestinationConnector < BaseConnector
6
+ def write(_sync_config, _records, _action = "insert")
7
+ raise "Not implemented"
8
+ # return list of record message
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Multiwoven
4
+ module Integrations::Core
5
+ class HttpClient
6
+ class << self
7
+ def request(url, method, payload: nil, headers: {})
8
+ uri = URI(url)
9
+ http = Net::HTTP.new(uri.host, uri.port)
10
+ http.use_ssl = (uri.scheme == "https")
11
+ request = build_request(method, uri, payload, headers)
12
+ http.request(request)
13
+ end
14
+
15
+ private
16
+
17
+ def build_request(method, uri, payload, headers)
18
+ request_class = case method.upcase
19
+ when Constants::HTTP_GET then Net::HTTP::Get
20
+ when Constants::HTTP_POST then Net::HTTP::Post
21
+ when Constants::HTTP_PUT then Net::HTTP::Put
22
+ when Constants::HTTP_DELETE then Net::HTTP::Delete
23
+ else raise ArgumentError, "Unsupported HTTP method: #{method}"
24
+ end
25
+
26
+ request = request_class.new(uri)
27
+ headers.each { |key, value| request[key] = value }
28
+ request.body = payload.to_json if payload && %w[POST PUT].include?(method.upcase)
29
+ request
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Multiwoven
4
+ module Integrations::Core
5
+ class SourceConnector < BaseConnector
6
+ def read(_sync_config)
7
+ raise "Not implemented"
8
+ # return list of RecordMessage
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Multiwoven
4
+ module Integrations::Core
5
+ module Utils
6
+ def keys_to_symbols(hash)
7
+ if hash.is_a?(Hash)
8
+ hash.each_with_object({}) do |(key, value), result|
9
+ result[key.to_sym] = keys_to_symbols(value)
10
+ end
11
+ elsif hash.is_a?(Array)
12
+ hash.map { |item| keys_to_symbols(item) }
13
+ else
14
+ hash
15
+ end
16
+ end
17
+
18
+ def convert_to_json_schema(column_definitions)
19
+ json_schema = {
20
+ "type" => "object",
21
+ "properties" => {}
22
+ }
23
+
24
+ column_definitions.each do |column|
25
+ column_name = column[:column_name]
26
+ type = column[:type]
27
+ optional = column[:optional]
28
+ json_type = map_type_to_json_schema(type)
29
+ json_schema["properties"][column_name] = {
30
+ "type" => json_type
31
+ }
32
+ json_schema["properties"][column_name]["type"] = [json_type, "null"] if optional
33
+ end
34
+
35
+ json_schema
36
+ end
37
+
38
+ def map_type_to_json_schema(type)
39
+ case type
40
+ when "NUMBER"
41
+ "integer"
42
+ else
43
+ "string" # Default type
44
+ end
45
+ end
46
+
47
+ def logger
48
+ Integrations::Service.logger
49
+ end
50
+
51
+ def create_log_message(context, type, exception)
52
+ Integrations::Protocol::LogMessage.new(
53
+ name: context,
54
+ level: type,
55
+ message: exception.message
56
+ ).to_multiwoven_message
57
+ end
58
+
59
+ def handle_exception(context, type, exception)
60
+ logger.error(
61
+ "#{context}: #{exception.message}"
62
+ )
63
+
64
+ create_log_message(context, type, exception)
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,119 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Multiwoven::Integrations::Destination
4
+ module Klaviyo
5
+ include Multiwoven::Integrations::Core
6
+ class Client < DestinationConnector
7
+ def check_connection(connection_config)
8
+ api_key = connection_config[:private_api_key]
9
+
10
+ response = Multiwoven::Integrations::Core::HttpClient.request(
11
+ KLAVIYO_AUTH_ENDPOINT,
12
+ HTTP_POST,
13
+ payload: KLAVIYO_AUTH_PAYLOAD,
14
+ headers: auth_headers(api_key)
15
+ )
16
+ parse_response(response)
17
+ end
18
+
19
+ def discover
20
+ catalog_json = read_json(CATALOG_SPEC_PATH)
21
+
22
+ streams = catalog_json["streams"].map do |stream|
23
+ Multiwoven::Integrations::Protocol::Stream.new(
24
+ name: stream["name"],
25
+ json_schema: stream["json_schema"],
26
+ url: stream["url"],
27
+ method: stream["method"],
28
+ action: stream["action"]
29
+ )
30
+ end
31
+
32
+ catalog = Multiwoven::Integrations::Protocol::Catalog.new(
33
+ streams: streams
34
+ )
35
+
36
+ catalog.to_multiwoven_message
37
+ rescue StandardError => e
38
+ handle_exception(
39
+ "KLAVIYO:DISCOVER:EXCEPTION",
40
+ "error",
41
+ e
42
+ )
43
+ end
44
+
45
+ def write(sync_config, records, _action = "insert")
46
+ connection_config = sync_config.destination.connection_specification.with_indifferent_access
47
+ url = sync_config.stream.url
48
+ request_method = sync_config.stream.request_method
49
+
50
+ write_success = 0
51
+ write_failure = 0
52
+ records.each do |record|
53
+ response = Multiwoven::Integrations::Core::HttpClient.request(
54
+ url,
55
+ request_method,
56
+ payload: record,
57
+ headers: auth_headers(connection_config["private_api_key"])
58
+ )
59
+ if success?(response)
60
+ write_success += 1
61
+ else
62
+ write_failure += 1
63
+ end
64
+ rescue StandardError => e
65
+ logger.error(
66
+ "KLAVIYO:RECORD:WRITE:FAILURE: #{e.message}"
67
+ )
68
+ write_failure += 1
69
+ end
70
+ tracker = Multiwoven::Integrations::Protocol::TrackingMessage.new(
71
+ success: write_success,
72
+ failed: write_failure
73
+ )
74
+ tracker.to_multiwoven_message
75
+ rescue StandardError => e
76
+ # TODO: Handle rate limiting seperately
77
+ handle_exception(
78
+ "KLAVIYO:WRITE:EXCEPTION",
79
+ "error",
80
+ e
81
+ )
82
+ end
83
+
84
+ private
85
+
86
+ def parse_response(response)
87
+ if success?(response)
88
+ ConnectionStatus.new(
89
+ status: ConnectionStatusType["succeeded"]
90
+ ).to_multiwoven_message
91
+ else
92
+ message = extract_message(response)
93
+ ConnectionStatus.new(
94
+ status: ConnectionStatusType["failed"], message: message
95
+ ).to_multiwoven_message
96
+ end
97
+ end
98
+
99
+ def success?(response)
100
+ response && %w[200 201].include?(response.code)
101
+ end
102
+
103
+ def extract_message(response)
104
+ JSON.parse(response.body)["message"]
105
+ rescue StandardError => e
106
+ "Klaviyo auth failed: #{e.message}"
107
+ end
108
+
109
+ def auth_headers(api_key)
110
+ {
111
+ "Accept" => "application/json",
112
+ "Authorization" => "Klaviyo-API-Key #{api_key}",
113
+ "Revision" => "2023-02-22",
114
+ "Content-Type" => "application/json"
115
+ }
116
+ end
117
+ end
118
+ end
119
+ end