<commit_before>require 'json'
require 'open-uri'

module Gem::Search
  class CLI
    BASE_URL   = 'https://rubygems.org'
    SEARCH_URL = "#{BASE_URL}/api/v1/search.json?query="
    DEFAULT_RULED_LINE_SIZE = [50, 8, 9]

    def search(query, opt_sort='name')
      return unless query

      opt_sort ||= 'name'
      url = "#{SEARCH_URL}#{query}"
      
      begin  
        open(url) do |f|
          gems = JSON.parse(f.read)
          if gems.size.zero?
            puts 'We did not find results.'
            return
          end
          fmt_size = ruled_line_size(gems)
          gems_sort!(gems, opt_sort)
          render_header(fmt_size)
          render_body(gems, fmt_size)
        end
      rescue
        puts 'An unexpected Network error has occurred.'
      end
    end

    private
      def ruled_line_size(gems)
        line_size = DEFAULT_RULED_LINE_SIZE.dup
        max_name_size = gems.map { |gem| "#{gem['name']} (#{gem['version']})".size }.max
        line_size[0] =  max_name_size if max_name_size > line_size[0]
        line_size
      end

      def gems_sort!(gems, opt_sort)
        if opt_sort == 'name'
          gems.sort!{ |x,y| x[opt_sort] <=> y[opt_sort] }
        else<commit_msg>support a connection with proxy<commit_after>require 'json'
require 'open-uri'

module Gem::Search
  class CLI
    BASE_URL   = 'https://rubygems.org'
    SEARCH_URL = "#{BASE_URL}/api/v1/search.json?query="
    DEFAULT_RULED_LINE_SIZE = [50, 8, 9]

    def search(query, opt_sort='name')
      return unless query

      opt_sort ||= 'name'
      url = "#{SEARCH_URL}#{query}"

      begin
        open_uri(url) do |f|
          gems = JSON.parse(f.read)
          if gems.size.zero?
            puts 'We did not find results.'
            return
          end
          fmt_size = ruled_line_size(gems)
          gems_sort!(gems, opt_sort)
          render_header(fmt_size)
          render_body(gems, fmt_size)
        end
      rescue
        puts 'An unexpected Network error has occurred.'
      end
    end

    private
      def open_uri(url, &block)
        option = {}
        proxy = URI.parse(url).find_proxy
        if proxy
          if proxy.user && proxy.password
            option[:proxy_http_basic_authentication] = [proxy, proxy.usesr, proxy.password]
          end
        end
        open(url, option, &block)
      end

      def ruled_line_size(gems)
        line_size = DEFAULT_RULED_LINE_SIZE.dup
        max_name_size = gems.map { |gem| "#{gem['name']} (#{gem['version']})".size }.max
        line_size[0] =  max_name_size if max_name_size > line_size[0]
        line_size
      end

      def gems_sort!(gems, opt_sort)
        if opt_sort == 'name'
          gems.sort!{ |x,y| x[opt_sort] <=> y[opt_sort] }
        else
{'fraction_non_alphanumeric': 0.113882863340564, 'fraction_numerical': 0.005061460592913955, 'mean_word_length': 2.060840707964602, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 2, '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': '1739321', 'n_tokens_mistral': 952, 'n_tokens_neox': 917, 'n_words': 202}