<commit_before>module FourInfo
  def self.mode;     @@mode ||= :live; end
  def self.mode=(m); @@mode = m;      end

  def self.numerize(numberish)
    numberish.to_s.scan(/\d+/).join
  end

  def self.internationalize(given_number)
    number = numerize(given_number)
    case number.size
    when 10
      "+1#{number}"
    when 11
      "+#{number}"
    when 12
      number =~ /\+\d(11)/ ? number : nil
    else
      nil
    end
  end

  Gateway = URI.parse 'http://gateway.4info.net:8080/msg'

  module Contactable

    Attributes = [  :sms_phone_number,
                    :sms_confirmation_code,
                    :sms_confirmation_attempted,
                    :sms_confirmed ]

    def self.included(model)
      gem 'haml'
      require 'haml'
      require 'net/http'

      Attributes.each do |attribute|
        # add a method for setting or retrieving
        # which column should be used for which attribute
        # 
        # :sms_phone_number_column defaults to :sms_phone_number, etc.
        model.instance_eval "
          def #{attribute}_column(value = nil)
            @#{attribute}_column ||= :#{attribute}
            @#{attribute}_column = value if value
            @#{attribute}_column
          end
        "
        # provide a helper method to access the right value
        # no matter which column it's stored in
        #
        # e.g.: @user.four_info_sms_confirmed
        #       => @user.send(User.sms_confirmed_column)
        model.class_eval "
          def four_info_#{attribute}
            send self.class.#{attribute}_column
          end
          alias_method :four_info_#{attribute}?, :four_info_#{attribute}
        "
      end
    end

    def confirm_sms!
      Confirmation.new(four_info_sms_phone_number, self).try
    end
  end

  class Confirmation
    def initialize(number, contactable_record)
      @number = FourInfo.numerize(number)
      @contactable_record = contactable_record
    end

    def try
      return true  if @contactable_record.four_info_sms_confirmed?
      return false if @number.blank?

      response = Request.new.confirm(@number)
      if response.success?
        @contactable_record.sms_confirmation_code = response.confirmation_code
        @contactable_record.sms_confirmation_attempted = Time.now
        @contactable_record.save
      else
        raise "Confirmation Failed: #{response.inspect}"
      end
    end
  end

  class Request

    @@templates = Dir.glob(File.expand_path(File.join(File.dirname(__FILE__), 'templates', '*.haml')))
    @@test_mode_config_file = File.join(File.dirname(__FILE__), '..', 'test', 'sms.yml')<commit_msg>properly setting attributes when confirm succeeds<commit_after>module FourInfo
  def self.mode;     @@mode ||= :live; end
  def self.mode=(m); @@mode = m;      end

  def self.numerize(numberish)
    numberish.to_s.scan(/\d+/).join
  end

  def self.internationalize(given_number)
    number = numerize(given_number)
    case number.size
    when 10
      "+1#{number}"
    when 11
      "+#{number}"
    when 12
      number =~ /\+\d(11)/ ? number : nil
    else
      nil
    end
  end

  Gateway = URI.parse 'http://gateway.4info.net:8080/msg'

  module Contactable

    Attributes = [  :sms_phone_number,
                    :sms_confirmation_code,
                    :sms_confirmation_attempted,
                    :sms_confirmed ]

    def self.included(model)
      gem 'haml'
      require 'haml'
      require 'net/http'

      Attributes.each do |attribute|
        # add a method for setting or retrieving
        # which column should be used for which attribute
        # 
        # :sms_phone_number_column defaults to :sms_phone_number, etc.
        model.instance_eval "
          def #{attribute}_column(value = nil)
            @#{attribute}_column ||= :#{attribute}
            @#{attribute}_column = value if value
            @#{attribute}_column
          end
        "
        # provide a helper method to access the right value
        # no matter which column it's stored in
        #
        # e.g.: @user.four_info_sms_confirmed
        #       => @user.send(User.sms_confirmed_column)
        model.class_eval "
          def four_info_#{attribute}(value = nil)
            value ?
              send(self.class.#{attribute}_column.to_s+'=', value) :
              send(self.class.#{attribute}_column)
          end
          alias_method :four_info_#{attribute}?, :four_info_#{attribute}
          alias_method :four_info_#{attribute}=, :four_info_#{attribute}
        "
      end
    end

    def confirm_sms!
      Confirmation.new(four_info_sms_phone_number, self).try
    end
  end

  class Confirmation
    def initialize(number, contactable_record)
      @number = FourInfo.numerize(number)
      @contactable_record = contactable_record
    end

    def try
      return true  if @contactable_record.four_info_sms_confirmed?
      return false if @number.blank?

      response = Request.new.confirm(@number)
      if response.success?
        @contactable_record.four_info_sms_confirmation_code = response.confirmation_code
        @contactable_record.four_info_sms_confirmation_attempted = Time.now
        @contactable_record.save
      else
        raise "Confirmation Failed: #{response.inspect}"
      end
    end
  end

  class Request

    @@templates = Dir.glob(File.expand_path(File.join(File.dirname(__FILE__), 'templates', '*.haml')))
    @@test_mode_config_file = File.join(File.dirname(__FILE__), '..', 'test', 'sms.yml')
{'fraction_non_alphanumeric': 0.10123367198838897, 'fraction_numerical': 0.0050798258345428155, 'mean_word_length': 2.3331318016928657, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '572227', 'n_tokens_mistral': 1737, 'n_tokens_neox': 1657, 'n_words': 358}