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

module Lib
  class << self
    def [](name)
      LibGen.proxy_method_missing(name)
    end
  
    def method_missing(name)
      LibGen.proxy_method_missing(name)
    end
  end
  
  class LibGen

    HOST = 'f.stdlib.com'
    PORT = 443
    PATH = '/'

    attr_reader :host, :port, :path

    def self.proxy_method_missing(name)
      self.new[name]
    end

    def initialize(host = HOST, port = PORT, path = PATH, names=[])
      @host = host
      @port = port
      @path = path
      @names = names
    end

    def to_s
      @names.join('.')
    end

    def __append_version__(names, str)
      if /^@[A-Z0-9\-\.]+$/i !~ str then
        raise StandardError, "#{names.join('.')} invalid version: #{str}"
      end

      names + [str]
    end

    def __append_path__(names, str)
      if /^[A-Z0-9\-]+$/i !~ str then
        if str.include? '@' then
          raise StandardError, "#{names.join('.')} invalid name: #{str}, please specify versions and environments with [@version]"
        end

        raise StandardError, "#{names.join('.')} invalid name: #{str}"
      end
      names + [str]
    end

    def __append_lib_path__(names, str)
      names = if names.length then names + [] else [] end
      default_version = '@release'

      if names.length === 0 && str === '' then

        return names + [str]

      elsif names.length === 0 && (str.include? '.') then

        arr = if versionMatch = str.match(/^[^\.]+?\.[^\.]*?(\[@[^\[\]]*?\])(\.|$)/)
          version = versionMatch[1]
          version = version.gsub(/^\[?(.*?)\]?$/, '\1')
          str = str.gsub versionMatch[1], ''
          arr = str.split '.'
          arr[0...2] + [version] + (arr[2..-1] || [])
        else
          if str == '.' then [''] else str.split '.' end
        end

        while arr.length > 0 do
          names = __append_lib_path__(names, arr.shift)
        end
        return names

      elsif names.length === 2 && names[0] != '' then
        if str[0] === '@' then
          return __append_version__(names, str)

        else
          return __append_path__(__append_version__(names, default_version), str)

        end

      else

        return __append_path__(names, str)

      end

    end

    def [](name)
      method_missing(name)
    end

    def method_missing(name)
      LibGen.new(host, port, path, __append_lib_path__(@names, name.to_s))
    end

    def make_http_call(args, kwargs, name)
      body = JSON.generate({args: args, kwargs: kwargs})
      https = Net::HTTP.new(host, port)
      https.use_ssl = true
      https.verify_mode = OpenSSL::SSL::VERIFY_NONE
      req = Net::HTTP::Post.new "#{path}#{name}"
      req.add_field 'Content-Type', 'application/json'
      req.body = body
      https.request req
    end

    def exec!(*args)
      names = @names
      path = names[0...2].join('/') + (if names[2..-1] then names[2..-1].join('/') else '' end)
      kwargs = if args[-1].is_a? ::Hash then args.pop else {} end
      is_local = names[0].empty?

      begin
        args.each do |v|
          if ![nil, true, false, String, Numeric].any? {|t| v === t } then
              raise ArgumentError, "Lib.#{names.join('.')}: All arguments must be Boolean, Number, String or nil", caller[2..-1]
          end
        end

        if is_local then
          raise StandardError, "StdLib local execution currently unavailable in Ruby", caller
        end


	http_response = make_http_call(args, kwargs, path)

        headers = {}
        status = http_response.code.to_i
        http_response.each_header { |header, value| headers[header.downcase] = value }
        content_type = headers['content-type']
        response = http_response.body

        response = if content_type === 'application/json' then
          begin
           ('{['.include? http_response.body.to_s[0]) ? JSON.parse(http_response.body.to_s) : JSON.parse("[#{http_response.body_.to_s}]")[0]
          rescue
           nil
          end
        elsif content_type =~ /^text\/.*$/i then
          http_response.body.to_s
        end

        if status / 100 != 2 then
          raise StandardError, "#{response}", caller
        end

      rescue Exception => e

        if block_given? then
          yield e, nil
          return
        else
          raise e
        end

      end

      if block_given? then
        yield nil, response
        return
      else
        return response
      end

    end

    private :__append_version__
    private :__append_path__
    private :__append_lib_path__

  end

  private_constant :LibGen

end<commit_msg>fixed a bug and made the names more idiomatic<commit_after>require 'net/https'
require 'json'

