Sha256: 6a1311d8af687c531895d98610995652fd5edc1b379ea11a934fdab2782355ee

Contents?: true

Size: 1.47 KB

Versions: 8

Compression:

Stored size: 1.47 KB

Contents

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

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
capucine-0.2.10 lib/cdnjs.rb
capucine-0.2.9 lib/cdnjs.rb
capucine-0.2.5 lib/cdnjs.rb
capucine-0.2.4 lib/cdnjs.rb
capucine-0.2.3 lib/cdnjs.rb
capucine-0.2.2 lib/cdnjs.rb
capucine-0.2.1 lib/cdnjs.rb
capucine-0.2.0 lib/cdnjs.rb