<commit_before>require 'net/http'
require 'json'

class DiscourseApi::Resource
  attr_accessor :protocol, :host, :port, :api_key, :api_username

  def self.put(args)
    # ruby 1.9.3 for now
    array_args = args.to_a
    method_name, path = array_args[0]
    route_args = {}
    array_args[1..-1].each do |k, v|
      route_args[k] = v
    end

    define_method method_name do |args|
      parsed_path = DiscourseApi::ParsedPath.new(path, route_args)
      perform_put(parsed_path, args)
    end
  end

  def self.post(args)
    # ruby 1.9.3 for now
    array_args = args.to_a
    method_name, path = array_args[0]
    route_args = {}
    array_args[1..-1].each do |k, v|
      route_args[k] = v
    end

    define_method method_name do |args|
      parsed_path = DiscourseApi::ParsedPath.new(path, route_args)
      perform_post(parsed_path, args)
    end
  end

  def self.get(args)
    # ruby 1.9.3 for now
    array_args = args.to_a
    method_name, path = array_args[0]
    route_args = {}
    array_args[1..-1].each do |k, v|
      route_args[k] = v
    end
    define_method method_name do |args|
      parsed_path = DiscourseApi::ParsedPath.new(path, route_args)
      perform_get(parsed_path, args)
    end
  end

  def api_args(args)
    args.merge(:api_key => self.api_key, :api_username => self.api_username)
  end

  def perform_get(parsed_path, args)
    parsed_path.validate!(args)
    path, actual_args = parsed_path.generate(args)
    actual_args = api_args(actual_args)
    req = Net::HTTP::Get.new(path, initheader = {'Content-Type' =>'application/json'})
    r = http_client.start {|http| http.request(req) }
    parse_result( r.body )
  end

  def perform_post(parsed_path, args)
    parsed_path.validate!(args)
    path, actual_args = parsed_path.generate(args)
    actual_args = api_args(actual_args)

    req = Net::HTTP::Post.new(path, initheader = {'Content-Type' =>'application/json'})
    req.body = api_args(actual_args).to_json
    http_client.start {|http| http.request(req) }
  end

  def perform_put(parsed_path, args)
    parsed_path.validate!(args)
    path, actual_args = parsed_path.generate(args)
    actual_args = api_args(actual_args)

    req = Net::HTTP::Put.new(path, initheader = {'Content-Type' =>'application/json'})
    req.body = api_args(actual_args).to_json
    http_client.start {|http| http.request(req) }
  end

  private
    def parse_result( text )
      JSON.parse( text, :create_extensions => false )
    end

    def http_client
      if protocol == 'https'
        client = Net::HTTP.new(host, port)
        client.use_ssl = true
        client.verify_mode = OpenSSL::SSL::VERIFY_NONE
        client
      else
        Net::HTTP.new(host, port)
      end
    end

end<commit_msg>DRY up Resource class into two main methods.<commit_after>require 'net/http'
require 'json'

class DiscourseApi::Resource
  attr_accessor :protocol, :host, :port, :api_key, :api_username

  def self.get(args)
    self.build_request(:get, args)
  end

  def self.post(args)
    self.build_request(:post, args)
  end

  def self.put(args)
    self.build_request(:put, args)
  end

  def api_args(args)
    args.merge(:api_key => self.api_key, :api_username => self.api_username)
  end

  private

  def http_client
    if protocol == 'https'
      client = Net::HTTP.new(host, port)
      client.use_ssl = true
      client.verify_mode = OpenSSL::SSL::VERIFY_NONE
      client
    else
      Net::HTTP.new(host, port)
    end
  end

  def parse_result( text )
    JSON.parse( text, :create_extensions => false )
  end

  def perform(method, parsed_path, args)
    parsed_path.validate!(args)
    path, actual_args = parsed_path.generate(args)
    actual_args = api_args(actual_args)

    request_type = Object.const_get("Net::HTTP::#{method.to_s.capitalize}")
    req = request_type.new(path, initheader = {'Content-Type' =>'application/json'})
    req.body = api_args(actual_args).to_json if [:post, :put].include? method

    http_client.start {|http| http.request(req) }
  end

  def self.build_request(method, args)
    # ruby 1.9.3 for now
    array_args = args.to_a
    method_name, path = array_args[0]
    route_args = {}
    array_args[1..-1].each do |k, v|
      route_args[k] = v
    end

    define_method method_name do |args|
      parsed_path = DiscourseApi::ParsedPath.new(path, route_args)
      perform(method, parsed_path, args)
    end
  end

end
{'fraction_non_alphanumeric': 0.11963677639046538, 'fraction_numerical': 0.005448354143019296, 'mean_word_length': 3.2365384615384616, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '493610', 'n_tokens_mistral': 1571, 'n_tokens_neox': 1552, 'n_words': 325}