<commit_before>module TwilioContactable
  module Gateway
    class WebsiteAddressNotConfiguredError < ArgumentError
      def initialize
        super "Please define your `website_address` config parameter "+
              "in the TwilioContactable initializer. "+
              "It should be in the format of: \"http://example.com/\""

      end
    end
    class ControllerNotConfiguredError < ArgumentError
      def initialize(record)
        super "You're initiating a voice call for a "+
              record.class.name.inspect +
              " but you have not specified this class "+
              "as a possible recipient in a controller."
      end
    end
    class Response
      def initialize(*args)
        @options = args.last
      end

      def success?
        :success == @options[:status]
      end
    end
    Success = Response.new(:status => :success)
    Error   = Response.new(:status => :error)

    API_VERSION   = '2010-04-01'

    class << self

      def initiate_voice_call(record, to, from = nil)

        unless TwilioContactable.configuration.website_address
          raise WebsiteAddressNotConfiguredError.new
        end
        unless controller = record.class.twilio_contactable.controller
          raise ControllerNotConfiguredError.new(record)
        end

        url = TwilioContactable.configuration.website_address.chomp('/')
        url = "#{url}/#{record.class.twilio_contactable.controller}"
        url = "#{url}/start_voice_confirmation?contactable_type=#{record.class}&contactable_id=#{record.id}"

        deliver :voice,
                'To' => to,
                'From' => from,
                'Url' => url
      end

      def deliver_sms(message, to, from = nil)
        deliver :sms,
                'Message' => message,
                'To' => to,
                'From' => from
      end

      def account
        @account ||= begin
          if TwilioContactable.configuration.client_id.blank? ||
             TwilioContactable.configuration.client_key.blank?
             raise "Add your Twilio account id (as client_id) and token (as client_key) to the TwilioContactable.configure block"
          end
          Twilio::RestAccount.new(
                      TwilioContactable.configuration.client_id,
                      TwilioContactable.configuration.client_key
                    )
        end
      end

      protected

        def deliver(type, data = {})

          data['From'] = TwilioContactable.configuration.default_from_phone_number if data['From'].blank?
          raise "'From' number required for Twilio" unless data['From']

          service = case type
          when :sms
            'SMS/Messages'
          when :voice
            'Calls'
          end

          response = post service, data
 
          Net::HTTPCreated == response.code_type ?
                                TwilioContactable::Gateway::Success :
                                TwilioContactable::Gateway::Error
        end

        def post(service, data = {})
          url = "/#{API_VERSION}/Accounts/#{TwilioContactable.configuration.client_id}/#{service}"
          puts "putting data: #{data.inspect}"
          puts "to url: #{url}"
          account.request url,
                          "POST",
                          data
        end
    end
  end
end<commit_msg>updating SMS call to 2010-04-01 API<commit_after>module TwilioContactable
  module Gateway
    class WebsiteAddressNotConfiguredError < ArgumentError
      def initialize
        super "Please define your `website_address` config parameter "+
              "in the TwilioContactable initializer. "+
              "It should be in the format of: \"http://example.com/\""

      end
    end
    class ControllerNotConfiguredError < ArgumentError
      def initialize(record)
        super "You're initiating a voice call for a "+
              record.class.name.inspect +
              " but you have not specified this class "+
              "as a possible recipient in a controller."
      end
    end
    class Response
      def initialize(*args)
        @options = args.last
      end

      def success?
        :success == @options[:status]
      end
    end
    Success = Response.new(:status => :success)
    Error   = Response.new(:status => :error)

    API_VERSION   = '2010-04-01'

    class << self

      def initiate_voice_call(record, to, from = nil)

        unless TwilioContactable.configuration.website_address
          raise WebsiteAddressNotConfiguredError.new
        end
        unless controller = record.class.twilio_contactable.controller
          raise ControllerNotConfiguredError.new(record)
        end

        url = TwilioContactable.configuration.website_address.chomp('/')
        url = "#{url}/#{record.class.twilio_contactable.controller}"
        url = "#{url}/start_voice_confirmation?contactable_type=#{record.class}&contactable_id=#{record.id}"

        deliver :voice,
                'To' => to,
                'From' => from,
                'Url' => url
      end

      def deliver_sms(message, to, from = nil)
        deliver :sms,
                'Body' => message,
                'To' => to,
                'From' => from
      end

      def account
        @account ||= begin
          if TwilioContactable.configuration.client_id.blank? ||
             TwilioContactable.configuration.client_key.blank?
             raise "Add your Twilio account id (as client_id) and token (as client_key) to the TwilioContactable.configure block"
          end
          Twilio::RestAccount.new(
                      TwilioContactable.configuration.client_id,
                      TwilioContactable.configuration.client_key
                    )
        end
      end

      protected

        def deliver(type, data = {})

          data['From'] = TwilioContactable.configuration.default_from_phone_number if data['From'].blank?
          raise "'From' number required for Twilio" unless data['From']

          service = case type
          when :sms
            'SMS/Messages'
          when :voice
            'Calls'
          end

          post service, data
        end

        def post(service, data = {})
          url = "/#{API_VERSION}/Accounts/#{TwilioContactable.configuration.client_id}/#{service}"
          account.request url,
                          "POST",
                          data
        end
    end
  end
end
{'fraction_non_alphanumeric': 0.08820784069695084, 'fraction_numerical': 0.00373366521468575, 'mean_word_length': 1.825934065934066, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, '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': '5345271', 'n_tokens_mistral': 1696, 'n_tokens_neox': 1628, 'n_words': 417}