Sha256: aea7d3c00d333a379e2e4d46c15cd253cbd933460084b40de1d5ef4e144adbc1

Contents?: true

Size: 1.01 KB

Versions: 9

Compression:

Stored size: 1.01 KB

Contents

require "nokogiri"
require "open-uri"

module Rgversion
  class Spider
    def initialize(gems, selector)
      raise NoArguments, error_messages(:arguments) if gems.blank?
      @gems = gems
      @selector = selector
    end

    def walk
      lines = []
      errors = []
      @gems.each do |gem|
        begin
          lines << grab_version(gem)
        rescue OpenURI::HTTPError
          errors << "#{gem} not found"
        end
      end
      { gems: lines, errors: errors }
    end

    private

    def error_messages(key)
      {
        arguments: "No gems passed as arguments. Try to use like this:\nrgversion rails sinatra",
        selector: "Selector #{@selector} doesn't exists on the page.\nRgversion should be updated."
      }[key].red
    end

    def grab_version(gem)
      gem_url = "https://rubygems.org/gems/#{gem}"
      gem_page = Nokogiri::HTML(open(gem_url))
      raise WrongSelector, error_messages(:selector) if gem_page.at(@selector).nil?
      gem_page.at(@selector)["value"]
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rgversion-1.1.7.rc1 lib/rgversion/spider.rb
rgversion-1.1.7.beta2 lib/rgversion/spider.rb
rgversion-1.1.7.beta1 lib/rgversion/spider.rb
rgversion-1.1.6 lib/rgversion/spider.rb
rgversion-1.1.5 lib/rgversion/spider.rb
rgversion-1.1.4 lib/rgversion/spider.rb
rgversion-1.1.3 lib/rgversion/spider.rb
rgversion-1.1.2 lib/rgversion/spider.rb
rgversion-1.1.1 lib/rgversion/spider.rb