ir_helper 0.2.0 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5e10d8fb83d7346b07073b7fd54f27c48e769b70
4
- data.tar.gz: 393df95bbd2b9fe1e2740a7b8cbe91a3fb19009b
3
+ metadata.gz: 6a4d7140747ce0fd13a2ac7ada02ddbb5ec65f72
4
+ data.tar.gz: 26f00987d59e1f968bda514cb3047c33c43484ba
5
5
  SHA512:
6
- metadata.gz: e2548c6e1739347dc163191011b22692fc5233fd9e9df51b15c3e057c7b1025402a176740dc86aaf1ab8937598b8548ad1b618e8df7c902a07ebf20cb9e7ce7c
7
- data.tar.gz: e4e9767006f1b435297b12bd588a37dd8eeafb5ad39e218ffae0ba130ef0016a793f4097d0a2f8d8134415ddee92e93ec2097c3546ad93aeabd63d335614fadc
6
+ metadata.gz: c89b42fdc67710de9731b235aa30b5f1822e4064da179fb8be0d43e8804527b223592f3b63da7befb997ea3bf9b9ae7227f61d65540844b6b6d2082fd2bd9941
7
+ data.tar.gz: f85378405c8d84fb71d0e393d12ecc8daa677a87280d142977a1637ee190293655b78ceb17cd8725c355f4cbe0d5a1f4638d0b909878aa98167265cdb8342c14
data/README.md CHANGED
@@ -6,7 +6,7 @@ This gem includes helpers to build the appropriate URLs for images served via an
6
6
 
7
7
  Add this line to your application's Gemfile:
8
8
 
9
- gem 'ir-helper'
9
+ gem 'ir_helper'
10
10
 
11
11
  And then execute:
12
12
 
@@ -14,7 +14,7 @@ And then execute:
14
14
 
15
15
  Or install it yourself as:
16
16
 
17
- $ gem install ir-helper
17
+ $ gem install ir_helper
18
18
 
19
19
  ## Configuration
20
20
  Configuring this gem is done via:
@@ -76,6 +76,8 @@ Javascript helper for building `image-resizer` url endpoints
76
76
  if (uri && urlDomain(uri.hostname) === 'facebook'){
77
77
  modArr.push('efacebook');
78
78
  }
79
+ modArr = cdnModArr(uri, modArr);
80
+ modArr = modArr.sort();
79
81
  return modArr.length > 0 ? '/' + modArr.join('-') : '';
80
82
  };
81
83
 
@@ -103,9 +105,40 @@ Javascript helper for building `image-resizer` url endpoints
103
105
  return modArr;
104
106
  };
105
107
 
