1
0
Fork 0

Initial commit

This commit is contained in:
Gregory Eremin 2013-07-17 20:26:20 +07:00
commit 588fdd0f64
12 changed files with 3993 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
*.gem
.bundle
Gemfile.lock

4
Gemfile Normal file
View File

@ -0,0 +1,4 @@
source 'https://rubygems.org'
# Specify your gem's dependencies in cdnjs-rails.gemspec
gemspec

22
LICENSE.txt Normal file
View File

@ -0,0 +1,22 @@
Copyright (c) 2013 Gregory Eremin
MIT License
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

29
README.md Normal file
View File

@ -0,0 +1,29 @@
# Cdnjs::Rails
TODO: Write a gem description
## Installation
Add this line to your application's Gemfile:
gem 'cdnjs-rails'
And then execute:
$ bundle
Or install it yourself as:
$ gem install cdnjs-rails
## Usage
TODO: Write usage instructions here
## Contributing
1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request

33
Rakefile Normal file
View File

@ -0,0 +1,33 @@
require 'bundler/gem_tasks'
namespace :cdnjs do
desc 'Updates packages.json file from cdnjs.com'
task :update do
require 'open-uri'
require 'multi_json'
packages_url = 'http://cdnjs.com/packages.json'
file_path = File.expand_path('../data/packages.json', __FILE__)
json = open(packages_url).read
# Remove all unnecessary data
# Keep repo small and code fast
data = MultiJson.load(json)['packages'].map do |pkg|
{
pkg['name'] => {
'filename' => pkg['filename'],
'latest_version' => pkg['version'],
'versions' => pkg['assets'].map{ |asset| asset['version'] }
}
}
end.inject(:merge)
# Format JSON for better diffs and smaller patches
json = MultiJson.dump(data, :pretty => true)
File.open(file_path, 'wb') do |file|
file.write json
file.close
end
end
end

25
cdnjs-rails.gemspec Normal file
View File

@ -0,0 +1,25 @@
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'cdnjs_rails/version'
Gem::Specification.new do |spec|
spec.name = 'cdnjs-rails'
spec.version = CdnjsRails::VERSION
spec.authors = ['Gregory Eremin']
spec.email = ['magnolia_fan@me.com']
spec.description = 'This gem adds view helpers to Ruby on Rails'+
'applications that embed javascripts and'+
'stylesheets directly from cdnjs.com'
spec.summary = 'Rails view helpers for embedding cdnjs.com assets'
spec.homepage = ''
spec.license = 'MIT'
spec.files = `git ls-files`.split($/)
spec.executables = []
spec.test_files = [] # spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ['lib']
spec.add_development_dependency 'bundler', '~> 1.3'
spec.add_development_dependency 'rake'
spec.add_runtime_dependency 'multi_json'
end

3806
data/packages.json Normal file

File diff suppressed because it is too large Load Diff

1
lib/cdnjs-rails.rb Normal file
View File

@ -0,0 +1 @@
require 'cdnjs_rails'

47
lib/cdnjs_rails.rb Normal file
View File

@ -0,0 +1,47 @@
require 'multi_json'
require 'cdnjs_rails/railtie' if defined? Rails
module CdnjsRails
URL_TEMPLATE = '%s//cdnjs.cloudflare.com/ajax/libs/%s/%s/%s'
def package_url name, options = {}
package = packages[name]
raise UnknownPackageError.new(name) if package.nil?
version = options[:version] || options[:ver] || options[:v]
version = package['latest_version'] if version.nil? || version == 'latest'
unless package['versions'].include?(version)
raise UnknownPackageVersionError.new(name, version)
end
protocol = options[:protocol] || options[:proto]
protocol = "#{protocol}:" unless protocol.nil?
URL_TEMPLATE % [protocol.to_s, name, version, package['filename']]
end
private
def packages
@packages ||= MultiJson.load(packages_json)
end
def packages_json
path = File.expand_path('../../data/packages.json', __FILE__)
File.open(path).read
end
extend self
class UnknownPackageError < ::Exception
def initialize name
super "Unknown package: #{name}"
end
end
class UnknownPackageVersionError < ::Exception
def initialize name, version
super "Unknown #{name} version: #{version}"
end
end
end

View File

@ -0,0 +1,9 @@
require 'cdnjs_rails/view_helpers'
module CdnjsRails
class Railtie < Rails::Railtie
initializer 'cdnjs_rails.view_helpers' do |app|
ActionView::Base.send(:include, ViewHelpers)
end
end
end

View File

@ -0,0 +1,3 @@
module CdnjsRails
VERSION = '0.0.1'
end

View File

@ -0,0 +1,11 @@
module CdnjsRails
module ViewHelpers
def javascript_cdnjs_tag name, version = nil
javascript_include_tag CdnjsRails.package_url(name, version)
end
def stylesheet_cdnjs_tag name, version = nil
stylesheet_include_tag CdnjsRails.package_url(name, version)
end
end
end