class GemUpdater::SourcePageParser

SourcePageParser is responsible for parsing a source page where the gem code is hosted.

Constants

CHANGELOG_NAMES
HOSTS
MARKUP_FILES

Attributes

uri[R]
version[R]

Public Class Methods

new(url: nil, version: nil) click to toggle source

@param url [String] url of page @param version [String] version of gem

# File lib/gem_updater/source_page_parser.rb, line 24
def initialize(url: nil, version: nil)
  @uri     = correct_uri(url)
  @version = version
end

Public Instance Methods

changelog() click to toggle source

Get the changelog in an uri.

@return [String, nil] URL of changelog

# File lib/gem_updater/source_page_parser.rb, line 32
def changelog
  return unless uri

  Bundler.ui.warn "Looking for a changelog in #{uri}"
  find_changelog(Nokogiri::HTML(uri.open))
rescue OpenURI::HTTPError # Uri points to nothing
  log_error("Cannot find #{uri}")
rescue Errno::ETIMEDOUT # timeout
  log_error("#{uri} is down")
rescue ArgumentError => e # x-oauth-basic raises userinfo not supported. [RFC3986]
  log_error(e)
end

Private Instance Methods

changelog_may_contain_anchor?(file_name) click to toggle source

Some documents like the one written in markdown may contain a direct anchor to specific version.

@param file_name [String] file name of changelog @return [Boolean] true if file may contain an anchor

# File lib/gem_updater/source_page_parser.rb, line 103
def changelog_may_contain_anchor?(file_name)
  MARKUP_FILES.include?(File.extname(file_name))
end
changelog_names() click to toggle source

List possible names for a changelog since humans may have many many ways to call it.

@return [Array] list of possible names

# File lib/gem_updater/source_page_parser.rb, line 92
def changelog_names
  CHANGELOG_NAMES.flat_map do |name|
    [name, name.upcase, name.capitalize]
  end.uniq
end
correct_uri(url) click to toggle source

Some gems have 'github.com' as URI which will redirect to https leading `open_uri` to crash.

@param url [String] the url to parse @return [URI] valid URI

# File lib/gem_updater/source_page_parser.rb, line 53
def correct_uri(url)
  return unless url.is_a?(String) && !url.empty?

  uri = URI.parse(url)
  uri.scheme == 'http' ? known_https(uri) : uri
end
find_changelog(doc) click to toggle source

Try to find where changelog might be.

@param doc [Nokogiri::XML::Element] document of source page

# File lib/gem_updater/source_page_parser.rb, line 81
def find_changelog(doc)
  case uri.host
  when 'github.com'
    GitHubParser.new(doc, version).changelog
  end
end
known_https(uri) click to toggle source

Some uris are not https, but we know they should be, in which case we have an https redirection which is not properly handled by open-uri

@param uri [URI::HTTP] @return [URI::HTTPS|URI::HTTP]

# File lib/gem_updater/source_page_parser.rb, line 66
def known_https(uri)
  case uri.host
  when HOSTS[:github]
    # remove possible subdomain like 'wiki.github.com'
    URI "https://github.com#{uri.path}"
  when HOSTS[:bitbucket], HOSTS[:rubygems]
    URI "https://#{uri.host}#{uri.path}"
  else
    uri
  end
end
log_error(error_message) click to toggle source
# File lib/gem_updater/source_page_parser.rb, line 107
def log_error(error_message)
  Bundler.ui.error error_message
  false
end