Class: Iconmap::Jsdelivr
- Inherits:
-
Object
show all
- Defined in:
- lib/iconmap/jsdelivr.rb
Defined Under Namespace
Classes: Error, HTTPError
Instance Attribute Summary collapse
Instance Method Summary
collapse
Instance Attribute Details
#cdn_uri ⇒ Object
Returns the value of attribute cdn_uri.
15
16
17
|
# File 'lib/iconmap/jsdelivr.rb', line 15
def cdn_uri
@cdn_uri
end
|
#data_uri ⇒ Object
Returns the value of attribute data_uri.
15
16
17
|
# File 'lib/iconmap/jsdelivr.rb', line 15
def data_uri
@data_uri
end
|
Instance Method Details
#download_url(package, version, path) ⇒ Object
29
30
31
|
# File 'lib/iconmap/jsdelivr.rb', line 29
def download_url(package, version, path)
"#{self.class.cdn_uri}/npm/#{package}@#{version}/#{path}"
end
|
#fetch_file(url) ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/iconmap/jsdelivr.rb', line 33
def fetch_file(url)
uri = URI(url)
response = Net::HTTP.get_response(uri)
case response.code
when '200' then response.body.dup.force_encoding('UTF-8')
else raise HTTPError, "Failed to download #{url} (#{response.code})"
end
rescue StandardError => e
raise e if e.is_a?(HTTPError)
raise HTTPError, "Unexpected transport error (#{e.class}: #{e.message})"
end
|
#resolve_version(package) ⇒ Object
19
20
21
22
23
24
25
26
27
|
# File 'lib/iconmap/jsdelivr.rb', line 19
def resolve_version(package)
uri = self.class.data_uri.dup
uri.path = "/v1/packages/npm/#{package}/resolved"
response = get_json(uri)
JSON.parse(response)['version']
rescue JSON::ParserError
nil
end
|