Skip to content

Instantly share code, notes, and snippets.

@kwatch
Created July 3, 2014 09:13
Show Gist options
  • Save kwatch/01ef60146f2965679cce to your computer and use it in GitHub Desktop.
Save kwatch/01ef60146f2965679cce to your computer and use it in GitHub Desktop.
CDNJSからライブラリをダウンロードしてくるRubyスクリプト
# -*- coding: utf-8 -*-
require 'open-uri'
require 'fileutils'
require 'json'
CDNJS_URL = "http://api.cdnjs.com/libraries?search={keyword}&fields=assets"
#
libname = 'angular.js'
url = CDNJS_URL.gsub('{keyword}', libname)
jstr = open(url) {|f| f.read }
jdata = JSON.parse(jstr)
#
entry = jdata['results'].detect {|d| d['name'] == libname }
base_url = File.dirname(entry['latest'])
latest_version = File.basename(base_url)
asset = entry['assets'].detect {|d| d['version'] == latest_version }
#
dist_dir = File.join(libname, latest_version)
FileUtils.rm_rf(dist_dir)
FileUtils.mkdir_p(dist_dir)
#
asset['files'].each do |file|
url = File.join(base_url, file)
p url
content = open(url) {|f| f.read }
dist_file = File.join(dist_dir, file)
File.open(dist_file, 'w') {|f| f.write(content) }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment