Class: CDNGet::GoogleCDN

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

Overview

TODO: use jsdelivr api

Constant Summary collapse

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

Instance Method Summary collapse

Methods inherited from Base

#download, inherited

Instance Method Details

#find(library) ⇒ Object



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/cdnget.rb', line 249

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) ⇒ Object



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/cdnget.rb', line 290

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

#listObject



239
240
241
242
243
244
245
246
247
# File 'lib/cdnget.rb', line 239

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