class Multiwoven::Integrations::Destination::Stripe::Client

def authenticate_client

def authenticate_client
  @client::Customer.list
end

def check_connection(connection_config)

def check_connection(connection_config)
  connection_config = connection_config.with_indifferent_access
  initialize_client(connection_config)
  authenticate_client
  success_status
rescue StandardError => e
  failure_status(e)
end

def discover(_connection_config = nil)

def discover(_connection_config = nil)
  catalog = build_catalog(load_catalog)
  catalog.to_multiwoven_message
rescue StandardError => e
  handle_exception(e, {
                     context: "STRIPE:CRM:DISCOVER:EXCEPTION",
                     type: "error"
                   })
end

def initialize_client(config)

def initialize_client(config)
  config = config.with_indifferent_access
  ::Stripe.api_key = config[:api_key]
  @client = ::Stripe
end

def load_catalog

def load_catalog
  read_json(CATALOG_SPEC_PATH)
end

def log_debug(message)

def log_debug(message)
  Multiwoven::Integrations::Service.logger.debug(message)
end

def process_records(records, stream)

def process_records(records, stream)
  write_success = 0
  write_failure = 0
  properties = stream.json_schema[:properties]
  records.each do |record_object|
    record = extract_data(record_object, properties)
    klass  = @client.const_get(stream.name)
    klass.send(@action, record)
    write_success += 1
  rescue StandardError => e
    handle_exception(e, {
                       context: "STRIPE:CRM:WRITE:EXCEPTION",
                       type: "error",
                       sync_id: @sync_config.sync_id,
                       sync_run_id: @sync_config.sync_run_id
                     })
    write_failure += 1
  end
  tracking_message(write_success, write_failure)
end

def tracking_message(success, failure)

def tracking_message(success, failure)
  Multiwoven::Integrations::Protocol::TrackingMessage.new(
    success: success, failed: failure
  ).to_multiwoven_message
end

def write(sync_config, records, action = "create")

def write(sync_config, records, action = "create")
  @sync_config = sync_config
  @action = sync_config.stream.action || action
  initialize_client(sync_config.destination.connection_specification)
  process_records(records, sync_config.stream)
rescue StandardError => e
  handle_exception(e, {
                     context: "STRIPE:CRM:WRITE:EXCEPTION",
                     type: "error",
                     sync_id: @sync_config.sync_id,
                     sync_run_id: @sync_config.sync_run_id
                   })
end