direct_inject 0.0.1 → 0.1.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: b9e281f54fb9f6b80e88e0341cc921f30ac69114
4
- data.tar.gz: bc35d48472e01577272e3af63e25eba9bb30cc7c
3
+ metadata.gz: dcb2cd60c4f467df942817fbc5f399722edef425
4
+ data.tar.gz: 1e18df2187f5dc604934fdface110d179acd2c4f
5
5
  SHA512:
6
- metadata.gz: 30d7c17427ecce6b6a226d4a76d6665c07cc84044ea5329a8b719a822364b57d8a653470e13ceac92b1b63603df82175f6d62f7119fe9a3b82f8b60228c98536
7
- data.tar.gz: b93c1a99a8c07585381b69f2d53d1df5908abd49f243a5dee527d46647957e2db47cc7fef8d3d7021c4817e81b73055ae5700a3419982d983131aed8e5afc933
6
+ metadata.gz: edbe0119bdfda86cd0e14d76d0f20650606cab17911a1fc9aa0127bc130140e349dd5be557ba834636ef6438f3167133de0cb4316b4c5d48f352b47a0e190c0b
7
+ data.tar.gz: f89eeed4b1cbb26fac0c703ea1846c7e794fb7395eb329d8934f15a4ec435ad93a07bfaa7351a2fd830e9d9e5d2617ec779a92a6ee5f16e3337d54141bce4224
@@ -1,4 +1,4 @@
1
- Copyright 2014 Ryan Cook
1
+ Copyright 2014 Ryan Cook <cookrn@gmail.com>
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -1,16 +1,140 @@
1
1
  DirectInject
2
2
  ============
3
3
 
4
- A Sprockets add-on helping you inject assets into your HTML inline.
4
+ A Sprockets-aware library that helps you inject assets inline into your HTML.
5
5
 
6
6
  ## Installation
7
7
 
8
- WIP
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'direct_inject'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install direct_inject
9
21
 
10
22
  ## Usage
11
23
 
