class GemsBond::Spy::All

Inspects gems and outputs the result in HTML and CSV files

Constants

RETRIES

Number of fetch retries before skipping gem

Public Instance Methods

call() click to toggle source

Fetches and scores gems then prints result @return [void]

# File lib/gems_bond/spy/all.rb, line 19
def call
  timer do
    fetch_gems_data
    GemsBond::Printers::HTML.new(gems).call
    GemsBond::Printers::CSV.new(gems).call
  end
end

Private Instance Methods

fetch_gems_data() click to toggle source

Fetches data for each gem @return [void] (mutate gems) @note use concurrency to fetch quickly fetch data from APIs

# File lib/gems_bond/spy/all.rb, line 58
def fetch_gems_data
  puts "Fetching data for..."
  # slice 100 to avoid too many requests on RubyGems and GitHub APIs
  gems.each_slice(100) do |batch|
    each_concurrently(batch) do |gem|
      begin
        retries ||= 0
        # set verbose to true to stdout the gem name
        gem.prepare_data(verbose: true)
      # rescue SocketError, Faraday::ConnectionFailed...
      rescue StandardError
        (retries += 1) <= RETRIES ? retry : nil
      end
    end
  end
end
gem_thread(gem) click to toggle source

Starts a thread to process the given gem @param gem [GemsBond::Gem] gem to process @note if there is a connection/API error

retry or rescue if too many retries
# File lib/gems_bond/spy/all.rb, line 79
def gem_thread(gem)
  Thread.new do
    begin
      retries ||= 0
      # set verbose to true to stdout the gem name
      gem.prepare_data(verbose: true)
    # rescue SocketError, Faraday::ConnectionFailed...
    rescue StandardError
      (retries += 1) <= RETRIES ? retry : nil
    end
  end
end
gems() click to toggle source

Returns list of gems to spy @return [Array<GemsBond::Gem>]

# File lib/gems_bond/spy/all.rb, line 48
def gems
  @gems ||=
    Bundler.load.current_dependencies.map do |dependency|
      GemsBond::Gem.new(dependency)
    end
end
gems_count() click to toggle source

Returns number of gems @return [Integer]

# File lib/gems_bond/spy/all.rb, line 31
def gems_count
  @gems_count ||= gems.count
end
timer() { || ... } click to toggle source

Starts a timer and executes given block @yieldparam [Proc] code to execute and time @return [void] (stdout)

# File lib/gems_bond/spy/all.rb, line 38
def timer
  start_at = Time.now
  yield
  seconds = Time.now - start_at
  time_per_gem_text = "#{(seconds / Float(gems_count)).round(2)} second(s) per gem"
  puts "\nIt took #{seconds} second(s) to spy #{gems_count} gem(s) (#{time_per_gem_text})."
end