<commit_before>require 'open-uri'

## factory-like class for creating direct connections with Peasant micro-service
## stripped from http://stackoverflow.com/questions/746207/ruby-design-pattern-how-to-make-an-extensible-factory-class

class PeasantAdapter
  @@subclasses = { }
  def self.create(type, url)
    c = @@subclasses[type]
    if c
      c.new(url)
    else
      raise "Unknown PeasantConnector type"
    end
  end
  def self.register_peasant name
    @@subclasses[name] = self
  end
end

class V1PeasantAdapter < PeasantAdapter
  def initialize url
    @@url = url
  end
  def getUsernameInfo username
    open("http://#{@@url}/user_info?username=#{username}") do |f|
      f.read
    end
  end
  register_peasant :v1
  Object.const_set("#{name.to_s.capitalize}PeasantConnector", self)
end

## example use
## aaa = PeasantConnector.create(:v1, "localhost:8888")
## aaa.getUsernameInfo("foo")
<commit_msg>redef of variable from global to local<commit_after>require 'open-uri'

## factory-like class for creating direct connections with Peasant micro-service
## stripped from http://stackoverflow.com/questions/746207/ruby-design-pattern-how-to-make-an-extensible-factory-class

class PeasantAdapter
  @@subclasses = { }
  def self.create(type, url)
    c = @@subclasses[type]
    if c
      c.new(url)
    else
      raise "Unknown PeasantConnector type"
    end
  end
  def self.register_peasant name
    @@subclasses[name] = self
  end
end

class V1PeasantAdapter < PeasantAdapter
  def initialize url
    @url = url
  end
  def getUsernameInfo username
    open("http://#{@url}/user_info?username=#{username}") do |f|
      f.read
    end
  end
  register_peasant :v1
  Object.const_set("#{name.to_s.capitalize}PeasantConnector", self)
end

## example use
## aaa = PeasantConnector.create(:v1, "localhost:8888")
## aaa.getUsernameInfo("foo")

{'fraction_non_alphanumeric': 0.11739364566505116, 'fraction_numerical': 0.014001077005923533, 'mean_word_length': 3.8766404199475066, 'pattern_counts': {'":': 0, '<': 5, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1307302', 'n_tokens_mistral': 629, 'n_tokens_neox': 613, 'n_words': 142}