Class: Gemstar::NpmMetadata

Inherits:
Object
  • Object
show all
Defined in:
lib/gemstar/npm_metadata.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(package_name) ⇒ NpmMetadata

Returns a new instance of NpmMetadata.



7
8
9
# File 'lib/gemstar/npm_metadata.rb', line 7

def initialize(package_name)
  @gem_name = package_name
end

Instance Attribute Details

#gem_nameObject (readonly)

Returns the value of attribute gem_name.



11
12
13
# File 'lib/gemstar/npm_metadata.rb', line 11

def gem_name
  @gem_name
end

Instance Method Details

#cache_keyObject



13
14
15
# File 'lib/gemstar/npm_metadata.rb', line 13

def cache_key
  "npm-#{gem_name}"
end

#changelog_sections(versions: nil, cache_only: false, force_refresh: false) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/gemstar/npm_metadata.rb', line 67

def changelog_sections(versions: nil, cache_only: false, force_refresh: false)
  requested_versions = Array(versions).compact
  changelog = Gemstar::ChangeLog.new(self)
  if requested_versions.empty?
    changelog.sections(cache_only: cache_only, force_refresh: force_refresh)
  else
    changelog.sections_for_versions(requested_versions, cache_only: cache_only, force_refresh: force_refresh)
  end
end

#discover_github_tag_sections?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/gemstar/npm_metadata.rb', line 83

def discover_github_tag_sections?
  true
end

#github_tag_candidates(version) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/gemstar/npm_metadata.rb', line 87

def github_tag_candidates(version)
  raw = version.to_s
  candidates = [raw, (raw.start_with?("v") ? raw : "v#{raw}")]

  package_name = gem_name.to_s
  unless package_name.empty?
    candidates << "#{package_name}@#{raw}"
    candidates << "#{package_name}@v#{raw}" unless raw.start_with?("v")

    short_name = package_name.split("/").last
    if short_name && short_name != package_name
      candidates << "#{short_name}@#{raw}"
      candidates << "#{short_name}@v#{raw}" unless raw.start_with?("v")
    end
  end

  candidates.uniq
end

#github_tag_matches?(tag_name) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/gemstar/npm_metadata.rb', line 106

def github_tag_matches?(tag_name)
  decoded = URI.decode_www_form_component(tag_name.to_s.split("?").first.to_s)
  package_name = gem_name.to_s
  return true if package_name.empty?

  if decoded.include?("@")
    prefix = decoded.sub(/@v?\d[\w.\-]*\z/i, "")
    return true if prefix == package_name

    short_name = package_name.split("/").last
    return true if short_name && prefix == short_name

    return false
  end

  true
end

#meta(cache_only: false, force_refresh: false) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/gemstar/npm_metadata.rb', line 17

def meta(cache_only: false, force_refresh: false)
  return @meta if !cache_only && defined?(@meta)

  json = if cache_only
    Cache.peek(cache_key)
  else
    url = "https://registry.npmjs.org/#{URI.encode_www_form_component(gem_name)}"
    Cache.fetch(cache_key, force: force_refresh) do
      URI.open(url, read_timeout: 8).read
    end
  end

  parsed = begin
    JSON.parse(json) if json
  rescue
    nil
  end

  normalized = normalize_meta(parsed)
  @meta = normalized unless cache_only
  normalized
end

#repo_uri(cache_only: false, force_refresh: false) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/gemstar/npm_metadata.rb', line 40

def repo_uri(cache_only: false, force_refresh: false)
  resolved_meta = meta(cache_only: cache_only, force_refresh: force_refresh)
  return nil unless resolved_meta

  return @repo_uri if !cache_only && defined?(@repo_uri)

  repo = begin
    uri = resolved_meta["source_code_uri"]
    uri ||= resolved_meta["homepage_uri"] if resolved_meta["homepage_uri"].to_s.include?("github.com")
    uri ||= ""

    uri = uri.sub(/\Agit\+/, "")
    uri = uri.sub("git://", "https://")
    uri = uri.sub("http://", "https://")
    uri = uri.gsub(/\.git$/, "")

    if uri.include?("github.com")
      uri = uri[%r{\Ahttps?://github\.com/[^/]+/[^/]+}] || uri
    end

    uri
  end

  @repo_uri = repo unless cache_only
  repo
end

#warm_cache(versions: nil) ⇒ Object



77
78
79
80
81
# File 'lib/gemstar/npm_metadata.rb', line 77

def warm_cache(versions: nil)
  meta
  repo_uri
  changelog_sections(versions: versions)
end