12
- WIP
24
+ ### Why?
25
+
26
+ Recently I was working on a application experimenting with public-key
27
+ cryptography in Javascript. I didn't want to manually manage my assets
28
+ or hand-roll them into HTML. You might be saying to yourself: "Why in
29
+ FSM's name would you be doing crypto in the browser?!" Just know that it
30
+ was only an experiment. Anyhow, it would have been great to have a way
31
+ to utilize Sprockets for managing all of the assets, but be able to
32
+ use them directly inline on the page. Hence, DirectInject was born.
33
+
34
+ ### Usage from Rails
35
+
36
+ DirectInject detects whether it has been loaded in Rails automagically.
37
+ The only requirement is that you're using Rails >= 3.1.
38
+
39
+ Once you're up and running, you have a few extra helpers in your views
40
+ now:
41
+
42
+ 1. `direct_inject_image`: This will find the image in your environment,
43
+ encode it, and place it in your view inline.
44
+ 2. `direct_inject_javascript`: This will find the script in your environment,
45
+ and place it in your view inline.
46
+ 3. `direct_inject_stylesheet`: This will find the stylesheet in your environment,
47
+ and place it in your view inline.
48
+
49
+ ### Usage Elsewhere
50
+
51
+ DirectInject can be used in any Sprockets-enabled application, but it'll
52
+ need a bit more help to get setup.
53
+
54
+ First, let's configure the environment:
55
+
56
+ ```ruby
57
+ DirectInject.sprockets_environment = MyApp.assets
58
+ ```
59
+
60
+ Next, we're able to render images, javascript, and stylesheets using the
61
+ following API:
62
+
63
+ ```ruby
64
+ image_tag = DirectInject.render_image( 'logo.png' )
65
+ javascript_tag = DirectInject.render_javascript( 'main' )
66
+ stylesheet_tag = DirectInject.render_stylesheet( 'application' )
67
+ ```
68
+
69
+ ```html
70
+ <img src="data:image/png;base64,MUMBOJUMBO">
71
+ <script type="text/javascript">/* the codez */</script>
72
+ <style type="text/css">/* the stylez */</style>
73
+ ```
74
+
75
+ The output is just strings containing HTML that you can use as needed.
76
+
77
+ ### Miscellaneous
78
+
79
+ DirectInject's API accepts an options hash as the final argument to any
80
+ of the functions. This has is converted into HTML attributes for the
81
+ resulting markup. For example, to add a CSS class:
82
+
83
+ ```ruby
84
+ DirectInject.render_stylesheet \
85
+ 'application',
86
+ :class => 'the-fanciest-styles-in-the-world'
87
+ ```
88
+
89
+ ```html
90
+ <style class="the-fanciest-styles-in-the-world">/* styles */</style>
91
+ ```
92
+
93
+ Or, for another example, a few data attributes:
94
+
95
+ ```ruby
96
+ DirectInject.render_stylesheet \
97
+ 'application',
98
+ :data => { 'attr1' => 'val1' , 'attr2' => 'val2' }
99
+ ```
100
+
101
+ ```html
102
+ <style data-attr1="val1" data-attr2="val2">/* styles */</style>
103
+ ```
104
+
105
+ DirectInject will automatically add some data attributes to all of the
106
+ HTML it outputs:
107
+
108
+ * `data-direct_inject_source`: the specified asset source
109
+ * `data-direct_inject_type`: the asset type that defined the compilation code path for the source
110
+
111
+ ```html
112
+ <img data-direct_inject_source="headshot.png" data-direct_inject_type="image">
113
+ <script data-direct_inject_source="application" data-direct_inject_type="javascript"></script>
114
+ <style data-direct_inject_source="site" data-direct_inject_type="stylesheet"></style>
115
+ ```
116
+
117
+ DirectInject supports injecting multiple assets simultaneously.
118
+
119
+ ```ruby
120
+ DirectInject.render_javascript \
121
+ 'core.js.coffee',
122
+ 'utils.js.coffee'
123
+ ```
124
+
125
+ ```html
126
+ <script data-direct_inject_source="core.js.coffee">/* the codez */</script>
127
+ <script data-direct_inject_source="utils.js.coffee">/* the codez */</script>
128
+ ```
129
+
130
+ ## Contributing
131
+
132
+ 1. Fork it ( https://github.com/cookrn/direct_inject/fork )
133
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
134
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
135
+ 4. Push to the branch (`git push origin my-new-feature`)
136
+ 5. Create a new Pull Request
13
137
 
14
138
  ## License
15
139
 
16
- MIT. See: MIT-LICENSE
140
+ MIT. See: [LICENSE](https://github.com/cookrn/direct_inject/blob/master/LICENSE)
data/Rakefile CHANGED
@@ -1,34 +1,24 @@
1
1
  begin
2
2
  require 'bundler/setup'
3
+ Bundler::GemHelper.install_tasks
3
4
  rescue LoadError
4
5
  puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
6
  end
6
7
 
7
- require 'rdoc/task'
8
+ APP_RAKEFILE =
9
+ File.expand_path \
10
+ '../test/dummy/Rakefile',
11
+ __FILE__
8
12
 
9
- RDoc::Task.new(:rdoc) do |rdoc|
10
- rdoc.rdoc_dir = 'rdoc'
11
- rdoc.title = 'DirectInject'
12
- rdoc.options << '--line-numbers'
13
- rdoc.rdoc_files.include('README.rdoc')
14
- rdoc.rdoc_files.include('lib/**/*.rb')
15
- end
16
-
17
- APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
18
13
  load 'rails/tasks/engine.rake'
19
14
 
20
-
21
-
22
- Bundler::GemHelper.install_tasks
23
-
24
15
  require 'rake/testtask'
25
16
 
26
- Rake::TestTask.new(:test) do |t|
17
+ Rake::TestTask.new :test do | t |
27
18
  t.libs << 'lib'
28
19
  t.libs << 'test'
29
20
  t.pattern = 'test/**/*_test.rb'
30
21
  t.verbose = false
31
22
  end
32
23
 
33
-
34
- task default: :test
24
+ task :default => :test
@@ -22,6 +22,9 @@ Gem::Specification.new do | spec |
22
22
  spec.add_dependency 'tagz'
23
23
 
24
24
  spec.add_development_dependency 'bundler' , '~> 1.7'
25
- spec.add_development_dependency 'rails' , '>= 3.1'
26
- spec.add_development_dependency 'rake' , '~> 10.0'
25
+ spec.add_development_dependency 'coffee-script'
26
+ spec.add_development_dependency 'nokogiri'
27
+ spec.add_development_dependency 'rails' , '>= 3.1'
28
+ spec.add_development_dependency 'rake' , '~> 10.0'
29
+ spec.add_development_dependency 'sprockets' , '~> 2.12'
27
30
  end
@@ -1,6 +1,8 @@
1
+ require 'base64'
1
2
  require 'direct_inject'
2
3
  require 'direct_inject/errors'
3
4
  require 'map'
5
+ require 'uri'
4
6
 
5
7
  module DirectInject
6
8
  module InternalHelpers
@@ -12,9 +14,15 @@ module DirectInject
12
14
 
13
15
  private
14
16
 
17
+ def data_uri_for( asset )
18
+ prefix = "data:#{ asset.content_type };base64,"
19
+ encoded = Base64.encode64( asset.to_s ).gsub( /\s+/ , '' )
20
+ "#{ prefix }#{ URI.encode_www_form_component encoded }"
21
+ end
22
+
15
23
  def asset_for!( source , options )
16
24
  environment =
17
- options.fetch 'sprockets_environment' do
25
+ options.delete 'sprockets_environment' do
18
26
  DirectInject.sprockets_environment
19
27
  end
20
28
 
@@ -39,10 +47,6 @@ module DirectInject
39
47
  extname if extname && File.extname( source ) != extname
40
48
  end
41
49
 
42
- def image_prefix_for( asset )
43
- "data:image/#{ asset.pathname.extname[ 1 .. -1 ] };base64,"
44
- end
45
-
46
50
  def render( *sources )
47
51
  options = Map.opts! sources
48
52
 
@@ -54,12 +58,11 @@ module DirectInject
54
58
  'direct_inject_source',
55
59
  source
56
60
 
57
- result =
58
- if block_given?
59
- yield( asset , options ).to_s
60
- else
61
- ''
62
- end
61
+ if block_given?
62
+ yield( asset , options ).to_s
63
+ else
64
+ ''
65
+ end
63
66
  end.join "\n"
64
67
  end
65
68
  end
@@ -1,4 +1,3 @@
1
- require 'base64'
2
1
  require 'direct_inject/internal_helpers'
3
2
  require 'direct_inject/templates'
4
3
  require 'map'
@@ -14,8 +13,7 @@ module DirectInject
14
13
  'direct_inject_type',
15
14
  'image'
16
15
 
17
- prefix = image_prefix_for asset
18
- encoded = "#{ prefix }#{ Base64.encode64( asset.to_s ) }"
16
+ encoded = data_uri_for asset
19
17
 
20
18
  Templates::Image.render \
21
19
  encoded,
@@ -9,7 +9,7 @@ module DirectInject
9
9
 
10
10
  initializer 'direct_inject.view_helpers' do
11
11
  ActiveSupport.on_load :action_view do
12
- include Rails::ViewHelpers
12
+ include DirectInject::Rails::ViewHelpers
13
13
  end
14
14
  end
15
15
  end
@@ -4,7 +4,7 @@ module DirectInject
4
4
  module Templates
5
5
  class Javascript < Abstract
6
6
  def render
7
- tagz { script_( tag_options( options ) ){ source } }
7
+ tagz { script_( tag_options options ){ source } }
8
8
  end
9
9
  end
10
10
  end
@@ -4,7 +4,7 @@ module DirectInject
4
4
  module Templates
5
5
  class Stylesheet < Abstract
6
6
  def render
7
- tagz { style_( tag_options( options ) ){ source } }
7
+ tagz { style_( tag_options options ){ source } }
8
8
  end
9
9
  end
10
10
  end
@@ -1,3 +1,3 @@
1
1
  module DirectInject
2
- VERSION = '0.0.1'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -0,0 +1,77 @@
1
+ require 'test_helper'
2
+
3
+ class DirectInjectApiTest < ActiveSupport::TestCase
4
+ test 'apis' do
5
+ tests =
6
+ [
7
+ {
8
+ :api => :render_image,
9
+ :matches => [ /base64/ , /image\/png/ , /iVBORw0KGgoAAAANSUhEUgAAAncAAANrCA/ ],
10
+ :source => 'face.png',
11
+ :tag => 'img',
12
+ :type => 'image'
13
+ },
14
+
15
+ {
16
+ :api => :render_javascript,
17
+ :matches => [ /alert/ , /\d+/ ],
18
+ :source => 'main',
19
+ :tag => 'script',
20
+ :type => 'javascript'
21
+ },
22
+
23
+ {
24
+ :api => :render_stylesheet,
25
+ :matches => [ /div span\.highlight/ , /yellowgreen/ ],
26
+ :source => 'site',
27
+ :tag => 'style',
28
+ :type => 'stylesheet'
29
+ }
30
+ ]
31
+
32
+ tests.each do | test |
33
+ result =
34
+ DirectInject.send \
35
+ test.fetch( :api ),
36
+ test.fetch( :source ),
37
+ :sprockets_environment => LOCAL_SPROCKETS,
38
+ :class => 'wat'
39
+
40
+ assert \
41
+ test.fetch( :matches ).all? do | regexp |
42
+ result =~ regexp
43
+ end
44
+
45
+ el = Nokogiri::HTML::DocumentFragment.parse( result ).children.first
46
+
47
+ assert_equal \
48
+ test.fetch( :tag ),
49
+ el.name
50
+
51
+ expected_keys =
52
+ %w(
53
+ class
54
+ data-direct_inject_source
55
+ data-direct_inject_type
56
+ )
57
+
58
+ expected_keys.each do | expected_key |
59
+ assert_includes \
60
+ el.attributes.keys,
61
+ expected_key
62
+ end
63
+
64
+ refute_includes \
65
+ el.attributes.keys,
66
+ 'sprockets_environment'
67
+
68
+ assert_equal \
69
+ test.fetch( :source ),
70
+ el.attributes.fetch( 'data-direct_inject_source' ).value
71
+
72
+ assert_equal \
73
+ test.fetch( :type ),
74
+ el.attributes.fetch( 'data-direct_inject_type' ).value
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,50 @@
1
+ require 'test_helper'
2
+
3
+ class DirectInjectTest < ActiveSupport::TestCase
4
+ test 'has api methods' do
5
+ %i(
6
+ render_image
7
+ render_javascript
8
+ render_stylesheet
9
+ ).each do | public_api_method |
10
+ assert_respond_to \
11
+ DirectInject,
12
+ public_api_method
13
+ end
14
+ end
15
+
16
+ test 'does not respond to internal helpers' do
17
+ # a sample of the internal helper methods
18
+ %i(
19
+ asset_for!
20
+ data_uri_for
21
+ render
22
+ ).each do | public_api_method |
23
+ refute_respond_to \
24
+ DirectInject,
25
+ public_api_method
26
+ end
27
+ end
28
+
29
+ test 'has a sprockets_environment accessor' do
30
+ old_env = DirectInject.sprockets_environment
31
+
32
+ assert_respond_to \
33
+ DirectInject,
34
+ :sprockets_environment
35
+
36
+ env = Object.new
37
+
38
+ assert_respond_to \
39
+ DirectInject,
40
+ :sprockets_environment=
41
+
42
+ DirectInject.sprockets_environment = env
43
+
44
+ assert_equal \
45
+ env,
46
+ DirectInject.sprockets_environment
47
+
48
+ DirectInject.sprockets_environment = old_env
49
+ end
50
+ end
@@ -1 +1,2 @@
1
+ View source to see result.
1
2
  <%= direct_inject_javascript 'application' %>
@@ -1 +1,2 @@
1
+ View source to see result.
1
2
  <%= direct_inject_stylesheet 'application' %>
@@ -0,0 +1,2 @@
1
+ @x = <%= rand 42 %>
2
+ alert @x
@@ -0,0 +1,10 @@
1
+ $color: yellowgreen;
2
+
3
+ div {
4
+ background-color: $color;
5
+
6
+ span.highlight {
7
+ color: white;
8
+ text-decoration: underline;
9
+ }
10
+ }
@@ -0,0 +1,10 @@
1
+ require 'coffee_script'
2
+
3
+ path = File.dirname __FILE__
4
+
5
+ env = Sprockets::Environment.new
6
+ env.append_path "#{ path }/assets/images"
7
+ env.append_path "#{ path }/assets/javascripts"
8
+ env.append_path "#{ path }/assets/stylesheets"
9
+
10
+ LOCAL_SPROCKETS = env
data/test/test_helper.rb CHANGED
@@ -1,15 +1,17 @@
1
- # Configure Rails Environment
2
- ENV["RAILS_ENV"] = "test"
1
+ ENV['RAILS_ENV'] = 'test'
3
2
 
4
- require File.expand_path("../dummy/config/environment.rb", __FILE__)
5
- require "rails/test_help"
3
+ require File.expand_path('../dummy/config/environment.rb', __FILE__)
4
+ require 'rails/test_help'
5
+
6
+ require 'minitest/hell'
7
+ require 'minitest/pride'
6
8
 
7
9
  Rails.backtrace_cleaner.remove_silencers!
8
10
 
11
+ # Comment out this line to turn the `test` log level back to `debug`
12
+ Rails.logger.level = 4
13
+
9
14
  # Load support files
10
- Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
15
+ Dir[ "#{ File.dirname __FILE__ }/support/**/*.rb" ].each { |f| require f }
11
16
 
12
- # Load fixtures from the engine
13
- if ActiveSupport::TestCase.method_defined?(:fixture_path=)
14
- ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures", __FILE__)
15
- end
17
+ require 'nokogiri'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: direct_inject
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Cook
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-18 00:00:00.000000000 Z
11
+ date: 2014-12-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: map
@@ -52,6 +52,34 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '1.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: coffee-script
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: nokogiri
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
55
83
  - !ruby/object:Gem::Dependency
56
84
  name: rails
57
85
  requirement: !ruby/object:Gem::Requirement
@@ -80,6 +108,20 @@ dependencies:
80
108
  - - "~>"
81
109
  - !ruby/object:Gem::Version
82
110
  version: '10.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: sprockets
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '2.12'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '2.12'
83
125
  description: Sprockets/Rails helpers for directly injecting assets onto the page as
84
126
  an alternative to linking/sourcing an asset by URL.
85
127
  email:
@@ -90,7 +132,7 @@ extra_rdoc_files: []
90
132
  files:
91
133
  - ".gitignore"
92
134
  - Gemfile
93
- - MIT-LICENSE
135
+ - LICENSE
94
136
  - README.md
95
137
  - Rakefile
96
138
  - direct_inject.gemspec
@@ -108,6 +150,8 @@ files:
108
150
  - lib/direct_inject/templates/javascript.rb
109
151
  - lib/direct_inject/templates/stylesheet.rb
110
152
  - lib/direct_inject/version.rb
153
+ - test/direct_inject_api_test.rb
154
+ - test/direct_inject_test.rb
111
155
  - test/dummy/Rakefile
112
156
  - test/dummy/app/assets/images/.keep
113
157
  - test/dummy/app/assets/images/headshot.png
@@ -145,6 +189,10 @@ files:
145
189
  - test/integration/image_test.rb
146
190
  - test/integration/javascript_test.rb
147
191
  - test/integration/stylesheet_test.rb
192
+ - test/support/local_sprockets/assets/images/face.png
193
+ - test/support/local_sprockets/assets/javascripts/main.js.coffee.erb
194
+ - test/support/local_sprockets/assets/stylesheets/site.css.scss
195
+ - test/support/local_sprockets/environment.rb
148
196
  - test/test_helper.rb
149
197
  homepage: https://github.com/cookrn/direct_inject
150
198
  licenses:
@@ -171,6 +219,8 @@ signing_key:
171
219
  specification_version: 4
172
220
  summary: Sprockets/Rails helpers for directly injecting assets onto the page.
173
221
  test_files:
222
+ - test/direct_inject_api_test.rb
223
+ - test/direct_inject_test.rb
174
224
  - test/dummy/Rakefile
175
225
  - test/dummy/app/assets/images/.keep
176
226
  - test/dummy/app/assets/images/headshot.png
@@ -208,4 +258,8 @@ test_files:
208
258
  - test/integration/image_test.rb
209
259
  - test/integration/javascript_test.rb
210
260
  - test/integration/stylesheet_test.rb
261
+ - test/support/local_sprockets/assets/images/face.png
262
+ - test/support/local_sprockets/assets/javascripts/main.js.coffee.erb
263
+ - test/support/local_sprockets/assets/stylesheets/site.css.scss
264
+ - test/support/local_sprockets/environment.rb
211
265
  - test/test_helper.rb