diff --git a/Gemfile.lock b/Gemfile.lock index 905ed75..9e27685 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - random_gem (0.1.0) + random_gem (0.1.1) thor GEM diff --git a/lib/random_gem.rb b/lib/random_gem.rb index 21310c4..5a0941c 100644 --- a/lib/random_gem.rb +++ b/lib/random_gem.rb @@ -1,4 +1,5 @@ require "random_gem/cli" +require "random_gem/query" require "random_gem/randomizer" require "random_gem/version" @@ -15,4 +16,10 @@ def self.pick rescue Randomizer::Error raise Error.new end + + def self.pick_by_downloads(lower_limit: nil, upper_limit: nil) + Randomizer.new.perform( download_options: { min: lower_limit, max: upper_limit }.compact ) + rescue Randomizer::Error + raise Error.new + end end diff --git a/lib/random_gem/query.rb b/lib/random_gem/query.rb new file mode 100644 index 0000000..f853628 --- /dev/null +++ b/lib/random_gem/query.rb @@ -0,0 +1,22 @@ +require "random_gem/request" +require 'timeout' + +module RandomGem + class Query + attr_accessor :name, :min_download, :max_download + + def initialize(name, download_options: {}) + @name = name + @min_download = download_options.dig(:min) + @max_download = download_options.dig(:max) + end + + def to_parameter_string + str = "name: #{name}" + str += " downloads: >#{min_download}" if min_download + str += " downloads: <#{max_download}" if max_download + + str + end + end +end diff --git a/lib/random_gem/randomizer.rb b/lib/random_gem/randomizer.rb index 485785b..e55c05f 100644 --- a/lib/random_gem/randomizer.rb +++ b/lib/random_gem/randomizer.rb @@ -1,22 +1,26 @@ require "random_gem/request" +require "random_gem/query" require 'timeout' module RandomGem class Randomizer class Error < Exception; end - TIMEOUT = 30.freeze - PAGE = 100.freeze + TIMEOUT = 10.freeze + PAGE = 10.freeze - def perform - gems = Timeout.timeout(TIMEOUT, RandomGem::Randomizer::Error) { random_pick_loop } + def perform(download_options: {}) + gems = Timeout.timeout(TIMEOUT, RandomGem::Randomizer::Error) { random_pick_loop(download_options: download_options) } pick_single_gem(gems: gems) end private - def random_pick_loop + def random_pick_loop(download_options:) loop do - request = RandomGem::Request.new(keyword: random_letter, page: random_index(max: PAGE)) + query = Query.new(random_letter, download_options: download_options) + page = random_index(max: PAGE) + + request = RandomGem::Request.new(query, page) gems = request.do break gems if gems.length > 0 diff --git a/lib/random_gem/request.rb b/lib/random_gem/request.rb index 7f60e5d..59f3fd8 100644 --- a/lib/random_gem/request.rb +++ b/lib/random_gem/request.rb @@ -2,27 +2,29 @@ require 'rubygems' require 'open-uri' require 'json' +require 'random_gem/query' module RandomGem class Request HOST = "https://rubygems.org" PATH = "/api/v1/search" - attr_reader :keyword, :page + attr_reader :query, :page - def initialize(keyword:, page:) - @keyword = keyword + def initialize(query, page) + @query = query @page = page end def do uri = URI("#{HOST}#{PATH}") - params = { query: keyword, page: page } + params = { page: 1, query: query.to_parameter_string } uri.query = URI.encode_www_form(params) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true res = http.get(uri.request_uri) + respond(res: res) end