class CDNGet::GoogleCDN

Constants

CODE
SITE_URL

Public Instance Methods

find(library) click to toggle source
# File lib/cdnget.rb, line 240
def find(library)
  validate(library, nil)
  html = fetch("https://developers.google.com/speed/libraries/")
  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)="([^"]*?)"/) do |href,|
          urls << href
        end
      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,
    urls: urls,
    versions: versions,
  }
end
get(library, version) click to toggle source
# File lib/cdnget.rb, line 281
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],
    urls:    urls,
    files:   files,
    baseurl: baseurl,
    version: version,
  }
end
list() click to toggle source
# File lib/cdnget.rb, line 230
def list
  libs = []
  html = fetch("https://developers.google.com/speed/libraries/")
  rexp = %r`"https://ajax\.googleapis\.com/ajax/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