cdn_assets 0.0.1 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a6820c7808226a2faa717350208ccf52e6f44048
4
- data.tar.gz: 1b21c6f73945d300c807603840ce5a16140bca88
3
+ metadata.gz: 7c7d19f077164bd8d2b62c2c2ba4ed93630b4266
4
+ data.tar.gz: b378391f98bc5786cb161d9d4ca30df81c8207f3
5
5
  SHA512:
6
- metadata.gz: af7f2003152770a09912409cf35fce953ba4cf304781e01bdbcc18ed1f991deb80112c23700bcc65fa9c7f61ac09eec2d6dbfc6527926b0e4ecfeef79c4ea5bf
7
- data.tar.gz: 11086e3857ef98a033ea06f3dd9d9830f8fa2d36e167e7fb90364939022b4942bca84ad2f1e87443f41e3fa372da432d789895296700d740c0850c8b3a1cba20
6
+ metadata.gz: 9fb08c64f14dea359e9fef09972a681a8f602822b85fb43d518da2625e623922233edaac6842cc891565f92074de089dda047e1e24122f5af367038cf4d2cf87
7
+ data.tar.gz: 84458e00d17d44e3c181effbc9da2546dc3aa15a406feda48093eacb38c63b6ddd783dcd2ccd942655060fb9864f31eba028d8036abef4f70e5f2a99ce42bc45
data/README.md CHANGED
@@ -1,29 +1,54 @@
1
- # CdnAssets
1
+ # cdn_assets
2
2
 
3
- TODO: Write a gem description
3
+ Easily include popular CDN assets (e.g. jQuery, Twitter Bootstrap, Font Awesome, etc.) in your Ruby on Rails application.
4
+
5
+ This gem can definitely be used as a long-term solution if you're okay with the risks associated with having your assets served by a third party, but it's probably much more suited to those times when you just need to put something together *quickly* without hassle.
4
6
 
5
7
  ## Installation
6
8
 
7
- Add this line to your application's Gemfile:
9
+ Add this line to your application's *Gemfile*:
8
10
 
9
- gem 'cdn_assets'
11
+ ```ruby
12
+ gem 'cdn_assets'
13
+ ```
10
14
 
11
15
  And then execute:
12
16
 
13
- $ bundle
17
+ ```bash
18
+ $ bundle install
19
+ ```
20
+
21
+ ## Usage
14
22
 
15
- Or install it yourself as:
23
+ Anywhere in a view file (e.g. the `<head>` of application.html.erb), use the `cdn_asset` function to include a single CDN asset. Example:
16
24
 
17
- $ gem install cdn_assets
25
+ ```ruby
26
+ cdn_asset :jquery, v: '1.10.1', c: :google
27
+ ```
18
28
 
19
- ## Usage
29
+ Which produces the following output:
30
+
31
+ ```html
32
+ <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
33
+ ```
34
+
35
+ As can be seen, the first argument to the function is the asset name, and the second is a hash of options containing the version (`v`) and the CDN (`c`) to serve it from. The options hash can also contain anything you would normally pass as options to a `javascript_include_tag` or a `stylesheet_link_tag`.
36
+
37
+ You can call the function as many times as you want, but if you want to include **multiple** assets in the same view, then a better way is to pass a hash to the `cdn_assets` (plural) function. Example:
38
+
39
+ ```ruby
40
+ cdn_assets bootstrap: { v: '2.3.2', c: :cdnjs }, font_awesome: { v: '3.2.1', c: :netdna }
41
+ ```
20
42
 
21
- TODO: Write usage instructions here
43
+ ## Supported Assets & CDNs
22
44
 
23
- ## Contributing
45
+ Below is a matrix showing the assets and CDNs currently supported by this gem, ordered alphabetically. Use the exact names listed here as symbols or strings in your code.
24
46
 
25
- 1. Fork it
26
- 2. Create your feature branch (`git checkout -b my-new-feature`)
27
- 3. Commit your changes (`git commit -am 'Add some feature'`)
28
- 4. Push to the branch (`git push origin my-new-feature`)
29
- 5. Create new Pull Request
47
+ | | **cdnjs** | **google** | **jsdelivr** | **microsoft** | **netdna** |
48
+ |:-------------------------------|:---------:|:----------:|:------------:|:-------------:|:----------:|
49
+ | **bootstrap** | :+1: | | :+1: | | |
50
+ | **bootstrap_combined** | | | | | :+1: |
51
+ | **bootstrap_js** | :+1: | | :+1: | | :+1: |
52
+ | **bootstrap_responsive** | :+1: | | :+1: | | |
53
+ | **font_awesome** | :+1: | | :+1: | | :+1: |
54
+ | **jquery** | :+1: | :+1: | :+1: | :+1: | |
data/lib/cdn_assets.rb CHANGED
@@ -1,5 +1,2 @@
1
- require "cdn_assets/version"
2
-
3
- module CdnAssets
4
- # Your code goes here...
5
- end
1
+ require 'cdn_assets/version'
2
+ require 'cdn_assets/railtie' if defined?(Rails)
@@ -0,0 +1,9 @@
1
+ require 'cdn_assets/view_helpers'
2
+
3
+ module CdnAssets
4
+ class Railtie < Rails::Railtie
5
+ initializer 'cdn_assets.view_helpers' do
6
+ ActionView::Base.send :include, ViewHelpers
7
+ end
8
+ end
9
+ end
@@ -1,3 +1,3 @@
1
1
  module CdnAssets
