class LicenseScout::License

Attributes

project[R]
records[R]

Public Class Methods

new(path = nil) click to toggle source

@param path [String, nil] A path to give to Licensee to search for the license. Could be local path or GitHub URL.

# File lib/license_scout/license.rb, line 50
def initialize(path = nil)
  if path.nil?
    @project = nil
    @records = []
  else
    @project = Licensee.project(path, detect_readme: true)
    @records = []

    project.licenses.each_index do |i|
      record = Record.new(
        project.licenses[i].spdx_id,
        project.matched_files[i].filename,
        project.matched_files[i].content
      )

      # Favor records that have identified a license
      record.id.nil? ? @records.push(record) : @records.unshift(record)
    end
  end
end

Public Instance Methods

add_license(license_id, source, contents_url, options) click to toggle source

Capture a license that was specified in metadata

@param license_id [String] The license as specified in the metadata file @param source [String] Where we found the license info @param contents_url [String] Where we can find the contents of the license @param options [Hash] Options to control various behavior

@return [void]

# File lib/license_scout/license.rb, line 79
def add_license(license_id, source, contents_url, options)
  content = license_content(license_id, contents_url)
  @records.push(Record.new(license_id, source, content, options))
end
is_allowed?() click to toggle source

@return [Boolean] Whether or not the license(s) are allowed

# File lib/license_scout/license.rb, line 85
def is_allowed?
  (records.map(&:parsed_expression).flatten.compact & LicenseScout::Config.allowed_licenses).any?
end
is_flagged?() click to toggle source

@return [Boolean] Whether or not the license(s) are flagged

# File lib/license_scout/license.rb, line 90
def is_flagged?
  (records.map(&:parsed_expression).flatten.compact & LicenseScout::Config.flagged_licenses).any?
end
undetermined?() click to toggle source

@return [Boolean] Whether we were unable to determine a license

# File lib/license_scout/license.rb, line 95
def undetermined?
  (records.map(&:parsed_expression).flatten.compact).empty?
end

Private Instance Methods

license_content(license_id, contents_url) click to toggle source
# File lib/license_scout/license.rb, line 101
def license_content(license_id, contents_url)
  if contents_url.nil?
    nil
  else
    new_url = raw_github_url(contents_url)

    begin
      LicenseScout::Log.debug("[license] Pulling license content for #{license_id} from #{new_url}")
      URI.open(new_url).read
    rescue RuntimeError => e
      if e.message =~ /redirection forbidden/
        m = /redirection forbidden:\s+(.+)\s+->\s+(.+)/.match(e.message)
        new_https_url = m[2].gsub("http://", "https://")

        LicenseScout::Log.debug("[license] Retrying download of #{license_id} from #{new_https_url}")
        license_content(license_id, new_https_url)
      else
        raise e
      end
    rescue
      LicenseScout::Log.warn("[license] Unable to download license for #{license_id} from #{new_url}")
      nil
    end
  end
end
raw_github_url(url) click to toggle source
# File lib/license_scout/license.rb, line 127
def raw_github_url(url)
  case url
  when %r{github.com/(.+)/blob/(.+)}
    "https://raw.githubusercontent.com/#{$1}/#{$2}"
  else
    url
  end
end