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/"
CDN_URL =
"https://ajax\.googleapis\.com/ajax/libs"

Instance Attribute Summary

Attributes inherited from Base

#debug_mode

Instance Method Summary collapse

Methods inherited from Base

#download, inherited, #initialize, #latest_version, #search

Constructor Details

This class inherits a constructor from CDNGet::Base

Instance Method Details

#find(library) ⇒ Object



570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
# File 'lib/cdnget.rb', line 570

def find(library)
  validate(library, nil)
  html = fetch(SITE_URL)
  _debug_print(html)
  rexp = %r`"#{Regexp.escape(CDN_URL)}/#{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



611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
# File 'lib/cdnget.rb', line 611

def get(library, version)
  validate(library, version)
  d = find(library)
  d[:versions].find {|s| s == version }  or
    raise CommandError.new("#{library} #{version}: Version not found.")
  urls = d[:urls]
  if urls
    rexp = /(\/libs\/#{library})\/[^\/]+/
    urls = urls.collect {|x| x.gsub(rexp, "\\1/#{version}") }
  end
  baseurl = "#{CDN_URL}/#{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



559
560
561
562
563
564
565
566
567
568
# File 'lib/cdnget.rb', line 559

def list()
  html = fetch(SITE_URL)
  _debug_print(html)
  rexp = %r`"#{Regexp.escape(CDN_URL)}/([^/]+)/([^/]+)/([^"]+)"`
  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