amp_helper 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3ad84d0bbb94178a0f7be96928cca9b8b2b75a37
4
- data.tar.gz: 134099b58f77541746d254a053bd7a51bdee82e7
3
+ metadata.gz: fa951bbfd148bc82240773e639cbe2e49d9eff1d
4
+ data.tar.gz: 8f64417bbae8acffbecc2834a5329db999f9f508
5
5
  SHA512:
6
- metadata.gz: a3f8bcb342cfe5c072496c9ba6e59ea348a1f3d6eb395f6fc710c282336b3d33177103d3a73e59867baace26901090225583a45203fce7b6997a4b8feea64b93
7
- data.tar.gz: e587c1f12cb6484b53d0d582f8e2ef45de9c58b48d2b60874646d43e18a7abcdffd6d84b22ad6666506342214de7a511321e086f3d056e7a59a5e6d2c45d4ee0
6
+ metadata.gz: 9d9c548951e44e6451936402fac6640f4dfd0cf355ab351f6c27fd0826fe1b6997949fc15c3918531217b488b85db71b9565dee3df955bc177bb3f21198a57eb
7
+ data.tar.gz: dcaed01087a475e2677f376c4b2c55201b6f5ab5e7bc4e265996137ac76e50ea793a5f9b85a25be2106d6f0a3ad241760e8546c0f12de370abc82e2e3ac4b266
data/README.md CHANGED
@@ -1,8 +1,7 @@
1
1
  # AmpHelper
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/amp_helper`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ AmpHelper helps you generate html tags for AMP.
4
+ To know AmpHelper ability, just look Usage section.
6
5
 
7
6
  ## Installation
8
7
 
@@ -25,24 +24,46 @@ Or install it yourself as:
25
24
 
26
25
  ### Helpers
27
26
 
28
- #### amp_image_tag(source, options)
27
+ #### amp_image_tag(source, options = {})
28
+
29
+ ##### String Source
30
+
31
+ $ amp_image_tag('http://placehold.it/350x150', width: 20, height: 20)
32
+ #=> '<amp-img alt="350x150" height="20" src="http://placehold.it/350x150" width="20" /></amp-img>'
33
+
34
+ $ amp_image_tag('http://placehold.it/350x150', size: '20x20')
35
+ #=> '<amp-img alt="350x150" height="20" src="http://placehold.it/350x150" width="20" /></amp-img>'
36
+
37
+ $ amp_image_tag('http://placehold.it/350x150')
38
+ #=> '<amp-img alt="350x150" height="150" src="http://placehold.it/350x150" width="350" /></amp-img>'
29
39
 
30
- ##### Url with image's dimensions
40
+ ##### Carrierwave Source
31
41
 
32
- % amp_image_tag('http://sample.com/image.jpg', width: '40', height: '40')
33
- #=> '<amp-img alt="Image" height="40" src="http://sample.com/image.jpg" width="40" /></amp-img>'
42
+ $ amp_image_tag(ThumbUploader.new.square)
43
+ #=> '<amp-img alt="Square 350x150" height="20" src="http://placehold.it/square_350x150" width="20" /></amp-img>'
34
44
 
35
- ##### Url without image's dimensions
45
+ ##### Retina
36
46
 
37
- It's going to access file dimensions.
47
+ $ amp_image_tag('http://placehold.it/350x150', srcset: 'http://placehold.it/700x300', size: '20x20')
48
+ #=> '<amp-img alt="350x150" height="20" src="http://placehold.it/350x150" srcset="http://placehold.it/700x300" width="20" /></amp-img>'
38
49
 
39
- % amp_image_tag('http://sample.com/image.jpg')
40
- #=> '<amp-img alt="Image" height="400" src="http://sample.com/image.jpg" width="400" /></amp-img>'
50
+ $ amp_image_tag(ThumbUploader.new.square, format_2x: '%s_2x')
51
+ #=> '<amp-img alt="Square 350x150" height="20" src="http://placehold.it/square_350x150" srcset="http://placehold.it/square_2x_350x150" width="20" /></amp-img>'
41
52
 
42
- ##### Carrierwave
53
+ ##### ThumbUploader Sample
43
54
 
44
- % amp_image_tag(Article.find(1).thumb.icon.url, width: '30', height: '30')
45
- # => '<amp-img alt="Icon placeholder" height="30" src="https:g/s3-ap-northeast-1.amazonaws.com/foo/assets/placeholders/icon_placeholder.png" width="30" /></amp-img>'
55
+ class ThumbUploader < CarrierWave::Uploader::Base
56
+ storage :file
57
+ version :square do
58
+ process resize_to_fill: [20, 20]
59
+ end
60
+ version :square_2x do
61
+ process resize_to_fill: [40, 40]
62
+ end
63
+ def default_url(*args)
64
+ 'http://placehold.it/' + [version_name, '350x150'].compact.join('_')
65
+ end
66
+ end
46
67
 
47
68
  ## Development
48
69
 
data/amp_helper.gemspec CHANGED
@@ -23,6 +23,8 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency 'bundler', '~> 1.13'
24
24
  spec.add_development_dependency 'rake', '~> 10.0'
25
25
  spec.add_development_dependency 'rspec-rails', '~> 3.0'
26
+ spec.add_development_dependency 'pry', '~> 0.10.4'
26
27
  spec.add_dependency 'rails', '~> 4.1.8'
27
28
  spec.add_dependency 'fastimage', '~> 2.0.1'
29
+ spec.add_dependency 'carrierwave', '~> 1.0.0.rc'
28
30
  end
@@ -1,10 +1,32 @@
1
1
  module AmpImageTagHelper
2
2
  def amp_image_tag(source, opts = {})
3
- has_dimensions = !opts[:width].nil? && !opts[:height].nil?
4
- opts[:width], opts[:height] = FastImage.size(source) unless has_dimensions
5
- image_tag(source, opts).
6
- gsub(/^<img/, '<amp-img').
7
- gsub(/>$/, '></amp-img>').
8
- html_safe
3
+ has_dimensions = (opts[:width] && opts[:height]) || opts[:size]
4
+ set_dimensions(source, opts) if !has_dimensions
5
+ set_scrset(source, opts)
6
+ image_tag_to_amp(image_tag(source, opts))
7
+ end
8
+
9
+ private
10
+
11
+ def image_tag_to_amp(img_tag)
12
+ img_tag.gsub(/^<img/, '<amp-img').gsub(/>$/, '></amp-img>').html_safe
13
+ end
14
+
15
+ def set_dimensions(source, opts)
16
+ if source.kind_of?(CarrierWave::Uploader::Base) &&
17
+ !source.class.processors.empty?
18
+ opts[:width], opts[:height] = source.class.processors[0][1]
19
+ else
20
+ opts[:width], opts[:height] = FastImage.size(source.to_s)
21
+ end
22
+ end
23
+
24
+ def set_scrset(source, opts)
25
+ if source.kind_of?(CarrierWave::Uploader::Base) &&
26
+ !source.class.processors.empty? &&
27
+ format_2x = opts.delete(:format_2x)
28
+ name_2x = format_2x % source.version_name
29
+ opts[:srcset] = source.parent_version.send(name_2x).url
30
+ end
9
31
  end
10
32
  end
@@ -1,3 +1,4 @@
1
+ require 'carrierwave'
1
2
  require 'fastimage'
2
3
  require 'amp_helper/amp_image_tag_helper'
3
4
 
@@ -1,3 +1,3 @@
1
1
  module AmpHelper
2
- VERSION = "0.1.0"
2
+ VERSION = '0.1.1'
3
3
  end
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.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Awjecc
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-12-20 00:00:00.000000000 Z
11
+ date: 2017-01-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.10.4
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.10.4
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: rails
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -80,6 +94,20 @@ dependencies:
80
94
  - - "~>"
81
95
  - !ruby/object:Gem::Version
82
96
  version: 2.0.1
97
+ - !ruby/object:Gem::Dependency
98
+ name: carrierwave
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 1.0.0.rc
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 1.0.0.rc
83
111
  description:
84
112
  email:
85
113
  - awjecc@gmail.com