Class: LicenseFinder::NpmPackage

Inherits:
Package
  • Object
show all
Defined in:
lib/license_finder/packages/npm_package.rb

Defined Under Namespace

Classes: Group, Identifier, PackageJson

Instance Attribute Summary collapse

Attributes inherited from Package

#authors, #children, #description, #homepage, #install_path, #license_names_from_spec, #logger, #manual_approval, #name, #parents, #summary, #version

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Package

#<=>, #activations, #approved?, #approved_manually!, #approved_manually?, #decide_on_license, #eql?, #hash, #license_files, license_names_from_standard_spec, #licenses, #licenses_from_spec, #licensing, #log_activation, #missing?, #notice_files, #permitted!, #permitted?, #restricted!, #restricted?

Constructor Details

#initialize(npm_json) ⇒ NpmPackage

Returns a new instance of NpmPackage.



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/license_finder/packages/npm_package.rb', line 67

def initialize(npm_json)
  @json = npm_json
  @identifier = Identifier.from_hash(npm_json)
  @dependencies = deps_from_json
  super(@identifier.name,
        @identifier.version,
        description: npm_json['description'],
        homepage: npm_json['homepage'],
        authors: author_names,
        spec_licenses: Package.license_names_from_standard_spec(npm_json),
        install_path: npm_json['path'],
        children: @dependencies.map(&:name))
end

Instance Attribute Details

#dependenciesObject

Returns the value of attribute dependencies.



5
6
7
# File 'lib/license_finder/packages/npm_package.rb', line 5

def dependencies
  @dependencies
end

#groupsObject

Returns the value of attribute groups.



5
6
7
# File 'lib/license_finder/packages/npm_package.rb', line 5

def groups
  @groups
end

#identifierObject

Returns the value of attribute identifier.



5
6
7
# File 'lib/license_finder/packages/npm_package.rb', line 5

def identifier
  @identifier
end

#jsonObject

Returns the value of attribute json.



5
6
7
# File 'lib/license_finder/packages/npm_package.rb', line 5

def json
  @json
end

Class Method Details

.packages_from_json(npm_json, package_path) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/license_finder/packages/npm_package.rb', line 8

def packages_from_json(npm_json, package_path)
  @packages = flattened_dependencies(npm_json)
  package_json = PackageJson.new(package_path)
  populate_groups(package_json)
  @packages.reject! do |_identifier, package|
    package.name.empty? &&
      package.version.empty? &&
      package.licenses.length == 1 &&
      package.licenses.first.name == 'unknown'
  end
  @packages.values
end

Instance Method Details

#==(other) ⇒ Object



101
102
103
# File 'lib/license_finder/packages/npm_package.rb', line 101

def ==(other)
  other.is_a?(NpmPackage) && @identifier == other.identifier
end

#author_name(author) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/license_finder/packages/npm_package.rb', line 88

def author_name(author)
  if author.instance_of?(String)
    author_name_from_combined(author)
  else
    author['name']
  end
end

#author_name_from_combined(author) ⇒ Object



96
97
98
99
# File 'lib/license_finder/packages/npm_package.rb', line 96

def author_name_from_combined(author)
  matches = author.match /^(.*?)\s*(<.*?>)?\s*(\(.*?\))?\s*$/
  matches[1]
end

#author_namesObject



81
82
83
84
85
86
# File 'lib/license_finder/packages/npm_package.rb', line 81

def author_names
  names = []
  names.push(author_name(@json['author'])) unless @json['author'].nil?
  names += @json['contributors'].map { |c| author_name(c) } if @json['contributors'].is_a?(Array)
  names.join(', ')
end

#package_managerObject



109
110
111
# File 'lib/license_finder/packages/npm_package.rb', line 109

def package_manager
  'Npm'
end

#package_urlObject



113
114
115
# File 'lib/license_finder/packages/npm_package.rb', line 113

def package_url
  "https://www.npmjs.com/package/#{CGI.escape(name)}/v/#{CGI.escape(version)}"
end

#to_sObject



105
106
107
# File 'lib/license_finder/packages/npm_package.rb', line 105

def to_s
  @identifier.to_s
end