<commit_before>require 'sinatra'
require 'endpoint_base'
require 'salesforce_integration'

class SalesforceEndpoint < EndpointBase::Sinatra::Base
  endpoint_key App.endpoint_key
  enable :logging

  def report_error(error)
    return unless App.env == 'production'
    logger.info request
    logger.info @payload
    Rollbar.report_exception(error)
  end

  ['/add_order', '/update_order'].each do |path|
    post path do
      begin
        SpreeService::Order.new(@payload, @config).upsert_contact_with_account!
        SpreeService::Order.new(@payload, @config).upsert_order!
        SpreeService::Order.new(@payload, @config).upsert_payments!
        set_summary "Successfully upserted contact for #{@payload["order"]["email"]} and order ##{@payload["order"]["id"]}"
        result 200
      rescue Exception => e
        report_error(e)
        result 500
      end
    end
  end

  post '/cancel_order' do
    begin
      # the real @payload will be provided by the HUB later
      @payload = JSON.parse IO.read("#{File.dirname(__FILE__)}/spec/support/factories/cancel_order.json")
      SpreeService::Order.new(@payload, @config).upsert_order!
      set_summary "Successfully upserted order ##{@payload["order"]["id"]}"
      result 200
    rescue Exception => e
      report_error(e)
      result 500
    end
  end

  post '/return_order' do
    begin
      # the real @payload will be provided by the HUB later
      @payload = JSON.parse IO.read("#{File.dirname(__FILE__)}/spec/support/factories/return_order.json")
      SpreeService::Order.new(@payload, @config).upsert_order!
      set_summary "Successfully upserted order ##{@payload["order"]["id"]}"
      result 200
    rescue Exception => e
      report_error(e)
      result 500
    end
  end

  ['/add_customer', '/update_customer'].each do |path|
    post path do
      begin
        SpreeService::Customer.new(@payload, @config).upsert_contact_with_account!
        set_summary "Successfully upserted contact for #{@payload["customer"]["email"]}"
        result 200
      rescue Exception => e
        report_error(e)
        result 500
      end
    end
  end

  ['/add_product', '/update_product'].each do |path|
    post path do
      begin
        SpreeService::Product.new(@payload, @config).upsert_product!
        set_summary "Successfully upserted product for #{@payload["product"]["sku"]}"
        result 200
      rescue Exception => e
        report_error(e)
        result 500
      end
    end
  end

  post '/import_products' do
    begin
      # the real @config and @payload will be provided by the HUB later
      # test_config_hash = {
      #   'salesforce_username' => '<EMAIL>',
      #   'salesforce_password' => '<PASSWORD>',
      #   'salesforce_security_token' => '<PASSWORD>',
      #   'salesforce_client_id' => '<KEY>',
      #   'salesforce_client_secret' => '3920716088724079571'
      # }.with_indifferent_access
      # @config = test_config_hash
      @payload = JSON.parse IO.read("#{File.dirname(__FILE__)}/spec/support/factories/import_products.json")
      batch = SpreeService::Product.new(@payload, @config).import_products!
      status = batch.status
      failed = status[:number_records_failed].to_i
      all = status[:number_records_processed].to_i
      successed = all - failed
      set_summary "#{successed}/#{all} products successfully upserted."
      result 200
    rescue Exception => e
      report_error(e)
      result 500
    end
  end
end
<commit_msg>endpoint update<commit_after>require 'sinatra'
require 'endpoint_base'
require 'salesforce_integration'

class SalesforceEndpoint < EndpointBase::Sinatra::Base
  endpoint_key App.endpoint_key
  enable :logging

  def report_error(error)
    return unless App.env == 'production'
    logger.info request
    logger.info @payload
    Rollbar.report_exception(error)
  end

  ['/add_order', '/update_order'].each do |path|
    post path do
      begin
        SpreeService::Order.new(@payload, @config).upsert_contact_with_account!
        SpreeService::Order.new(@payload, @config).upsert_order!
        SpreeService::Product.new(@payload, @config).upsert_products!
        SpreeService::Order.new(@payload, @config).upsert_lineitems!
        SpreeService::Order.new(@payload, @config).upsert_payments!
        set_summary "Successfully upserted contact for #{@payload["order"]["email"]} and order ##{@payload["order"]["id"]}"
        result 200
      rescue Exception => e
        report_error(e)
        result 500
      end
    end
  end

  post '/cancel_order' do
    begin
      # the real @payload will be provided by the HUB later
      @payload = JSON.parse IO.read("#{File.dirname(__FILE__)}/spec/support/factories/cancel_order.json")
      SpreeService::Order.new(@payload, @config).upsert_order!
      set_summary "Successfully upserted order ##{@payload["order"]["id"]}"
      result 200
    rescue Exception => e
      report_error(e)
      result 500
    end
  end

  post '/return_order' do
    begin
      # the real @payload will be provided by the HUB later
      @payload = JSON.parse IO.read("#{File.dirname(__FILE__)}/spec/support/factories/return_order.json")
      SpreeService::Order.new(@payload, @config).upsert_order!
      set_summary "Successfully upserted order ##{@payload["order"]["id"]}"
      result 200
    rescue Exception => e
      report_error(e)
      result 500
    end
  end

  ['/add_customer', '/update_customer'].each do |path|
    post path do
      begin
        SpreeService::Customer.new(@payload, @config).upsert_contact_with_account!
        set_summary "Successfully upserted contact for #{@payload["customer"]["email"]}"
        result 200
      rescue Exception => e
        report_error(e)
        result 500
      end
    end
  end

  ['/add_product', '/update_product'].each do |path|
    post path do
      begin
        SpreeService::Product.new(@payload, @config).upsert_product!
        set_summary "Successfully upserted product for #{@payload["product"]["sku"]}"
        result 200
      rescue Exception => e
        report_error(e)
        result 500
      end
    end
  end

  post '/import_products' do
    begin
      # the real @config and @payload will be provided by the HUB later
      # test_config_hash = {
      #   'salesforce_username' => '<EMAIL>',
      #   'salesforce_password' => '<PASSWORD>',
      #   'salesforce_security_token' => '<PASSWORD>',
      #   'salesforce_client_id' => '3MVG9WtWSKUDG.x5hyqXeboVoSErlfbiCvJNDfuwmN77rRhJ6tqCeFKFhuFvMNo0COBif7CT1NnevkMq464Qp',
      #   'salesforce_client_secret' => '3920716088724079571'
      # }.with_indifferent_access
      # @config = test_config_hash
      @payload = JSON.parse IO.read("#{File.dirname(__FILE__)}/spec/support/factories/import_products.json")
      batch = SpreeService::Product.new(@payload, @config).import_products!
      status = batch.status
      failed = status[:number_records_failed].to_i
      all = status[:number_records_processed].to_i
      successed = all - failed
      set_summary "#{successed}/#{all} products successfully upserted."
      result 200
    rescue Exception => e
      report_error(e)
      result 500
    end
  end
end

{'fraction_non_alphanumeric': 0.116214335421016, 'fraction_numerical': 0.01697981906750174, 'mean_word_length': 3.132259919493962, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3477614', 'n_tokens_mistral': 2285, 'n_tokens_neox': 2194, 'n_words': 489}