require 'open-uri' require 'json' module Capucine class CDNJS attr_accessor :result @@cdnjsroot_github = 'https://raw.github.com/cdnjs/cdnjs/master/ajax/libs/' @@cdnjsroot = 'http://cdnjs.cloudflare.com/ajax/libs/' @@cdnjs = 'http://www.cdnjs.com/packages.json' def initialize(lib_name, version = nil, content = true) return false unless lib_name @result = {} cdnjs_url_pkg = "#{@@cdnjsroot_github}#{lib_name}/package.json" raw_content = nil begin distant = open(cdnjs_url_pkg).read rescue => e @result[:error] = e; return end content = JSON.parse(distant) version = version || content['version'] version = version.to_s filename = content['filename'] raw_file_url = "#{@@cdnjsroot}#{lib_name}/#{version}/#{filename}" if content begin raw_content = open(raw_file_url).read rescue => e @result[:error] = e; return end end @result = { :lib => lib_name, :version => version, :file_url => raw_file_url, :content => raw_content, :response => content, } end def self.get_all content = JSON.parse open(@@cdnjs).read return content['packages'] end def self.search(query) results = [] self.get_all.each do |lib| if lib['name'] =~ /#{query}/ results.push(lib) end end return results end end end