<commit_before>require "uri"
require "rexml/document"
require "rexml/xpath"
require "activesupport"

class FogbugzService
  class FogbugzError < RuntimeError; end
  class ClientOutOfDate < FogbugzError; end
  class BadXml < FogbugzError; end
  class BadCredentials < FogbugzError; end

  attr_reader :root_uri, :api_uri

  def initialize(root, curl, token=<PASSWORD>)
    @root_uri = root.respond_to?(:scheme) ? root : URI.parse(root)
    @curl = curl
    @token = token
  end

  def validate!
    document = get(@root_uri.merge("api.xml"))
    raise BadXml, "Did not find the expected root response element.  Instead, I found:\n#{document.root}" unless document.root.name == "response"

    minversion = REXML::XPath.first(document.root, "//minversion/text()").to_s
    raise ClientOutOfDate, "This client expected to find a minversion <= 3 in the api.xml file.  Instead it found #{minversion.inspect}" unless minversion.to_i <= 3

    relative_path = REXML::XPath.first(document.root, "//url/text()")
    @api_uri = @root_uri.merge(relative_path.to_s)
  end

  def connect
    validate!
    yield self
  end

  def logon(email, password)
    params = {"cmd" => "logon", "email" => email, "password" => password}
    uri = @api_uri.dup
    uri.query = params.to_query
    document = get(uri)
    bad_logon = REXML::XPath.first(document.root, "//error")
    raise BadCredentials, "Bad credentials supplied to Fogbugz: #{bad_logon}" unless bad_logon.blank?
    REXML::XPath.first(document.root, "//token/text()").to_s
  end

  def implement(data)
    uri = @api_uri.dup
    uri.query = {"cmd" => "resolve", "ixBug" => data[:case], "sEvent" => data[:message],
      "ixStatus" => STATES[:implemented], "token" => @token}.to_query
    get(uri)
  end

  def fix(data)
    uri = @api_uri.dup
    uri.query = {"cmd" => "resolve", "ixBug" => data[:case], "sEvent" => data[:message],
      "ixStatus" => STATES[:fixed], "token" => @token}.to_query
    get(uri)
  end

  def complete(data)
    uri = @api_uri.dup
    uri.query = {"cmd" => "resolve", "ixBug" => data[:case], "sEvent" => data[:message],
      "ixStatus" => STATES[:completed], "token" => @token}.to_query
    get(uri)
  end

  def close(data)
    uri = @api_uri.dup
    uri.query = {"cmd" => "close", "ixBug" => data[:case], "sEvent" => data[:message],
      "token" => @token}.to_query
    get(uri)
  end

  def append_message(data)
    uri = @api_uri.dup
    uri.query = {"cmd" => "edit", "ixBug" => data[:case], "sEvent" => data[:message],
      "token" => @token}.to_query
    get(uri)
  end

  protected
  # Returns an REXML::Document to the specified URI
  def get(uri)
    cmd = "#{@curl} --silent '#{uri.to_s}'"
    puts cmd
    data = `#{cmd}`
    begin
      REXML::Document.new(data)
    rescue REXML::ParseException
      raise BadXml, "Could not parse response data:\n#{data}"
    end
  end

  STATES = {:fixed => 2, :completed => 15, :implemented => 8}
end<commit_msg>Refactored FogbugzService#fix, #close, #complete, #append_message and #implement to have a single implementation.<commit_after>require "uri"
require "rexml/document"
require "rexml/xpath"
require "activesupport"

class FogbugzService
  class FogbugzError < RuntimeError; end
  class ClientOutOfDate < FogbugzError; end
  class BadXml < FogbugzError; end
  class BadCredentials < FogbugzError; end

  attr_reader :root_uri, :api_uri

  def initialize(root, curl, token=nil)
    @root_uri = root.respond_to?(:scheme) ? root : URI.parse(root)
    @curl = curl
    @token = token
  end

  def validate!
    document = get(@root_uri.merge("api.xml"))
    raise BadXml, "Did not find the expected root response element.  Instead, I found:\n#{document.root}" unless document.root.name == "response"

    minversion = REXML::XPath.first(document.root, "//minversion/text()").to_s
    raise ClientOutOfDate, "This client expected to find a minversion <= 3 in the api.xml file.  Instead it found #{minversion.inspect}" unless minversion.to_i <= 3

    relative_path = REXML::XPath.first(document.root, "//url/text()")
    @api_uri = @root_uri.merge(relative_path.to_s)
  end

  def connect
    validate!
    yield self
  end

  def logon(email, password)
    params = {"cmd" => "logon", "email" => email, "password" => password}
    uri = @api_uri.dup
    uri.query = params.to_query
    document = get(uri)
    bad_logon = REXML::XPath.first(document.root, "//error")
    raise BadCredentials, "Bad credentials supplied to Fogbugz: #{bad_logon}" unless bad_logon.blank?
    REXML::XPath.first(document.root, "//token/text()").to_s
  end

  def implement(data)
    tell_fogbugz(:resolve, data, STATES[:implemented])
  end

  def fix(data)
    tell_fogbugz(:resolve, data, STATES[:fixed])
  end

  def complete(data)
    tell_fogbugz(:resolve, data, STATES[:completed])
  end

  def close(data)
    tell_fogbugz(:close, data)
  end

  def append_message(data)
    tell_fogbugz(:edit, data)
  end

  protected
  # Returns an REXML::Document to the specified URI
  def get(uri)
    cmd = "#{@curl} --silent '#{uri.to_s}'"
    puts cmd
    data = `#{cmd}`
    begin
      REXML::Document.new(data)
    rescue REXML::ParseException
      raise BadXml, "Could not parse response data:\n#{data}"
    end
  end

  def tell_fogbugz(operation, data, status=nil)
    uri = @api_uri.dup
    params = {"cmd" => operation.to_s, "ixBug" => data[:case], "sEvent" => data[:message],
      "token" => @token}
    params["ixStatus"] = status if status
    uri.query = params.to_query
    get(uri)
  end

  STATES = {:fixed => 2, :completed => 15, :implemented => 8}
end
{'fraction_non_alphanumeric': 0.14718537110075297, 'fraction_numerical': 0.002151308712800287, 'mean_word_length': 3.6491666666666664, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, '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': '1741737', 'n_tokens_mistral': 1973, 'n_tokens_neox': 1924, 'n_words': 480}