Class: Pbom::Package

Inherits:
Object
  • Object
show all
Defined in:
lib/pbom/package.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#detailsObject (readonly)

Returns the value of attribute details.



7
8
9
# File 'lib/pbom/package.rb', line 7

def details
  @details
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/pbom/package.rb', line 7

def name
  @name
end

#purlObject (readonly)

Returns the value of attribute purl.



7
8
9
# File 'lib/pbom/package.rb', line 7

def purl
  @purl
end

#versionObject (readonly)

Returns the value of attribute version.



7
8
9
# File 'lib/pbom/package.rb', line 7

def version
  @version
end

Instance Method Details

#authorsObject



71
72
73
# File 'lib/pbom/package.rb', line 71

def authors
  Array(@details['maintainers']).map{ |m| m['name'] || m['login'] }.join(', ')
end

#download_citation_cffObject



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_detailsObject



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_entryObject



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 = {#{authors}},
      title = {{#{title}}},
      version = {#{version}},
      url = {#{url}},
      license = {#{licenses}},
      year = {#{year}},
      month = {#{month}},
      howpublished = {#{howpublished}}
    }
  BIB
end

#howpublishedObject



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

#licensesObject



59
60
61
# File 'lib/pbom/package.rb', line 59

def licenses
  @details['licenses']
end

#matches?(other_purl) ⇒ Boolean

Returns:

  • (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

#monthObject



67
68
69
# File 'lib/pbom/package.rb', line 67

def month
  (@details['latest_release_published_at']&.split('-') || [])[1]
end

#render_citation_cffObject



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

#titleObject



43
44
45
# File 'lib/pbom/package.rb', line 43

def title
  "#{@name} (#{@version})"
end

#to_citeObject



51
52
53
# File 'lib/pbom/package.rb', line 51

def to_cite
  "\\cite{#{to_reference}}"
end

#to_referenceObject



47
48
49
# File 'lib/pbom/package.rb', line 47

def to_reference
  [@type, @namespace, @name].compact.join(':')
end

#urlObject



55
56
57
# File 'lib/pbom/package.rb', line 55

def url
  @details['homepage'] || @details['repository_url'] || @details['registry_url'] 
end

#yearObject



63
64
65
# File 'lib/pbom/package.rb', line 63

def year
  @details['latest_release_published_at']&.split('-')&.first
end