middleman-imgix 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fbba5297726c9a3e3a325bfba635f9e3cd060bb3
4
+ data.tar.gz: 77c58e86e08c9ce123ee8d9b37082c92b996ba53
5
+ SHA512:
6
+ metadata.gz: dbd10bdb03066aa4b3d7d07d0be3a70171709d71c38503f36f771eeeb000a432e60c918c61e404b76ee8a214971641770440bac6e02ed6b6c1c8cbb7893b7d09
7
+ data.tar.gz: be4c53177182f57dfa062f919e30c40b0d112307c8438de2fea082b37eb06fc488cc5e1ac0813bab8eebe1083be667272cd45c7f9d7362c1fdf831a6c1208fc5
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ # Ignore bundler lock file
2
+ /Gemfile.lock
3
+
4
+ # Ignore pkg folder
5
+ /pkg
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ sudo: false
3
+ cache: bundler
4
+ rvm:
5
+ - 2.0
6
+ - 2.1
7
+ - 2.2
8
+ - 2.3.1
9
+ notifications:
10
+ email: false
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :development do
6
+ gem 'rake'
7
+ gem 'rspec', '~> 2.14.1'
8
+ gem 'capybara', '~> 2.1.0'
9
+ end
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require 'rspec/core/rake_task'
2
+ RSpec::Core::RakeTask.new(:spec)
3
+ task :default => :spec
@@ -0,0 +1,6 @@
1
+ require 'middleman-core'
2
+
3
+ Middleman::Extensions.register :imgix do
4
+ require 'middleman-imgix/extension'
5
+ Middleman::Imgix
6
+ end
@@ -0,0 +1,78 @@
1
+ require 'middleman-core'
2
+
3
+ class Middleman::Imgix < ::Middleman::Extension
4
+ option :host, nil, 'Your imgix host.', required: true
5
+ option :secure_url_token, nil, 'Your imgix secure_url_token.'
6
+ option :use_https, true, 'Whether to use http or https for imgix.'
7
+ option :shard_strategy, :crc, 'Your imgix shard strategy.'
8
+ option :include_library_param, true, 'Include the imgix library param in each URL.'
9
+ option :default_params, { auto: 'format' }, 'Default imgix params to use on all images.'
10
+ option :imgix_js_version, nil, 'Converts image_tags to support imgix.js version 2 or 3.'
11
+ option :exts, %w(.png .jpg .jpeg), 'List of file extensions that get converted to imgix URLs.'
12
+ option :sources, %w(.css .htm .html .js .php .xhtml), 'List of source extensions that are searched for imgix images.'
13
+ option :ignore, [], 'Regexes of filenames to skip adding imgix to.'
14
+ option :rewrite_ignore, [], 'Regexes of filenames to skip processing for path rewrites.'
15
+
16
+ expose_to_template imgix_options: :options
17
+
18
+ def initialize(app, options_hash={}, &block)
19
+ super
20
+
21
+ app.rewrite_inline_urls id: :imgix,
22
+ url_extensions: options.exts,
23
+ source_extensions: options.sources,
24
+ ignore: options.ignore,
25
+ rewrite_ignore: options.rewrite_ignore,
26
+ proc: method(:rewrite_url)
27
+
28
+ require 'imgix'
29
+ end
30
+
31
+ def rewrite_url(asset_path, dirpath, _request_path)
32
+ uri = URI.parse(asset_path)
33
+
34
+ if uri.relative?
35
+ params = CGI::parse(uri.query.to_s)
36
+ params = params.reverse_merge(options.default_params)
37
+ path = client.path(uri.path)
38
+ path.to_url(params)
39
+ else
40
+ asset_path
41
+ end
42
+ end
43
+ memoize :rewrite_url
44
+
45
+ helpers do
46
+ def image_tag(path, params={})
47
+ if imgix_options.imgix_js_version && imgix_image?(path)
48
+ version = imgix_options.imgix_js_version.to_i
49
+ params[:class] = 'imgix-fluid' if version == 2
50
+ src = version == 2 ? 'data-src=' : 'ix-src='
51
+ super(path, params).gsub('src=', src)
52
+ else
53
+ super
54
+ end
55
+ end
56
+
57
+ def imgix_image?(path)
58
+ uri = URI.parse(path)
59
+ uri.relative? &&
60
+ imgix_options.exts.include?(::File.extname(uri.path)) &&
61
+ imgix_options.sources.include?(::File.extname(current_resource.path)) &&
62
+ imgix_options.ignore.none? { |r| ::Middleman::Util.should_ignore?(r, uri.path) } &&
63
+ imgix_options.rewrite_ignore.none? { |i| ::Middleman::Util.path_match(i, uri.path) }
64
+ end
65
+ end
66
+
67
+ private
68
+
69
+ def client
70
+ @client ||= Imgix::Client.new({
71
+ host: options.host,
72
+ secure_url_token: options.secure_url_token,
73
+ use_https: options.use_https,
74
+ shard_strategy: options.shard_strategy,
75
+ include_library_param: options.include_library_param
76
+ })
77
+ end
78
+ end
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.name = 'middleman-imgix'
5
+ gem.version = '1.0.1'
6
+ gem.licenses = ['MIT']
7
+ gem.authors = ['Big Cartel']
8
+ gem.email = ['dev@bigcartel.com']
9
+ gem.description = %q(Use imgix images in your Middleman site.)
10
+ gem.summary = %q(Run all of your images (or only some) through imgix for all sorts of fun features.)
11
+ gem.homepage = 'https://github.com/bigcartel/middleman-imgix'
12
+
13
+ gem.files = `git ls-files`.split($/)
14
+ gem.require_paths = ['lib']
15
+
16
+ gem.add_runtime_dependency 'middleman-core', '~> 4.1', '>= 4.1.8'
17
+ gem.add_runtime_dependency 'imgix', '~> 1.1', '>= 1.1.0'
18
+ end
@@ -0,0 +1,153 @@
1
+ require 'spec_helper'
2
+
3
+ describe "middleman-imgix", type: :feature do
4
+ def visit_app(config='host: "images.example.com"')
5
+ app("activate :imgix, #{config}")
6
+ visit '/imgix.html'
7
+ end
8
+
9
+ describe "host" do
10
+ it "should raise an error if missing" do
11
+ expect { app('activate :imgix') }.to raise_error
12
+ end
13
+
14
+ it "can be set to a single host" do
15
+ visit_app
16
+ expect(page.body).to include('images.example.com')
17
+ end
18
+
19
+ it "can be set to multiple hosts" do
20
+ visit_app('host: ["images1.example.com", "images2.example.com"]')
21
+ expect(page.body).to include('images1.example.com')
22
+ expect(page.body).to include('images2.example.com')
23
+ end
24
+ end
25
+
26
+ describe "secure_url_token" do
27
+ it "should be nil by default" do
28
+ visit_app
29
+ expect(page.body).to_not include('&s=')
30
+ end
31
+
32
+ it "should work when set" do
33
+ visit_app('host: "images.example.com", secure_url_token: "test"')
34
+ expect(page.body).to include('&s=c2708015f12eae21de4d0e6cc55f525e')
35
+ end
36
+ end
37
+
38
+ describe "use_https" do
39
+ it "should be true by default" do
40
+ visit_app
41
+ expect(page.body).to include('https://')
42
+ end
43
+
44
+ it "should work when false" do
45
+ visit_app('host: "images.example.com", use_https: false')
46
+ expect(page.body).to include('http://')
47
+ end
48
+ end
49
+
50
+ describe "shard_strategy" do
51
+ it "should be crc by default" do
52
+ visit_app('host: ["1.example.com", "2.example.com", "3.example.com"]')
53
+ expect(page.body).to eq(%{<img src="https://2.example.com/images/test.png?ixlib=rb-1.1.0&auto=format" alt="Test" />\n<img src="https://2.example.com/images/test.jpg?ixlib=rb-1.1.0&auto=format" alt="Test" />\n<img src="/images/test.gif" alt="Test" />\n})
54
+ end
55
+
56
+ it "should work when cycle" do
57
+ visit_app('host: ["1.example.com", "2.example.com", "3.example.com"], shard_strategy: :cycle')
58
+ expect(page.body).to eq(%{<img src="https://1.example.com/images/test.png?ixlib=rb-1.1.0&auto=format" alt="Test" />\n<img src="https://2.example.com/images/test.jpg?ixlib=rb-1.1.0&auto=format" alt="Test" />\n<img src="/images/test.gif" alt="Test" />\n})
59
+ end
60
+ end
61
+
62
+ describe "include_library_param" do
63
+ it "should be true by default" do
64
+ visit_app
65
+ expect(page.body).to include('ixlib=rb-1.1.0')
66
+ end
67
+
68
+ it "should work when set" do
69
+ visit_app('host: "images.example.com", include_library_param: false')
70
+ expect(page.body).to_not include('ixlib=rb-1.1.0')
71
+ end
72
+ end
73
+
74
+ describe "default_params" do
75
+ it "should be auto format by default" do
76
+ visit_app
77
+ expect(page.body).to include('auto=format')
78
+ end
79
+
80
+ it "should work when customized" do
81
+ visit_app('host: "images.example.com", default_params: {fit:"max"}')
82
+ expect(page.body).to_not include('auto=format')
83
+ expect(page.body).to include('fit=max')
84
+ end
85
+ end
86
+
87
+ describe "imgix_js_version" do
88
+ it "should not use imgix.js default" do
89
+ visit_app
90
+ expect(page.body).to include(%{<img src="https://images.example.com/images/test.png?ixlib=rb-1.1.0&auto=format" alt="Test" />})
91
+ end
92
+
93
+ it "should support imgix.js v2" do
94
+ visit_app('host: "images.example.com", imgix_js_version: 2')
95
+ expect(page.body).to include(%{<img data-src="https://images.example.com/images/test.png?ixlib=rb-1.1.0&auto=format" class="imgix-fluid" alt="Test" />})
96
+ end
97
+
98
+ it "should support imgix.js v3" do
99
+ visit_app('host: "images.example.com", imgix_js_version: 3')
100
+ expect(page.body).to include(%{<img ix-src="https://images.example.com/images/test.png?ixlib=rb-1.1.0&auto=format" alt="Test" />})
101
+ end
102
+ end
103
+
104
+ describe "exts" do
105
+ it "should be pngs and jpgs by default" do
106
+ visit_app
107
+ expect(page.body).to eq(%{<img src="https://images.example.com/images/test.png?ixlib=rb-1.1.0&auto=format" alt="Test" />\n<img src="https://images.example.com/images/test.jpg?ixlib=rb-1.1.0&auto=format" alt="Test" />\n<img src="/images/test.gif" alt="Test" />\n})
108
+ end
109
+
110
+ it "should work when customized" do
111
+ visit_app('host: "images.example.com", exts: %w(.png .jpg .jpeg .gif)')
112
+ expect(page.body).to eq(%{<img src="https://images.example.com/images/test.png?ixlib=rb-1.1.0&auto=format" alt="Test" />\n<img src="https://images.example.com/images/test.jpg?ixlib=rb-1.1.0&auto=format" alt="Test" />\n<img src="https://images.example.com/images/test.gif?ixlib=rb-1.1.0&auto=format" alt="Test" />\n})
113
+ end
114
+ end
115
+
116
+ describe "sources" do
117
+ it "should be html, js, and css by default" do
118
+ visit_app
119
+ expect(page.body).to include(%'images.example.com')
120
+ end
121
+
122
+ it "should work when customized" do
123
+ visit_app('host: "images.example.com", sources: %w(.css)')
124
+ expect(page.body).to_not include(%'images.example.com')
125
+ end
126
+ end
127
+
128
+ describe "ignore" do
129
+ it "should not ignore anything by default" do
130
+ visit_app
131
+ expect(page.body).to include(%'images.example.com/images/test.png')
132
+ end
133
+
134
+ it "should work when customized" do
135
+ visit_app('host: "images.example.com", ignore: %w(/images/test.png)')
136
+ expect(page.body).to_not include(%'images.example.com/images/test.png')
137
+ expect(page.body).to include(%'/images/test.png')
138
+ end
139
+ end
140
+
141
+ describe "rewrite_ignore" do
142
+ it "should not ignore anything by default" do
143
+ visit_app
144
+ expect(page.body).to include(%'images.example.com/images/test.png')
145
+ end
146
+
147
+ it "should work when customized" do
148
+ visit_app('host: "images.example.com", rewrite_ignore: %w(/imgix.html)')
149
+ expect(page.body).to_not include(%'images.example.com/images/test.png')
150
+ expect(page.body).to include(%'/images/test.png')
151
+ end
152
+ end
153
+ end
@@ -0,0 +1,3 @@
1
+ <%= image_tag 'test.png' %>
2
+ <%= image_tag 'test.jpg' %>
3
+ <%= image_tag 'test.gif' %>
@@ -0,0 +1,28 @@
1
+ require 'rspec'
2
+ require 'capybara/rspec'
3
+ require 'middleman-core'
4
+ require 'middleman-core/rack'
5
+ require 'middleman-imgix'
6
+
7
+ def app(config_rb='')
8
+ ENV['MM_ROOT'] = File.expand_path(File.join(File.dirname(__FILE__), 'fixtures', 'app'))
9
+ ENV['MM_ENV'] = 'test'
10
+
11
+ config_path = File.join(ENV['MM_ROOT'], 'config.rb')
12
+ File.write(config_path, config_rb)
13
+
14
+ mm_app = ::Middleman::Application.new do
15
+ config[:watcher_disable] = true
16
+ end
17
+
18
+ Capybara.app = ::Middleman::Rack.new(mm_app).to_app
19
+ File.delete(config_path)
20
+
21
+ mm_app
22
+ end
23
+
24
+ RSpec.configure do |config|
25
+ config.color = true
26
+ config.tty = true
27
+ config.formatter = :documentation
28
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: middleman-imgix
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Big Cartel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-07-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: middleman-core
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '4.1'
20
+ - - '>='
21
+ - !ruby/object:Gem::Version
22
+ version: 4.1.8
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '4.1'
30
+ - - '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 4.1.8
33
+ - !ruby/object:Gem::Dependency
34
+ name: imgix
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ~>
38
+ - !ruby/object:Gem::Version
39
+ version: '1.1'
40
+ - - '>='
41
+ - !ruby/object:Gem::Version
42
+ version: 1.1.0
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ~>
48
+ - !ruby/object:Gem::Version
49
+ version: '1.1'
50
+ - - '>='
51
+ - !ruby/object:Gem::Version
52
+ version: 1.1.0
53
+ description: Use imgix images in your Middleman site.
54
+ email:
55
+ - dev@bigcartel.com
56
+ executables: []
57
+ extensions: []
58
+ extra_rdoc_files: []
59
+ files:
60
+ - .gitignore
61
+ - .travis.yml
62
+ - Gemfile
63
+ - Rakefile
64
+ - lib/middleman-imgix.rb
65
+ - lib/middleman-imgix/extension.rb
66
+ - middleman-imgix.gemspec
67
+ - spec/features/imgix_spec.rb
68
+ - spec/fixtures/app/source/images/test.gif
69
+ - spec/fixtures/app/source/images/test.jpg
70
+ - spec/fixtures/app/source/images/test.png
71
+ - spec/fixtures/app/source/imgix.html.erb
72
+ - spec/spec_helper.rb
73
+ homepage: https://github.com/bigcartel/middleman-imgix
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.2.2
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Run all of your images (or only some) through imgix for all sorts of fun
97
+ features.
98
+ test_files: []
99
+ has_rdoc: