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

module HttpService # mix-in

  module_function

  def http_get(method, *args)
    http_get_hash(method, args_hash(method, *args))
  end

  def http_get_hash(method, args_hash)
    http(method, args_hash) { |uri| Net::HTTP::Get.new(uri) }
  end

  # - - - - - - - - - - - - - - - - - - -

  def http_post(method, *args)
    http_post_hash(method, args_hash(method, *args))
  end

  def http_post_hash(method, args_hash)
    http(method, args_hash) { |uri| Net::HTTP::Post.new(uri) }
  end

  # - - - - - - - - - - - - - - - - - - -
  # - - - - - - - - - - - - - - - - - - -

  def args_hash(method, *args)
    parameters = self.class.instance_method(method.to_s).parameters
    Hash[parameters.map.with_index { |parameter,index|
      [parameter[1], args[index]]
    }]
  end

  def http(method, args)
    uri = URI.parse("http://#{hostname}:#{port}/" + method.to_s)
    request = yield uri.request_uri
    request.content_type = 'application/json'
    request.body = args.to_json
    service = Net::HTTP.new(uri.host, uri.port)
    response = service.request(request)
    json = JSON.parse(response.body)
    result(json, method.to_s)
  end

  def result(json, name)
    fail error(name, 'bad json') unless json.class.name == 'Hash'
    exception = json['exception']
    fail error(name, exception)  unless exception.nil?
    fail error(name, 'no key')   unless json.key? name
    json[name]
  end

  def error(name, message)
    StandardError.new("#{self.class.name}:#{name}:#{message}")
  end

end<commit_msg>refactor http_server so http() method can become a mockable external service<commit_after>require 'json'
require 'net/http'

module HttpService # mix-in

  module_function

  def http_get(method, *args)
    http_get_hash(method, args_hash(method, *args))
  end

  def http_get_hash(method, args_hash)
    json = http('GET', hostname, port, method, args_hash)
    result(json, method.to_s)
  end

  # - - - - - - - - - - - - - - - - - - -

  def http_post(method, *args)
    http_post_hash(method, args_hash(method, *args))
  end

  def http_post_hash(method, args_hash)
    json = http('POST', hostname, port, method, args_hash)
    result(json, method.to_s)
  end

  # - - - - - - - - - - - - - - - - - - -
  # - - - - - - - - - - - - - - - - - - -

  def args_hash(method, *args)
    parameters = self.class.instance_method(method.to_s).parameters
    Hash[parameters.map.with_index { |parameter,index|
      [parameter[1], args[index]]
    }]
  end

  def http(gp, hostname, port, method, named_args)
    uri = URI.parse("http://#{hostname}:#{port}/" + method.to_s)
    request = Net::HTTP:: Get.new(uri) if gp == 'GET'
    request = Net::HTTP::Post.new(uri) if gp == 'POST'
    request.content_type = 'application/json'
    request.body = named_args.to_json
    service = Net::HTTP.new(uri.host, uri.port)
    response = service.request(request)
    JSON.parse(response.body)
  end

  def result(json, name)
    fail error(name, 'bad json') unless json.class.name == 'Hash'
    exception = json['exception']
    fail error(name, exception)  unless exception.nil?
    fail error(name, 'no key')   unless json.key? name
    json[name]
  end

  def error(name, message)
    StandardError.new("#{self.class.name}:#{name}:#{message}")
  end

end
{'fraction_non_alphanumeric': 0.16399394856278365, 'fraction_numerical': 0.000605143721633888, 'mean_word_length': 2.804372842347526, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7450765', 'n_tokens_mistral': 1191, 'n_tokens_neox': 1176, 'n_words': 231}