<commit_before>require 'uri'

class Order
  def initialize(order_hash, config)
    @order_hash = order_hash
    @config     = config
  end

  def address
    address_type = @config['twilio.address_type'] || "billing"
    @order_hash["#{address_type}_address"]
  end

  def customer_phone
    address['phone']
  end

  def number
    @order_hash['number']
  end

  def customer_name
    address['firstname']
  end
end

class TwilioEndpoint < EndpointBase
  set :logging, true

  post '/sms_order' do
    order  = Order.new(@message['payload']['order'], @config)

    client = Twilio::REST::Client.new(@config['twilio.account_sid'], @config['twilio.auth_token'])

    body   = "Hey #{order.customer_name}! Your order #{order.number} has been received."

    client.account.messages.create(
      from:   @config['twilio.phone_from'],
      to:     order.customer_phone,
      body:   body
    )

    process_result 200, { message_id: @message[:message_id] }
  end

  post '/call_order' do
    order  = Order.new(@message['payload']['order'], @config)

    client = Twilio::REST::Client.new(@config['twilio.account_sid'], @config['twilio.auth_token'])

    body   = "Hey #{order.customer_name}! Your order has been received."

    url = 'http://urlecho.appspot.com/echo?status=200&Content-Type=text%2Fxml&body='
    url += "<?xml version='1.0' encoding='UTF-8'?><Response><Say>#{body}</Say></Response>"
    url = URI.escape(url)

    client.account.calls.create(
      from:   @config['twilio.phone_from'],
      to:     order.customer_phone,
      url:    url
    )

    process_result 200, { message_id: @message[:message_id] }
  end
end

<commit_msg>Add info notifications on successful message send<commit_after>require 'uri'

class Order
  def initialize(order_hash, config)
    @order_hash = order_hash
    @config     = config
  end

  def address
    address_type = @config['twilio.address_type'] || "billing"
    @order_hash["#{address_type}_address"]
  end

  def customer_phone
    address['phone']
  end

  def number
    @order_hash['number']
  end

  def customer_name
    address['firstname']
  end
end

class TwilioEndpoint < EndpointBase
  set :logging, true

  post '/sms_order' do
    order  = Order.new(@message['payload']['order'], @config)

    client = Twilio::REST::Client.new(@config['twilio.account_sid'], @config['twilio.auth_token'])

    body   = "Hey #{order.customer_name}! Your order #{order.number} has been received."

    client.account.messages.create(
      from:   @config['twilio.phone_from'],
      to:     order.customer_phone,
      body:   body
    )

    process_result 200, { message_id: @message[:message_id], notifications: [{level: 'info', subject: 'SMS confirmation sent to #{order.customer_phone}', description: body}] }
  end

  post '/call_order' do
    order  = Order.new(@message['payload']['order'], @config)

    client = Twilio::REST::Client.new(@config['twilio.account_sid'], @config['twilio.auth_token'])

    body   = "Hey #{order.customer_name}! Your order has been received."

    url = 'http://urlecho.appspot.com/echo?status=200&Content-Type=text%2Fxml&body='
    url += "<?xml version='1.0' encoding='UTF-8'?><Response><Say>#{body}</Say></Response>"
    url = URI.escape(url)

    client.account.calls.create(
      from:   @config['twilio.phone_from'],
      to:     order.customer_phone,
      url:    url
    )

    process_result 200, { message_id: @message[:message_id], notifications: [{level: 'info', subject: 'Call placed to #{order.customer_phone}', description: body}] }
  end
end


{'fraction_non_alphanumeric': 0.14969048958919529, 'fraction_numerical': 0.007315700619020821, 'mean_word_length': 3.7148541114058355, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 2, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '1260136', 'n_tokens_mistral': 1190, 'n_tokens_neox': 1137, 'n_words': 239}