module Lib
  class << self
    def [](name)
      LibGen.proxy_method_missing(name)
    end
  
    def method_missing(name)
      LibGen.proxy_method_missing(name)
    end
  end
  
  class LibGen

    HOST = 'f.stdlib.com'
    PORT = 443
    PATH = '/'

    attr_reader :host, :port, :path

    def self.proxy_method_missing(name)
      self.new[name]
    end

    def initialize(host = HOST, port = PORT, path = PATH, names=[])
      @host = host
      @port = port
      @path = path
      @names = names
    end

    def to_s
      @names.join('.')
    end

    def append_version(names, str)
      if /^@[A-Z0-9\-\.]+$/i !~ str then
        raise StandardError, "#{names.join('.')} invalid version: #{str}"
      end

      names + [str]
    end

    def append_path(names, str)
      if /^[A-Z0-9\-]+$/i !~ str then
        if str.include? '@' then
          raise StandardError, "#{names.join('.')} invalid name: #{str}, please specify versions and environments with [@version]"
        end

        raise StandardError, "#{names.join('.')} invalid name: #{str}"
      end
      names + [str]
    end

    def append_lib_path(names, str)
      names = if names.length > 0 then names + [] else [] end
      default_version = '@release'

      if names.length === 0 && str === '' then

        return names + [str]

      elsif names.length === 0 && (str.include? '.') then

        arr = if version_match = str.match(/^[^\.]+?\.[^\.]*?(\[@[^\[\]]*?\])(\.|$)/)
          version = version_match[1]
          version = version.gsub(/^\[?(.*?)\]?$/, '\1')
          str = str.gsub version_match[1], ''
          arr = str.split '.'
          arr[0...2] + [version] + (arr[2..-1] || [])
        else
          if str == '.' then [''] else str.split '.' end
        end

        while arr.length > 0 do
          names = append_lib_path(names, arr.shift)
        end
        return names

      elsif names.length === 2 && names[0] != '' then
        if str[0] === '@' then
          return append_version(names, str)

        else
          return append_path(append_version(names, default_version), str)

        end

      else

        return append_path(names, str)

      end

    end

    def [](name)
      method_missing(name)
    end

    def method_missing(name)
      LibGen.new(host, port, path, append_lib_path(@names, name.to_s))
    end

    def make_http_call(args, kwargs, name)
      body = JSON.generate({args: args, kwargs: kwargs})
      https = Net::HTTP.new(host, port)
      https.use_ssl = true
      https.verify_mode = OpenSSL::SSL::VERIFY_NONE
      req = Net::HTTP::Post.new "#{path}#{name}"
      req.add_field 'Content-Type', 'application/json'
      req.body = body
      https.request req
    end

    def exec!(*args)
      names = @names
      path = names[0...2].join('/') + (if names[2..-1] then names[2..-1].join('/') else '' end)
      kwargs = if args[-1].is_a? ::Hash then args.pop else {} end
      is_local = names[0].empty?

      begin
        args.each do |v|
          if ![nil, true, false, String, Numeric].any? {|t| v === t } then
              raise ArgumentError, "Lib.#{names.join('.')}: All arguments must be Boolean, Number, String or nil", caller[2..-1]
          end
        end

        if is_local then
          raise StandardError, "StdLib local execution currently unavailable in Ruby", caller
        end


	http_response = make_http_call(args, kwargs, path)

        headers = {}
        status = http_response.code.to_i
        http_response.each_header { |header, value| headers[header.downcase] = value }
        content_type = headers['content-type']
        response = http_response.body

        response = if content_type === 'application/json' then
          begin
           ('{['.include? http_response.body.to_s[0]) ? JSON.parse(http_response.body.to_s) : JSON.parse("[#{http_response.body_.to_s}]")[0]
          rescue
           nil
          end
        elsif content_type =~ /^text\/.*$/i then
          http_response.body.to_s
        end

        if status / 100 != 2 then
          raise StandardError, "#{response}", caller
        end

      rescue Exception => e

        if block_given? then
          yield e, nil
          return
        else
          raise e
        end

      end

      if block_given? then
        yield nil, response
        return
      else
        return response
      end

    end

    private :append_version
    private :append_path
    private :append_lib_path

  end

  private_constant :LibGen

end
{'fraction_non_alphanumeric': 0.13800194826279902, 'fraction_numerical': 0.007901288018183785, 'mean_word_length': 1.973929835854522, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, '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': '1811833', 'n_tokens_mistral': 2983, 'n_tokens_neox': 2866, 'n_words': 754}