Class: CDNGet::JSDelivr
Constant Summary collapse
- CODE =
"jsdelivr"
- SITE_URL =
"https://www.jsdelivr.com/"
- API_URL =
API_URL = “api.jsdelivr.com/v1/jsdelivr/libraries”
"https://data.jsdelivr.com/v1"
- CDN_URL =
"https://cdn.jsdelivr.net/npm"
- HEADERS =
{ "x-algo""lia-app""lication-id"=>"OFCNC""OG2CU", "x-algo""lia-api""-key"=>"f54e21fa3a2""a0160595bb05""8179bfb1e", }
- LIBRARY_REXP =
/\A([-.\w]+|\@[-\w]+\/[-.\w]+)\z/
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #find(library) ⇒ Object
- #get(library, version) ⇒ Object
- #latest_version(library) ⇒ Object
- #list ⇒ Object
- #search(pattern) ⇒ Object
Methods inherited from Base
#download, inherited, #initialize
Constructor Details
This class inherits a constructor from CDNGet::Base
Instance Method Details
#find(library) ⇒ Object
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
# File 'lib/cdnget.rb', line 367 def find(library) validate(library, nil) url = "https://ofcncog2cu-dsn.algolia.net/1/indexes/npm-search/#{library.sub('/', '%2f')}" 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
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 |
# File 'lib/cdnget.rb', line 396 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 = "#{CDN_URL}/#{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: npmpkg_url(library, version), urls: files.collect {|x| baseurl + x }, files: files, baseurl: baseurl, default: jdata["default"], destdir: "#{library}@#{version}", }) return dict end |
#latest_version(library) ⇒ Object
424 425 426 427 428 429 |
# File 'lib/cdnget.rb', line 424 def latest_version(library) validate(library, nil) json = fetch("#{API_URL}/package/npm/#{library}") jdict = JSON.load(json) return jdict["tags"]["latest"] end |
#list ⇒ Object
342 343 344 |
# File 'lib/cdnget.rb', line 342 def list() return nil # nil means that this CDN can't list libraries without pattern end |
#search(pattern) ⇒ Object
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
# File 'lib/cdnget.rb', line 346 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 |