Class: Capucine::CDNJS
- Inherits:
-
Object
- Object
- Capucine::CDNJS
- Defined in:
- lib/cdnjs.rb
Constant Summary collapse
- @@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'
Instance Attribute Summary collapse
-
#result ⇒ Object
Returns the value of attribute result.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(lib_name, version = nil, content = true) ⇒ CDNJS
constructor
A new instance of CDNJS.
Constructor Details
#initialize(lib_name, version = nil, content = true) ⇒ CDNJS
Returns a new instance of CDNJS.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/cdnjs.rb', line 13 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 |
Instance Attribute Details
#result ⇒ Object
Returns the value of attribute result.
6 7 8 |
# File 'lib/cdnjs.rb', line 6 def result @result end |
Class Method Details
.get_all ⇒ Object
53 54 55 56 |
# File 'lib/cdnjs.rb', line 53 def self.get_all content = JSON.parse open(@@cdnjs).read return content['packages'] end |
.search(query) ⇒ Object
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/cdnjs.rb', line 58 def self.search(query) results = [] self.get_all.each do |lib| if lib['name'] =~ /#{query}/ results.push(lib) end end return results end |