108
+ function cdnModArr(uri, modArr){
109
+ var modStr, mods, i;
110
+
111
+ if (uri && urlDomain(uri.host) === 'cdn'){
112
+ modStr = uri.pathname.split('/')[1];
113
+ if (hasModifierStr(modStr)){
114
+ mods = modStr.split('-');
115
+
116
+ // if no mods are specified then use those from the existing url
117
+ if (modArr.length === 0){
118
+ modArr = mods;
119
+ }
120
+
121
+ // if there are mods specified then only add any existing external
122
+ // source or filter modifiers
123
+ else {
124
+ for (i=0; i < mods.length; i++){
125
+ if (mods[i][0] === 'e' || mods[i][0] === 'f'){
126
+ modArr.push(mods[i]);
127
+ }
128
+ }
129
+ }
130
+ }
131
+ }
132
+
133
+ return modArr;
134
+ };
135
+
106
136
  function buildPath(uri, modifiers){
107
137
  if(uri){
108
138
  switch(urlDomain(uri.hostname)){
139
+ case 'cdn':
140
+ return cdnObject(uri);
141
+ break
109
142
  case 's3':
110
143
  return s3Object(uri);
111
144
  break;
@@ -125,7 +158,11 @@ Javascript helper for building `image-resizer` url endpoints
125
158
  };
126
159
 
127
160
  function urlDomain(host){
128
- var domain = 'other';
161
+ var domain = 'other',
162
+ cdnRe = new RegExp(URI(IR.CDN).hostname, 'i');
163
+ if (cdnRe.test(host)){
164
+ domain = 'cdn';
165
+ }
129
166
  if (/s3.amazonaws.com/i.test(host)){
130
167
  domain = 's3';
131
168
  }
@@ -135,6 +172,44 @@ Javascript helper for building `image-resizer` url endpoints
135
172
  return domain;
136
173
  };
137
174
 
175
+ // Get the path of an already IR'd url, basically make sure we remove the
176
+ // modifier string if it is present
177
+ function cdnObject(uri){
178
+ var parts = uri.pathname.split('/');
179
+
180
+ // test to see if there is an existing modifier string in place
181
+ if (hasModifierStr(parts[1])){
182
+ return '/' + parts.slice(2).join('/');
183
+ }
184
+
185
+ return uri.pathname;
186
+ };
187
+
188
+ function hasModifierStr(part){
189
+ var items, i, item, key, value, flag, v;
190
+
191
+ items = part.split('-');
192
+ for (i in items){
193
+ item = items[i];
194
+ key = item[0];
195
+ value = item.slice(1);
196
+ flag = false;
197
+ v = modSet(key);
198
+ if (v){
199
+ if (v.values){
200
+ flag = true;
201
+ } else {
202
+ flag = 1 * value > 0;
203
+ }
204
+ }
205
+ if (flag){
206
+ return true;
207
+ }
208
+ }
209
+
210
+ return false;
211
+ };
212
+
138
213
  function s3Object(uri){
139
214
  if (uri.hostname === 's3.amazonaws.com'){
140
215
  return '/' + uri.pathname.split('/').slice(2).join('/');
@@ -46,7 +46,7 @@ module IrHelper
46
46
 
47
47
  def mod_set(key)
48
48
  mods.each do |k, v|
49
- return v if key == k || v[:alias] == key
49
+ return v if key.to_sym == k || v[:alias] == key
50
50
  end
51
51
  nil
52
52
  end
@@ -84,6 +84,8 @@ module IrHelper
84
84
  end
85
85
  mod_arr << 'efacebook' if uri && url_domain(uri.host) == :facebook
86
86
  mod_arr.compact
87
+ mod_arr = cdn_mod_arr(uri, mod_arr)
88
+ mod_arr = mod_arr.sort
87
89
  mod_arr.length > 0 ? "/#{mod_arr.join('-')}" : ''
88
90
  end
89
91
 
@@ -105,9 +107,33 @@ module IrHelper
105
107
  end
106
108
  end
107
109
 
110
+ def cdn_mod_arr(uri, mod_arr)
111
+ if uri && url_domain(uri.host) == :cdn
112
+ mod = uri.path.split('/')[1]
113
+ if has_modifier_str(mod)
114
+ mods = mod.split('-')
115
+ # if no mods are specified then use those from the existing url
116
+ if mod_arr.length == 0
117
+ mod_arr = mods
118
+
119
+ # if there are mods specified then only add any existing external
120
+ # source or filter modifiers
121
+ else
122
+ mods.each do |m|
123
+ mod_arr << m if ['e','f'].include?(m[0])
124
+ end
125
+ end
126
+ end
127
+ end
128
+
129
+ mod_arr
130
+ end
131
+
108
132
  def build_path(uri, modifiers)
109
133
  if uri
110
134
  case url_domain(uri.host)
135
+ when :cdn
136
+ cdn_object uri
111
137
  when :s3
112
138
  s3_object uri
113
139
  when :facebook
@@ -123,11 +149,44 @@ module IrHelper
123
149
  end
124
150
 
125
151
  def url_domain(host)
152
+ return :cdn if /#{URI(cdn).host}/i =~ host
126
153
  return :s3 if /s3.amazonaws.com/i =~ host
127
154
  return :facebook if /facebook.com/i =~ host
128
155
  :other
129
156
  end
130
157
 
158
+ # Get the path of an already IR'd url, basically make sure we remove the
159
+ # modifier string if it is present
160
+ def cdn_object(uri)
161
+ parts = uri.path.split('/')
162
+
163
+ # test to see if there is an existing modifier string in place
164
+ if has_modifier_str(parts[1])
165
+ "/#{parts[2..-1].join('/')}"
166
+ else
167
+ uri.path
168
+ end
169
+ end
170
+
171
+ def has_modifier_str(part)
172
+ part.split('-').each do |item|
173
+ key = item[0]
174
+ value = item[1..-1]
175
+ flag = if v = mod_set(key)
176
+ if v.include?(:values)
177
+ v[:values].include?(value)
178
+ else
179
+ value.to_i > 0
180
+ end
181
+ else
182
+ false
183
+ end
184
+ return true if flag
185
+ end
186
+
187
+ false
188
+ end
189
+
131
190
  def s3_object(uri)
132
191
  # test to see which type of s3 url we have
133
192
  if uri.host == 's3.amazonaws.com'
@@ -1,3 +1,3 @@
1
1
  module IrHelper
2
- VERSION = '0.2.0'
2
+ VERSION = '0.3.1'
3
3
  end
data/lib/ir_helper.rb CHANGED
@@ -17,7 +17,7 @@ module IrHelper
17
17
  @ir_image_tag = nil
18
18
  @ir_background = nil
19
19
  @ir_url = nil
20
- @js_class = 'ImageResizer'
20
+ @js_class = 'IR'
21
21
  @js_image_tag = nil
22
22
  @js_background = nil
23
23
  @js_url = nil
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ir_helper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Nicol