Class: Pbom::Package
- Inherits:
-
Object
- Object
- Pbom::Package
- Defined in:
- lib/pbom/package.rb
Instance Attribute Summary collapse
-
#details ⇒ Object
readonly
Returns the value of attribute details.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#purl ⇒ Object
readonly
Returns the value of attribute purl.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Instance Method Summary collapse
- #authors ⇒ Object
- #download_citation_cff ⇒ Object
- #fetch_details ⇒ Object
- #generate_bib_entry ⇒ Object
- #howpublished ⇒ Object
-
#initialize(purl) ⇒ Package
constructor
A new instance of Package.
- #licenses ⇒ Object
- #matches?(other_purl) ⇒ Boolean
- #month ⇒ Object
- #render_citation_cff ⇒ Object
- #title ⇒ Object
- #to_cite ⇒ Object
- #to_reference ⇒ Object
- #url ⇒ Object
- #year ⇒ Object
Constructor Details
#initialize(purl) ⇒ Package
Returns a new instance of Package.
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/pbom/package.rb', line 9 def initialize(purl) @purl = purl parse_purl = PackageURL.parse(purl) @type = parse_purl.type @namespace = parse_purl.namespace @name = parse_purl.name @version = parse_purl.version @qualifiers = parse_purl.qualifiers @subpath = parse_purl.subpath @details = {} @citation = nil fetch_details end |
Instance Attribute Details
#details ⇒ Object (readonly)
Returns the value of attribute details.
7 8 9 |
# File 'lib/pbom/package.rb', line 7 def details @details end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/pbom/package.rb', line 7 def name @name end |
#purl ⇒ Object (readonly)
Returns the value of attribute purl.
7 8 9 |
# File 'lib/pbom/package.rb', line 7 def purl @purl end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
7 8 9 |
# File 'lib/pbom/package.rb', line 7 def version @version end |
Instance Method Details
#authors ⇒ Object
71 72 73 |
# File 'lib/pbom/package.rb', line 71 def Array(@details['maintainers']).map{ |m| m['name'] || m['login'] }.join(', ') end |
#download_citation_cff ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/pbom/package.rb', line 83 def download_citation_cff if @details['repo_metadata'] && @details['repo_metadata']['metadata'] && @details['repo_metadata']['metadata']['files'] && @details['repo_metadata']['metadata']['files']['citation'] path = @details['repo_metadata']['metadata']['files']['citation'] return unless path.end_with?('.cff') puts "Downloading #{path} from #{@details['repo_metadata']['full_name']}..." branch = @details['repo_metadata']['default_branch'] || 'master' url = "https://raw.githubusercontent.com/#{@details['repo_metadata']['full_name']}/#{branch}/#{path}" response = Faraday.get(url) if response.status == 200 @citation = response.body end end end |
#fetch_details ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/pbom/package.rb', line 23 def fetch_details puts "Looking up #{@purl}..." response = Faraday.get("https://packages.ecosyste.ms/api/v1/packages/lookup?purl=#{@purl}") if response.status == 200 data = JSON.parse(response.body) pkg = data.first return if pkg.nil? @details = pkg download_citation_cff end end |
#generate_bib_entry ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/pbom/package.rb', line 109 def generate_bib_entry return render_citation_cff if @citation <<~BIB @software{#{to_reference}, author = {#{}}, title = {{#{title}}}, version = {#{version}}, url = {#{url}}, license = {#{licenses}}, year = {#{year}}, month = {#{month}}, howpublished = {#{howpublished}} } BIB end |
#howpublished ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/pbom/package.rb', line 75 def howpublished if @details['registry'] "Published on #{@details['registry']['url']}" else "Retrieved from #{purl}" end end |
#licenses ⇒ Object
59 60 61 |
# File 'lib/pbom/package.rb', line 59 def licenses @details['licenses'] end |
#matches?(other_purl) ⇒ Boolean
35 36 37 38 39 40 41 |
# File 'lib/pbom/package.rb', line 35 def matches?(other_purl) other_parse_purl = PackageURL.parse(other_purl) return false if @type != other_parse_purl.type return false if @namespace != other_parse_purl.namespace return false if @name != other_parse_purl.name true end |
#month ⇒ Object
67 68 69 |
# File 'lib/pbom/package.rb', line 67 def month (@details['latest_release_published_at']&.split('-') || [])[1] end |
#render_citation_cff ⇒ Object
100 101 102 103 104 105 106 107 |
# File 'lib/pbom/package.rb', line 100 def render_citation_cff begin cff = CFF::Index.read(@citation) cff.to_bibtex rescue StandardError nil end end |
#title ⇒ Object
43 44 45 |
# File 'lib/pbom/package.rb', line 43 def title "#{@name} (#{@version})" end |
#to_cite ⇒ Object
51 52 53 |
# File 'lib/pbom/package.rb', line 51 def to_cite "\\cite{#{to_reference}}" end |
#to_reference ⇒ Object
47 48 49 |
# File 'lib/pbom/package.rb', line 47 def to_reference [@type, @namespace, @name].compact.join(':') end |
#url ⇒ Object
55 56 57 |
# File 'lib/pbom/package.rb', line 55 def url @details['homepage'] || @details['repository_url'] || @details['registry_url'] end |
#year ⇒ Object
63 64 65 |
# File 'lib/pbom/package.rb', line 63 def year @details['latest_release_published_at']&.split('-')&.first end |