amp_helper 0.1.3 → 1.0.0.pre.1
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 +4 -4
- data/README.md +46 -9
- data/lib/amp_helper/amp_image_tag_helper.rb +11 -3
- data/lib/amp_helper/amp_link_to_helper.rb +39 -0
- data/lib/amp_helper/configuration.rb +3 -1
- data/lib/amp_helper/generator.rb +9 -0
- data/lib/amp_helper/railtie.rb +3 -1
- data/lib/amp_helper/version.rb +1 -1
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a1fd42d6d1ead9832928bf7f30ba7312e7369bc9
|
4
|
+
data.tar.gz: c2dc138af979eeb3a2c58ef33e41a45b075c59fb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d0f31e5602cddba8532909293ca4d9567aef745e73a0cf9caf4b289f8ae717b325386001f2bd4725b3acd636d562d72266b34e19c6a94a47026036ffc3ba3112
|
7
|
+
data.tar.gz: 7fce0a40a5d7a9ace07c7dfc5e66acd4f636fb07fc3a7a0fa322be2837527f7bc50af1b80fc7fef86cca7b5fd124edabcffec935d3bbd05930c2e2930f41c0a3
|
data/README.md
CHANGED
@@ -8,7 +8,7 @@ To know AmpHelper ability, just look Usage section.
|
|
8
8
|
Add this line to your application's Gemfile:
|
9
9
|
|
10
10
|
```ruby
|
11
|
-
gem 'amp_helper'
|
11
|
+
gem 'amp_helper', '~> 1.0.0.pre'
|
12
12
|
```
|
13
13
|
|
14
14
|
And then execute:
|
@@ -18,30 +18,33 @@ And then execute:
|
|
18
18
|
|
19
19
|
## Helpers usage
|
20
20
|
|
21
|
-
###
|
21
|
+
### image_tag(source, options = {})
|
22
|
+
|
23
|
+
image_tag method creates a `<amp-img>` tag if it's passed `amp` option as true.
|
24
|
+
Learn more about [amp-img tag](https://ampbyexample.com/components/amp-img/)
|
22
25
|
|
23
26
|
#### String Source
|
24
27
|
|
25
|
-
$
|
28
|
+
$ image_tag('http://placehold.it/350x150', width: 20, height: 20, amp: true)
|
26
29
|
#=> '<amp-img alt="350x150" height="20" src="http://placehold.it/350x150" width="20" /></amp-img>'
|
27
30
|
|
28
|
-
$
|
31
|
+
$ image_tag('http://placehold.it/350x150', size: '20x20', amp: true)
|
29
32
|
#=> '<amp-img alt="350x150" height="20" src="http://placehold.it/350x150" width="20" /></amp-img>'
|
30
33
|
|
31
|
-
$
|
34
|
+
$ image_tag('http://placehold.it/350x150', amp: true)
|
32
35
|
#=> '<amp-img alt="350x150" height="150" src="http://placehold.it/350x150" width="350" /></amp-img>'
|
33
36
|
|
34
37
|
#### Carrierwave Source
|
35
38
|
|
36
|
-
$
|
39
|
+
$ image_tag(ThumbUploader.new.square, amp: true)
|
37
40
|
#=> '<amp-img alt="Square 350x150" height="20" src="http://placehold.it/square_350x150" width="20" /></amp-img>'
|
38
41
|
|
39
42
|
#### Retina
|
40
43
|
|
41
|
-
$
|
44
|
+
$ image_tag('http://placehold.it/350x150', srcset: 'http://placehold.it/700x300 2x', size: '20x20', amp: true)
|
42
45
|
#=> '<amp-img alt="350x150" height="20" src="http://placehold.it/350x150" srcset="http://placehold.it/700x300 2x" width="20" /></amp-img>'
|
43
46
|
|
44
|
-
$
|
47
|
+
$ image_tag(ThumbUploader.new.square, format_2x: '%s_2x', amp: true)
|
45
48
|
#=> '<amp-img alt="Square 350x150" height="20" src="http://placehold.it/square_350x150" srcset="http://placehold.it/square_2x_350x150 2x" width="20" /></amp-img>'
|
46
49
|
|
47
50
|
|
@@ -60,6 +63,32 @@ And then execute:
|
|
60
63
|
end
|
61
64
|
end
|
62
65
|
|
66
|
+
### link_to(name = nil, options = nil, html_options = nil, &block)
|
67
|
+
|
68
|
+
link_to method creates a `<a>` tag links to AMP cache if it's passed `amp` option as true.
|
69
|
+
Learn more about [using AMP cache](https://ampbyexample.com/advanced/using_the_google_amp_cache/).
|
70
|
+
|
71
|
+
#### If indexed AMP cache is found
|
72
|
+
|
73
|
+
$ link_to(@title, @path, amp: true)
|
74
|
+
#=> "<a data-vars-link-url=\"https://ampbyexample.com/components/amp-form/preview/\" href=\"https://ampbyexample-com.cdn.ampproject.org/c/s/ampbyexample.com/components/amp-form/preview/\">link title</a>"
|
75
|
+
|
76
|
+
$ link_to(@path, amp: true) { @title }
|
77
|
+
#=> "<a data-vars-link-url=\"https://ampbyexample.com/components/amp-form/preview/\" href=\"https://ampbyexample-com.cdn.ampproject.org/c/s/ampbyexample.com/components/amp-form/preview/\">link title</a>"
|
78
|
+
|
79
|
+
|
80
|
+
#### If indexed AMP cache is **NOT** found
|
81
|
+
|
82
|
+
$ link_to(@title, @path, amp: true)
|
83
|
+
#=> "<a data-vars-link-url=\"https://ampbyexample.com/components/amp-form/preview/noamp\" href=\"https://ampbyexample.com/components/amp-form/preview/noamp\">link title</a>"
|
84
|
+
|
85
|
+
## amp-analytics conbination
|
86
|
+
|
87
|
+
As you can see, AmpHelper puts the `data-vars-link-url` option on `<a>` tags.
|
88
|
+
It's gonna help to tracking count of link clicking.
|
89
|
+
|
90
|
+
Learn more about [Variables as data attribute](https://github.com/ampproject/amphtml/blob/master/extensions/amp-analytics/analytics-vars.md#variables-as-data-attribute) for amp-analytics.
|
91
|
+
|
63
92
|
## Configure
|
64
93
|
|
65
94
|
### Configure ratina version name format For CarrierWave::Uploader
|
@@ -67,8 +96,17 @@ And then execute:
|
|
67
96
|
config/initializers/amp_helper.rb
|
68
97
|
|
69
98
|
AmpHelper.configure do |config|
|
99
|
+
# Enable amp image.
|
100
|
+
# Default: true
|
101
|
+
# config.enable_amp_image = true
|
102
|
+
#
|
70
103
|
# Configure ratina version name format For CarrierWave::Uploader
|
104
|
+
# Default: nil
|
71
105
|
# config.format_2x = '%s_2x'
|
106
|
+
|
107
|
+
# Enable amp cache link.
|
108
|
+
# Default: true
|
109
|
+
# config.enable_amp_link = Rails.env.production?
|
72
110
|
end
|
73
111
|
|
74
112
|
## Development
|
@@ -83,4 +121,3 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERN
|
|
83
121
|
## License
|
84
122
|
|
85
123
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
86
|
-
|
@@ -1,14 +1,22 @@
|
|
1
1
|
module AmpImageTagHelper
|
2
|
+
def self.included(base)
|
3
|
+
base.module_eval do
|
4
|
+
alias_method :original_image_tag, :image_tag
|
5
|
+
alias_method :image_tag, :amp_image_tag
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
2
9
|
def amp_image_tag(source, opts = {})
|
10
|
+
return original_image_tag(source, opts) unless opts.delete(:amp) && AmpHelper.configuration.enable_amp_image
|
3
11
|
has_dimensions = (opts[:width] && opts[:height]) || opts[:size]
|
4
|
-
set_dimensions(source, opts)
|
12
|
+
set_dimensions(source, opts) unless has_dimensions
|
5
13
|
set_scrset(source, opts)
|
6
|
-
|
14
|
+
img_to_amp_img(original_image_tag(source, opts))
|
7
15
|
end
|
8
16
|
|
9
17
|
private
|
10
18
|
|
11
|
-
def
|
19
|
+
def img_to_amp_img(img_tag)
|
12
20
|
img_tag.gsub(/^<img/, '<amp-img').gsub(/>$/, '></amp-img>').html_safe
|
13
21
|
end
|
14
22
|
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module AmpLinkToHelper
|
2
|
+
def self.included(base)
|
3
|
+
base.module_eval do
|
4
|
+
alias_method :original_link_to, :link_to
|
5
|
+
alias_method :link_to, :amp_link_to
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def amp_link_to(name = nil, options = nil, html_options = nil, &block)
|
10
|
+
html_options, options, name = options, name, block if block_given?
|
11
|
+
html_options ||= {}
|
12
|
+
set_analytics_vars(options, html_options)
|
13
|
+
options = cdn_url(options) if html_options.delete(:amp) && AmpHelper.configuration.enable_amp_link
|
14
|
+
options, name, block = html_options, options, name if block_given?
|
15
|
+
original_link_to(name, options, html_options, &block)
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
# Pass url to amp-analytics as 'linkUrl' variable.
|
21
|
+
def set_analytics_vars(options, html_options)
|
22
|
+
html_options['data-vars-link-url'] = url_for(options)
|
23
|
+
end
|
24
|
+
|
25
|
+
# If AMP cache exists, return cdn url.
|
26
|
+
def cdn_url(options)
|
27
|
+
uri = URI(url_for(options))
|
28
|
+
identify = ''
|
29
|
+
identify += 's/' if request.scheme == 'https'
|
30
|
+
identify += "#{request.host}#{uri.path}#{uri.query}"
|
31
|
+
escaped_host = request.host.gsub('-', '--').gsub('.', '-')
|
32
|
+
cache_url = "https://#{escaped_host}.cdn.ampproject.org/c/#{identify}"
|
33
|
+
response_200?(cache_url) ? cache_url : uri.to_s
|
34
|
+
end
|
35
|
+
|
36
|
+
def response_200?(url)
|
37
|
+
Net::HTTP.get_response(URI(url)).code == '200'
|
38
|
+
end
|
39
|
+
end
|
@@ -9,10 +9,12 @@ module AmpHelper
|
|
9
9
|
end
|
10
10
|
|
11
11
|
class Configuration
|
12
|
-
attr_accessor :format_2x
|
12
|
+
attr_accessor :enable_amp_image, :format_2x, :enable_amp_link
|
13
13
|
|
14
14
|
def initialize
|
15
|
+
@enable_amp_image = true
|
15
16
|
@format_2x = nil
|
17
|
+
@enable_amp_link = true
|
16
18
|
end
|
17
19
|
end
|
18
20
|
end
|
data/lib/amp_helper/generator.rb
CHANGED
@@ -5,8 +5,17 @@ module AmpHelper
|
|
5
5
|
initializer 'amp_helper.rb' do
|
6
6
|
<<-FILE.strip_heredoc
|
7
7
|
AmpHelper.configure do |config|
|
8
|
+
# Enable amp image.
|
9
|
+
# Default: true
|
10
|
+
# config.enable_amp_image = true
|
11
|
+
|
8
12
|
# Configure ratina version name format For CarrierWave::Uploader
|
13
|
+
# Default: nil
|
9
14
|
# config.format_2x = '%s_2x'
|
15
|
+
|
16
|
+
# Enable amp cache link.
|
17
|
+
# Default: true
|
18
|
+
# config.enable_amp_link = Rails.env.production?
|
10
19
|
end
|
11
20
|
FILE
|
12
21
|
end
|
data/lib/amp_helper/railtie.rb
CHANGED
@@ -2,11 +2,13 @@ require 'carrierwave'
|
|
2
2
|
require 'fastimage'
|
3
3
|
require 'amp_helper/configuration'
|
4
4
|
require 'amp_helper/amp_image_tag_helper'
|
5
|
+
require 'amp_helper/amp_link_to_helper'
|
5
6
|
|
6
7
|
module AmpHelper
|
7
8
|
class Railtie < Rails::Railtie
|
8
|
-
initializer '
|
9
|
+
initializer 'amp_helper' do |app|
|
9
10
|
ActionView::Base.send :include, AmpImageTagHelper
|
11
|
+
ActionView::Base.send :include, AmpLinkToHelper
|
10
12
|
end
|
11
13
|
|
12
14
|
generators do
|
data/lib/amp_helper/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: amp_helper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 1.0.0.pre.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Awjecc
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-01-
|
11
|
+
date: 2017-01-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -128,6 +128,7 @@ files:
|
|
128
128
|
- bin/setup
|
129
129
|
- lib/amp_helper.rb
|
130
130
|
- lib/amp_helper/amp_image_tag_helper.rb
|
131
|
+
- lib/amp_helper/amp_link_to_helper.rb
|
131
132
|
- lib/amp_helper/configuration.rb
|
132
133
|
- lib/amp_helper/generator.rb
|
133
134
|
- lib/amp_helper/railtie.rb
|
@@ -147,9 +148,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
147
148
|
version: '0'
|
148
149
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
149
150
|
requirements:
|
150
|
-
- - "
|
151
|
+
- - ">"
|
151
152
|
- !ruby/object:Gem::Version
|
152
|
-
version:
|
153
|
+
version: 1.3.1
|
153
154
|
requirements: []
|
154
155
|
rubyforge_project:
|
155
156
|
rubygems_version: 2.2.2
|