Class: CDNGet::Unpkg
Constant Summary collapse
- CODE =
"unpkg"
- SITE_URL =
"https://unpkg.com/"
- API_URL =
API_URL = “www.npmjs.com”
"https://api.npms.io/v2"
- 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
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 |
# File 'lib/cdnget.rb', line 468 def find(library) validate(library, nil) json = fetch("#{API_URL}/package/#{library.sub('/', '%2f')}", library) jdata = JSON.load(json) _debug_print(jdata) dict = jdata["collected"]["metadata"] versions = [dict["version"]] # url = File.join(SITE_URL, "/browse/#{library}/") html = fetch(url, library) _debug_print(html) if html =~ /<script>window.__DATA__\s*=\s*(.*?)<\/script>/m jdata2 = JSON.load($1) versions = jdata2["availableVersions"].reverse() end # return { name: dict["name"], desc: dict["description"], tags: (dict["keywords"] || []).join(", "), site: dict["links"] ? dict["links"]["homepage"] : dict["links"]["npm"], info: File.join(SITE_URL, "/browse/#{library}/"), versions: versions, license: dict["license"], } end |
#get(library, version) ⇒ Object
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 |
# File 'lib/cdnget.rb', line 495 def get(library, version) validate(library, version) dict = find(library) dict.delete(:versions) # url = "#{SITE_URL}#{library}@#{version}/?meta" begin json = fetch(url, library) rescue CommandError raise CommandError.new("#{library}@#{version}: Version not found.") end jdata = JSON.load(json) _debug_print(jdata) pr = proc do |jdata, files| jdata['files'].each do |d| d['type'] == "directory" ? pr.call(d, files) \ : (files << d['path']) end if jdata['files'] files end files = pr.call(jdata, []) #files = files.sort_by {|s| s.downcase } baseurl = File.join(SITE_URL, "/#{library}@#{version}") # dict.update({ name: library, version: version, info: File.join(SITE_URL, "/browse/#{library}@#{version}/"), npmpkg: npmpkg_url(library, version), urls: files.collect {|x| baseurl+x }, files: files, baseurl: baseurl, #default: jdata["default"], destdir: "#{library}@#{version}", skipfile: /\.DS_Store\z/, # downloading '.DS_Store' from UNPKG results in 403 }) return dict end |
#latest_version(library) ⇒ Object
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 |
# File 'lib/cdnget.rb', line 534 def latest_version(library) validate(library, nil) version = nil url = File.join(SITE_URL, "/browse/#{library}/") uri = URI.parse(url) HttpConnection.open(URI.parse(SITE_URL)) do |http| resp = http.request('HEAD', "/browse/#{library}/") if resp.code == "302" # 302 Found location = resp.header['Location'] location =~ /@([^@\/]+)\/\z/ version = $1 end end version ||= super(library) return version end |
#list ⇒ Object
449 450 451 |
# File 'lib/cdnget.rb', line 449 def list() return nil # nil means that this CDN can't list libraries without pattern end |
#search(pattern) ⇒ Object
453 454 455 456 457 458 459 460 461 462 463 464 465 466 |
# File 'lib/cdnget.rb', line 453 def search(pattern) #json = fetch("#{API_URL}/search?q=#{pattern}") json = fetch("#{API_URL}/search?q=#{pattern}&size=250") jdata = JSON.load(json) _debug_print(jdata) #arr = jdata["objects"] # www.npmjs.com arr = jdata["results"] # api.npms.io return arr.select {|dict| File.fnmatch(pattern, dict["package"]["name"], File::FNM_CASEFOLD) }.collect {|dict| d = dict["package"] {name: d["name"], desc: d["description"], version: d["version"]} } end |