Class: CDNGet::JSDelivr

Inherits:
Base
  • Object
show all
Defined in:
lib/cdnget.rb

Constant Summary collapse

CODE =
"jsdelivr"
SITE_URL =
"https://www.jsdelivr.com/"
API_URL =
"https://api.jsdelivr.com/v1/jsdelivr/libraries"

Instance Method Summary collapse

Methods inherited from Base

#download, inherited

Instance Method Details

#find(library) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/cdnget.rb', line 200

def find(library)
  validate(library, nil)
  json = fetch("#{API_URL}?name=#{library}&fields=name,description,homepage,versions")
  arr = JSON.load(json)
  d = arr.first  or
    raise CommandError.new("#{library}: Library not found.")
  return {
    name:     d['name'],
    desc:     d['description'],
    site:     d['homepage'],
    versions: d['versions'],
  }
end

#get(library, version) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/cdnget.rb', line 214

def get(library, version)
  validate(library, version)
  baseurl = "https://cdn.jsdelivr.net/#{library}/#{version}"
  url = "#{API_URL}/#{library}/#{version}"
  json  = fetch("#{API_URL}/#{library}/#{version}")
  files = JSON.load(json)
  ! files.empty?  or
    raise CommandError.new("#{library}: Library not found.")
  urls  = files.collect {|x| "#{baseurl}/#{x}" }
  return {
    name:     library,
    version:  version,
    urls:     urls,
    files:    files,
    baseurl:  baseurl,
  }
end

#listObject



192
193
194
195
196
197
198
# File 'lib/cdnget.rb', line 192

def list
  json = fetch("#{API_URL}?fields=name,description,homepage")
  arr = JSON.load(json)
  return arr.collect {|d|
    {name: d["name"], desc: d["description"], site: d["homepage"] }
  }
end