<commit_before>module Snapcat
  class Requestor
    include HTTMultiParty

    APP_VERSION = '6.0.0'
    SECRET = '<KEY>'
    STATIC_TOKEN = '<KEY>'
    HASH_PATTERN = '0001110111101110001111010101111011010001001110011000110001000110'

    base_uri 'https://feelinsonice-hrd.appspot.com/bq/'

    def initialize(username)
      @auth_token = STATIC_TOKEN
      @username = username
    end

    def request(endpoint, data = {})
      response = self.class.post(
        "/#{endpoint}",
        { body: merge_defaults_with(data) }
      )

      additional_fields = additional_fields_for(data)
      result = Snapcat::Response.new(response, additional_fields)

      auth_token_from(result, endpoint)
      result
    end

    def request_media(snap_id)
      response = self.class.post(
        '/blob',
        { body: merge_defaults_with({ id: snap_id, username: @username }) }
      )

      Response.new(response)
    end

    def request_with_username(endpoint, data = {})
      request(endpoint, data.merge({ username: @username }))
    end

    def request_upload(data, type = nil)
      encrypted_data = Crypt.encrypt(data)
      media = Media.new(encrypted_data, type)
      file_extension = media.file_extension

      begin
        file = Tempfile.new(['snap', ".#{file_extension}"])
        file.write(encrypted_data)
        file.rewind

        return request_with_username(
          'upload',
          data: file,
          media_id: media.generate_id(@username),
          type: media.type_code
        )
      ensure
        file.close
        file.unlink
      end
    end

    private

    def additional_fields_for(data)
      if data[:media_id]
        { media_id: data[:media_id] }
      else<commit_msg>Coerce media to utf 8<commit_after>module Snapcat
  class Requestor
    include HTTMultiParty

    APP_VERSION = '6.0.0'
    SECRET = '<KEY>'
    STATIC_TOKEN = '<KEY>'
    HASH_PATTERN = '0001110111101110001111010101111011010001001110011000110001000110'

    base_uri 'https://feelinsonice-hrd.appspot.com/bq/'

    def initialize(username)
      @auth_token = STATIC_TOKEN
      @username = username
    end

    def request(endpoint, data = {})
      response = self.class.post(
        "/#{endpoint}",
        { body: merge_defaults_with(data) }
      )

      additional_fields = additional_fields_for(data)
      result = Snapcat::Response.new(response, additional_fields)

      auth_token_from(result, endpoint)
      result
    end

    def request_media(snap_id)
      response = self.class.post(
        '/blob',
        { body: merge_defaults_with({ id: snap_id, username: @username }) }
      )

      Response.new(response)
    end

    def request_with_username(endpoint, data = {})
      request(endpoint, data.merge({ username: @username }))
    end

    def request_upload(data, type = nil)
      encrypted_data = Crypt.encrypt(data)
      media = Media.new(encrypted_data, type)
      file_extension = media.file_extension

      begin
        file = Tempfile.new(['snap', ".#{file_extension}"])
        file.write(encrypted_data.force_encoding('utf-8'))
        file.rewind

        return request_with_username(
          'upload',
          data: file,
          media_id: media.generate_id(@username),
          type: media.type_code
        )
      ensure
        file.close
        file.unlink
      end
    end

    private

    def additional_fields_for(data)
      if data[:media_id]
        { media_id: data[:media_id] }
      else
{'fraction_non_alphanumeric': 0.10263761467889908, 'fraction_numerical': 0.0389908256880734, 'mean_word_length': 2.451038575667656, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, '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': '628510', 'n_tokens_mistral': 1186, 'n_tokens_neox': 1065, 'n_words': 203}