<commit_before>require "digitsend/version"
require "net/http"
require "net/http/post/multipart"
require "json"

module DigitSend
  class Config
    @host = 'digitsend.com'
    @use_ssl = true

    class <<self
      attr_accessor :api_token
      attr_accessor :host
      attr_accessor :use_ssl
      attr_writer :port

      def port
        @port || use_ssl ? 443 : 80
      end
    end
  end

  class Message
    def initialize
      @attachments = []
    end

    def self.send(&block)
      new.tap { |m| yield(m) }.send
    end

    attr_accessor :to, :cc, :subject, :body

    def add_file(filename, data = nil)
      @attachments << [ filename, data ]
    end

    def send
      api_call :post, '/api/messages', message: {
        to: to,
        cc: cc,
        subject: subject,
        body: body,
        s3_file_uuids: s3_file_uuids
      }
    end

    private

      def s3_file_uuids
        @attachments.collect do |filename, data|
          s3_file_uuid(filename, stream_for_data(filename, data))
        end
      end

      def stream_for_data(filename, data)
        if data.nil?
          File.open(filename, "r")
        else
          if data.is_a?(String)
            StringIO.new(data)
          else
            data
          end
        end
      end

      def s3_file_uuid(filename, stream)
         response = create_s3_file(filename)
         upload_to_s3(URI.parse(response["url"]), response["fields"], stream)
         update_s3_file(response["uuid"])
         response["uuid"]
      end

      def create_s3_file(name)
        api_call :post, '/api/s3_files', s3_file: { name: name }
      end

      def upload_to_s3(url, fields, stream)
        req = Net::HTTP::Post::Multipart.new \
          url.to_s,
          fields.merge("file" => UploadIO.new(stream, "binary/octet-stream"))

        n = Net::HTTP.new(url.host, url.port)
        n.use_ssl = true
        n.verify_mode = OpenSSL::SSL::VERIFY_NONE

        n.start do |http|
          http.request(req)<commit_msg>refactored<commit_after>require "digitsend/version"
require "net/http"
require "net/http/post/multipart"
require "json"

module DigitSend
  class Config
    @host = 'digitsend.com'
    @use_ssl = true

    class <<self
      attr_accessor :api_token
      attr_accessor :host
      attr_accessor :use_ssl
      attr_writer :port

      def port
        @port || use_ssl ? 443 : 80
      end
    end
  end

  class Message
    def initialize
      @attachments = []
    end

    def self.send(&block)
      new.tap { |m| yield(m) }.send
    end

    attr_accessor :to, :cc, :subject, :body

    def add_file(filename, data = nil)
      @attachments << [ filename, data ]
    end

    def send
      api_call :post, '/api/messages', message: {
        to: to,
        cc: cc,
        subject: subject,
        body: body,
        s3_file_uuids: s3_file_uuids
      }
    end

    private

      def s3_file_uuids
        @attachments.collect do |filename, data|
          s3_file_uuid(filename, stream_for_data(filename, data))
        end
      end

      def stream_for_data(filename, data)
        if data.nil?
          File.open(filename, "r")
        elsif data.is_a?(String)
          StringIO.new(data)
        else
          data
        end
      end

      def s3_file_uuid(filename, stream)
         response = create_s3_file(filename)
         upload_to_s3(URI.parse(response["url"]), response["fields"], stream)
         update_s3_file(response["uuid"])
         response["uuid"]
      end

      def create_s3_file(name)
        api_call :post, '/api/s3_files', s3_file: { name: name }
      end

      def upload_to_s3(url, fields, stream)
        req = Net::HTTP::Post::Multipart.new \
          url.to_s,
          fields.merge("file" => UploadIO.new(stream, "binary/octet-stream"))

        n = Net::HTTP.new(url.host, url.port)
        n.use_ssl = true
        n.verify_mode = OpenSSL::SSL::VERIFY_NONE

        n.start do |http|
          http.request(req)
{'fraction_non_alphanumeric': 0.10639899623588457, 'fraction_numerical': 0.008531994981179424, 'mean_word_length': 2.0083018867924527, 'pattern_counts': {'":': 0, '<': 11, '<?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': '716491', 'n_tokens_mistral': 1346, 'n_tokens_neox': 1304, 'n_words': 291}