<commit_before>require 'tent-client/version'
require 'faraday'
require 'tent-client/faraday/utils'
require 'tent-client/multipart-post/parts'
require 'tent-client/faraday/chunked_adapter'
require 'tent-client/tent_type'
require 'tent-client/middleware/content_type_header'
require 'tent-client/middleware/encode_json'
require 'tent-client/middleware/authentication'
require 'tent-client/cycle_http'
require 'tent-client/discovery'
require 'tent-client/post'
require 'tent-client/attachment'

##
# Ruby 1.8.7 compatibility
require 'uri'
unless URI.respond_to?(:encode_www_form_component)
  require 'addressable/uri'

  URI.class_eval do
    def self.encode_www_form_component(str)
      Addressable::URI.encode_component(
        str.to_s.gsub(/(\r\n|\n|\r)/, "\r\n"),
        Addressable::URI::CharacterClasses::UNRESERVED
      ).gsub("%20", "+")
    end
  end
end

class TentClient
  POST_MEDIA_TYPE = %(application/vnd.tent.post.v0+json).freeze
  POST_CONTENT_TYPE = %(#{POST_MEDIA_TYPE}; type="%s").freeze
  POST_MENTIONS_CONTENT_TYPE = %(application/vnd.tent.post-mentions.v0+json).freeze
  POST_VERSIONS_CONTENT_TYPE = %(application/vnd.tent.post-versions.v0+json).freeze
  POST_CHILDREN_CONTENT_TYPE = %(application/vnd.tent.post-children.v0+json).freeze
  OAUTH_TOKEN_CONTENT_TYPE = %(application/vnd.tent.oauth.token.v0+json).freeze
  MULTIPART_CONTENT_TYPE = 'multipart/form-data'.freeze
  MULTIPART_BOUNDARY = "-----------TentPart".freeze

  MalformedServerMeta = Class.new(StandardError)
  ServerNotFound = Class.new(StandardError)

  attr_reader :entity_uri, :options
  attr_writer :faraday_adapter, :server_meta_post
  def initialize(entity_uri, options = {})
    @server_meta_post = options.delete(:server_meta)
    @faraday_adapter = options.delete(:faraday_adapter)
    @faraday_block = options.delete(:faraday_setup)
    @entity_uri, @options = entity_uri, options
  end

  def dup
    self.class.new(@entity_uri, @options.merge(
      :server_meta => @server_meta_post,
      :faraday_adapter => @faraday_adapter,
      :faraday_block => @faraday_block
    ))
  end

  def server_meta
    server_meta_post['content'] if server_meta_post
  end

  def server_meta_post
    @server_meta_post ||= entity_uri ? Discovery.discover(self, entity_uri) : nil
  end

  def primary_server
    server_meta['servers'].sort_by { |s| s['preference'] }.first
  end

  def new_http
    CycleHTTP.new(self) do |f|
      f.use Middleware::ContentTypeHeader
      f.use Middleware::EncodeJson unless @options[:skip_serialization]
      f.use Middleware::Authentication, @options[:credentials] if @options[:credentials]
      f.response :multi_json, :content_type => /\bjson\Z/ unless @options[:skip_serialization] || @options[:skip_response_serialization]
      @faraday_block.call(f) if @faraday_block
      f.adapter *Array(faraday_adapter)
    end
  end

  def http
    @http ||= new_http
  end

  def faraday_adapter
    @faraday_adapter || Faraday.default_adapter
  end

  def faraday_adapter=(adapter)
    @faraday_adapter = adapter
  end

  def hex_digest(data)
    if data.kind_of?(IO)
      _data = data.read
      data.rewind
      data = _data
    end
    Digest::SHA512.new.update(data).to_s[0...64]
  end

  def post
    Post.new(self)
  end
<commit_msg>Rename faraday_block to faraday_setup #consistency<commit_after>require 'tent-client/version'
require 'faraday'
require 'tent-client/faraday/utils'
require 'tent-client/multipart-post/parts'
require 'tent-client/faraday/chunked_adapter'
require 'tent-client/tent_type'
require 'tent-client/middleware/content_type_header'
require 'tent-client/middleware/encode_json'
require 'tent-client/middleware/authentication'
require 'tent-client/cycle_http'
require 'tent-client/discovery'
require 'tent-client/post'
require 'tent-client/attachment'

##
# Ruby 1.8.7 compatibility
require 'uri'
unless URI.respond_to?(:encode_www_form_component)
  require 'addressable/uri'

  URI.class_eval do
    def self.encode_www_form_component(str)
      Addressable::URI.encode_component(
        str.to_s.gsub(/(\r\n|\n|\r)/, "\r\n"),
        Addressable::URI::CharacterClasses::UNRESERVED
      ).gsub("%20", "+")
    end
  end
end

class TentClient
  POST_MEDIA_TYPE = %(application/vnd.tent.post.v0+json).freeze
  POST_CONTENT_TYPE = %(#{POST_MEDIA_TYPE}; type="%s").freeze
  POST_MENTIONS_CONTENT_TYPE = %(application/vnd.tent.post-mentions.v0+json).freeze
  POST_VERSIONS_CONTENT_TYPE = %(application/vnd.tent.post-versions.v0+json).freeze
  POST_CHILDREN_CONTENT_TYPE = %(application/vnd.tent.post-children.v0+json).freeze
  OAUTH_TOKEN_CONTENT_TYPE = %(application/vnd.tent.oauth.token.v0+json).freeze
  MULTIPART_CONTENT_TYPE = 'multipart/form-data'.freeze
  MULTIPART_BOUNDARY = "-----------TentPart".freeze

  MalformedServerMeta = Class.new(StandardError)
  ServerNotFound = Class.new(StandardError)

  attr_reader :entity_uri, :options
  attr_writer :faraday_adapter, :faraday_setup, :server_meta_post
  def initialize(entity_uri, options = {})
    @server_meta_post = options.delete(:server_meta)
    @faraday_adapter = options.delete(:faraday_adapter)
    @faraday_setup = options.delete(:faraday_setup)
    @entity_uri, @options = entity_uri, options
  end

  def dup
    self.class.new(@entity_uri, @options.merge(
      :server_meta => @server_meta_post,
      :faraday_adapter => @faraday_adapter,
      :faraday_setup => @faraday_setup
    ))
  end

  def server_meta
    server_meta_post['content'] if server_meta_post
  end

  def server_meta_post
    @server_meta_post ||= entity_uri ? Discovery.discover(self, entity_uri) : nil
  end

  def primary_server
    server_meta['servers'].sort_by { |s| s['preference'] }.first
  end

  def new_http
    CycleHTTP.new(self) do |f|
      f.use Middleware::ContentTypeHeader
      f.use Middleware::EncodeJson unless @options[:skip_serialization]
      f.use Middleware::Authentication, @options[:credentials] if @options[:credentials]
      f.response :multi_json, :content_type => /\bjson\Z/ unless @options[:skip_serialization] || @options[:skip_response_serialization]
      @faraday_setup.call(f) if @faraday_setup
      f.adapter *Array(faraday_adapter)
    end
  end

  def http
    @http ||= new_http
  end

  def faraday_adapter
    @faraday_adapter || Faraday.default_adapter
  end

  def faraday_adapter=(adapter)
    @faraday_adapter = adapter
  end

  def hex_digest(data)
    if data.kind_of?(IO)
      _data = data.read
      data.rewind
      data = _data
    end
    Digest::SHA512.new.update(data).to_s[0...64]
  end

  def post
    Post.new(self)
  end

{'fraction_non_alphanumeric': 0.1210694212365183, 'fraction_numerical': 0.004861005620537749, 'mean_word_length': 4.3441558441558445, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '612150', 'n_tokens_mistral': 2392, 'n_tokens_neox': 2283, 'n_words': 371}