Class: CDNGet::GoogleCDN

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

Constant Summary collapse

CODE =
"google"
SITE_URL =
'https://developers.google.com/speed/libraries/'

Instance Attribute Summary

Attributes inherited from Base

#debug_mode

Instance Method Summary collapse

Methods inherited from Base

#download, inherited, #initialize, #search

Constructor Details

This class inherits a constructor from CDNGet::Base

Instance Method Details

#find(library) ⇒ Object



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
533
534
535
# File 'lib/cdnget.rb', line 496

def find(library)
  validate(library, nil)
  html = fetch("https://developers.google.com/speed/libraries/")
  _debug_print(html)
  rexp = %r`"https://ajax\.googleapis\.com/ajax/libs/#{library}/`
  site_url = nil
  versions = []
  urls = []
  found = false
  html.scan(/<h3\b.*?>.*?<\/h3>\s*<dl>(.*?)<\/dl>/m) do |text,|
    if text =~ rexp
      found = true
      if text =~ /<dt>.*?snippet:<\/dt>\s*<dd>(.*?)<\/dd>/m
        s = $1
        s.scan(/\b(?:src|href)="([^"]*?)"/) {|href,| urls << href }
      end
      if text =~ /<dt>site:<\/dt>\s*<dd>(.*?)<\/dd>/m
        s = $1
        if s =~ %r`href="([^"]+)"`
          site_url = $1
        end
      end
      text.scan(/<dt>(?:stable |unstable )?versions:<\/dt>\s*<dd\b.*?>(.*?)<\/dd>/m) do
        s = $1
        vers = s.split(/,/).collect {|x| x.strip() }
        versions.concat(vers)
      end
      break
    end
  end
  found  or
    raise CommandError.new("#{library}: Library not found.")
  return {
    name: library,
    site: site_url,
    info: "#{SITE_URL}\##{library}",
    urls: urls,
    versions: versions,
  }
end

#get(library, version) ⇒ Object



537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
# File 'lib/cdnget.rb', line 537

def get(library, version)
  validate(library, version)
  d = find(library)
  d[:versions].find(version)  or
    raise CommandError.new("#{version}: No such version of #{library}.")
  urls = d[:urls]
  if urls
    rexp = /(\/libs\/#{library})\/[^\/]+/
    urls = urls.collect {|x| x.gsub(rexp, "\\1/#{version}") }
  end
  baseurl = "https://ajax.googleapis.com/ajax/libs/#{library}/#{version}"
  files = urls ? urls.collect {|x| x[baseurl.length..-1] } : nil
  return {
    name:    d[:name],
    site:    d[:site],
    info:    "#{SITE_URL}\##{library}",
    urls:    urls,
    files:   files,
    baseurl: baseurl,
    version: version,
  }
end

#listObject



485
486
487
488
489
490
491
492
493
494
# File 'lib/cdnget.rb', line 485

def list()
  html = fetch("https://developers.google.com/speed/libraries/")
  _debug_print(html)
  rexp = %r`"https://ajax\.googleapis\.com/ajax/libs/([^/]+)/([^/]+)/([^"]+)"`
  libs = []
  html.scan(rexp) do |lib, ver, file|
    libs << {name: lib, desc: "latest version: #{ver}" }
  end
  return libs.sort_by {|d| d[:name] }.uniq
end