<commit_before># encoding: utf-8
require 'net/http'
require 'net/https'
require 'uri'
require 'json'

class TrackerClient
  def initialize(key, project, requester_id)
    @api_key = key
    @project_id = project
    @requester_id = requester_id
    raise 'invalid api key for tracker' unless validate_string @api_key
    raise 'invalid project id for tracker' unless validate_string @project_id
    raise 'invalid requester id for tracker' unless validate_number @requester_id
  end

  def search(name:)
    uri = URI.parse("https://www.pivotaltracker.com/services/v5/projects/#{@project_id}/stories")
    uri.query = "filter=name:#{name}"
    request = Net::HTTP::Get.new(uri)
    request['Content-Type'] = 'application/json'
    request['X-TrackerToken'] = @api_key

    response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
      http.request(request)
    end

    raise response.message if response.code != '200'

    JSON.parse(response.body)
  end

  def post_to_tracker(name, description, tasks = [], point_value = nil)
    name = name.to_s
    raise 'requested tracker story has no title' unless validate_string name
    raise 'requested tracker story has no description' unless validate_string description

    task_api_objs = tasks.map { |task| { description: task } }

    payload = {
      name: name,
      description: description,
      requested_by_id: @requester_id,
      tasks: task_api_objs
    }

    payload[:estimate] = point_value if point_value

    create_story_uri = URI.parse("https://www.pivotaltracker.com/services/v5/projects/#{@project_id}/stories")

    request = Net::HTTP::Post.new(create_story_uri)
    request.body = payload.to_json
    request['Content-Type'] = 'application/json'
    request['X-TrackerToken'] = @api_key

    response = Net::HTTP.start(create_story_uri.hostname, create_story_uri.port, use_ssl: true) do |http|
      http.request(request)
    end

    raise response.message if response.code != '200'

    response
  end

  private

  def validate_string(cred)
    !(cred.nil? || !cred.is_a?(String) || cred.empty?)
  end

  def validate_number(cred)
    !(cred.nil? || !cred.is_a?(Numeric))
  end
end<commit_msg>Refector TrackerClient for easy http requests [#120583965]<commit_after># encoding: utf-8
require 'net/http'
require 'net/https'
require 'uri'
require 'json'

class TrackerClient
  def initialize(key, project, requester_id)
    @api_key = key
    @project_id = project
    @requester_id = requester_id
    raise 'invalid api key for tracker' unless validate_string @api_key
    raise 'invalid project id for tracker' unless validate_string @project_id
    raise 'invalid requester id for tracker' unless validate_number @requester_id
  end

  def search(name:)
    response = http_request do |uri|
      uri.query = "filter=name:#{name}"
      Net::HTTP::Get.new(uri)
    end
    JSON.parse(response.body)
  end

  def post_to_tracker(name, description, tasks = [], point_value = nil)
    name = name.to_s
    raise 'requested tracker story has no title' unless validate_string name
    raise 'requested tracker story has no description' unless validate_string description

    task_api_objs = tasks.map { |task| { description: task } }

    payload = {
      name: name,
      description: description,
      requested_by_id: @requester_id,
      tasks: task_api_objs
    }

    payload[:estimate] = point_value if point_value

    response = http_request do |uri|
      request = Net::HTTP::Post.new(uri)
      request.body = payload.to_json
      request
    end
    response
  end

  private

  def validate_string(cred)
    !(cred.nil? || !cred.is_a?(String) || cred.empty?)
  end

  def validate_number(cred)
    !(cred.nil? || !cred.is_a?(Numeric))
  end

  def http_request(&block)
    uri = URI.parse("https://www.pivotaltracker.com/services/v5/projects/#{@project_id}/stories")
    request = block.call(uri)
    request['Content-Type'] = 'application/json'
    request['X-TrackerToken'] = @api_key

    response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
      http.request(request)
    end

    raise response.message if response.code != '200'

    response
  end
end
{'fraction_non_alphanumeric': 0.11233007393274505, 'fraction_numerical': 0.005485332697352731, 'mean_word_length': 3.624035281146637, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 3, 'lorem ipsum': 0, 'www.': 3, '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': '3788429', 'n_tokens_mistral': 1368, 'n_tokens_neox': 1324, 'n_words': 329}