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://data.jsdelivr.com/v1"
HEADERS =
{
  "x-algo""lia-app""lication-id"=>"OFCNC""OG2CU",
  "x-algo""lia-api""-key"=>"f54e21fa3a2""a0160595bb05""8179bfb1e",
}

Instance Attribute Summary

Attributes inherited from Base

#debug_mode

Instance Method Summary collapse

Methods inherited from Base

#download, inherited, #initialize

Constructor Details

This class inherits a constructor from CDNGet::Base

Instance Method Details

#find(library) ⇒ Object



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/cdnget.rb', line 328

def find(library)
  validate(library, nil)
  url = "https://ofcncog2cu-dsn.algolia.net/1/indexes/npm-search/#{library}"
  uri = URI.parse(url)
  begin
    json = HttpConnection.open(uri, HEADERS) {|http| http.get(uri) }
  rescue HttpError
    raise CommandError, "#{library}: Library not found."
  end
  dict1 = JSON.load(json)
  _debug_print(dict1)
  #
  json = fetch("#{API_URL}/package/npm/#{library}")
  dict2 = JSON.load(json)
  _debug_print(dict2)
  #
  d = dict1
  return {
    name:      d['name'],
    desc:      d['description'],
    #versions: d['versions'].collect {|k,v| k },
    versions:  dict2['versions'],
    tags:      (d['keywords'] || []).join(", "),
    site:      d['homepage'],
    info:      File.join(SITE_URL, "/package/npm/#{library}"),
    license:   d['license'],
  }
end

#get(library, version) ⇒ Object



357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/cdnget.rb', line 357

def get(library, version)
  validate(library, version)
  url = File.join(API_URL, "/package/npm/#{library}@#{version}/flat")
  begin
    json = fetch(url, library)
  rescue CommandError
    raise CommandError.new("#{library}@#{version}: Library or version not found.")
  end
  jdata   = JSON.load(json)
  files   = jdata["files"].collect {|d| d["name"] }
  baseurl = "https://cdn.jsdelivr.net/npm/#{library}@#{version}"
  _debug_print(jdata)
  #
  dict = find(library)
  dict.delete(:versions)
  dict.update({
    version: version,
    info:    File.join(SITE_URL, "/package/npm/#{library}?version=#{version}"),
    npmpkg:  "https://registry.npmjs.org/#{library}/-/#{library}-#{version}.tgz",
    urls:    files.collect {|x| baseurl + x },
    files:   files,
    baseurl: baseurl,
    default: jdata["default"],
    destdir: "#{library}@#{version}",
  })
  return dict
end

#listObject



303
304
305
# File 'lib/cdnget.rb', line 303

def list()
  return nil    # nil means that this CDN can't list libraries without pattern
end

#search(pattern) ⇒ Object



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/cdnget.rb', line 307

def search(pattern)
  form_data = {
    query:        pattern,
    page:         "0",
    hitsPerPage:  "1000",
    attributesToHighlight: '[]',
    attributesToRetrieve:  '["name","description","version"]'
  }
  payload = JSON.dump({"params"=>URI.encode_www_form(form_data)})
  url = "https://ofcncog2cu-3.algolianet.com/1/indexes/npm-search/query"
  uri = URI.parse(url)
  json = HttpConnection.open(uri, HEADERS) {|http| http.post(uri, payload) }
  jdata = JSON.load(json)
  _debug_print(jdata)
  return jdata["hits"].select {|d|
    File.fnmatch(pattern, d["name"], File::FNM_CASEFOLD)
  }.collect {|d|
    {name: d["name"], desc: d["description"], version: d["version"]}
  }
end