2
- VERSION = "0.0.1"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -0,0 +1,60 @@
1
+ module CdnAssets
2
+ module ViewHelpers
3
+ ASSET_CDN_URLS = {
4
+ bootstrap: {
5
+ cdnjs: '//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/{v}/css/bootstrap.min.css',
6
+ jsdelivr: '//cdn.jsdelivr.net/bootstrap/{v}/css/bootstrap.min.css'
7
+ },
8
+ bootstrap_combined: {
9
+ netdna: '//netdna.bootstrapcdn.com/twitter-bootstrap/{v}/css/bootstrap-combined.min.css'
10
+ },
11
+ bootstrap_js: {
12
+ cdnjs: '//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/{v}/js/bootstrap.min.js',
13
+ jsdelivr: '//cdn.jsdelivr.net/bootstrap/{v}/js/bootstrap.min.js',
14
+ netdna: '//netdna.bootstrapcdn.com/twitter-bootstrap/{v}/js/bootstrap.min.js'
15
+ },
16
+ bootstrap_responsive: {
17
+ cdnjs: '//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/{v}/css/bootstrap-responsive.min.css',
18
+ jsdelivr: '//cdn.jsdelivr.net/bootstrap/{v}/css/bootstrap-responsive.min.css'
19
+ },
20
+ font_awesome: {
21
+ cdnjs: '//cdnjs.cloudflare.com/ajax/libs/font-awesome/{v}/css/font-awesome.min.css',
22
+ jsdelivr: '//cdn.jsdelivr.net/fontawesome/{v}/css/font-awesome.min.css',
23
+ netdna: '//netdna.bootstrapcdn.com/font-awesome/{v}/css/font-awesome.min.css'
24
+ },
25
+ jquery: {
26
+ cdnjs: '//cdnjs.cloudflare.com/ajax/libs/jquery/{v}/jquery.min.js',
27
+ google: '//ajax.googleapis.com/ajax/libs/jquery/{v}/jquery.min.js',
28
+ jsdelivr: '//cdn.jsdelivr.net/jquery/{v}/jquery-{v}.min.js',
29
+ microsoft: '//ajax.aspnetcdn.com/ajax/jQuery/jquery-{v}.min.js'
30
+ }
31
+ }
32
+
33
+ def cdn_assets(assets = {})
34
+ assets.map { |asset, options| cdn_asset asset, options }.join("\n").html_safe
35
+ end
36
+
37
+ def cdn_asset(asset, options = {})
38
+ cdn = options.delete(:c)
39
+ raise ArgumentError, 'Asset CDN not specified' if cdn.nil?
40
+
41
+ version = options.delete(:v)
42
+ raise ArgumentError, 'Asset version not specified' if version.nil?
43
+
44
+ cdn_urls = ASSET_CDN_URLS[asset.to_sym]
45
+ raise ArgumentError, 'Invalid asset name specified' if cdn_urls.nil?
46
+
47
+ url = cdn_urls[cdn.to_sym]
48
+ raise ArgumentError, 'Invalid CDN name specified' if url.nil?
49
+
50
+ url = url.gsub '{v}', version.to_s
51
+ if url.end_with? '.js'
52
+ javascript_include_tag url, options
53
+ elsif url.end_with? '.css'
54
+ stylesheet_link_tag url, options
55
+ else
56
+ raise RuntimeError, 'Unexpected asset type encountered' # should never happen
57
+ end
58
+ end
59
+ end
60
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cdn_assets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luke Horvat
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-06 00:00:00.000000000 Z
11
+ date: 2013-07-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -53,7 +53,9 @@ files:
53
53
  - Rakefile
54
54
  - cdn_assets.gemspec
55
55
  - lib/cdn_assets.rb
56
+ - lib/cdn_assets/railtie.rb
56
57
  - lib/cdn_assets/version.rb
58
+ - lib/cdn_assets/view_helpers.rb
57
59
  homepage: https://github.com/lukehorvat/cdn_assets
58
60
  licenses:
59
61
  - MIT