Skip to content

Loading…

Add optional image cache expiry to the imageResize API #87

Merged
merged 6 commits into from

4 participants

@b1tr0t
Mobify Research & Development Inc. member

No description provided.

@b1tr0t
Mobify Research & Development Inc. member

@jansepar Can you have a look at this for me?

@jansepar jansepar commented on an outdated diff
api/resizeImages.js
@@ -26,6 +26,10 @@ var $ = Mobify.$
bits.push(projectId);
}
+ if (options.cacheHours && typeof(options.cacheHours) === 'number') {
@jansepar Mobify Research & Development Inc. member
jansepar added a note

If someone sets options.cacheHours as a string, then this will silently fail. Probably bad because I would imagine some people might set the value as a string, for example:

options.cacheHours = 27;
options.cacheHours = '27';

I feel like both of those should work. If the argument IS a string, we can check with isNaN or parseInt to determine if it is a valid number, and throw an exception if it is not a number.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@b1tr0t
Mobify Research & Development Inc. member

@jansepar Integrated your feedback

@johnboxall
Mobify Research & Development Inc. member

@b1tr0t we can rely on the backend to provide error reporting and for the most part, JavaScript's type conversion will do the right thing here.

@johnboxall johnboxall merged commit 17a78b7 into master

1 check passed

Details default
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 4, 2013
  1. @b1tr0t
  2. @b1tr0t

    * change api call to be more JavaScript-eze

    b1tr0t committed
    * add test
Commits on Jan 7, 2013
  1. @noahadams
  2. @b1tr0t

    * allow cache hours to be a string

    b1tr0t committed
    * add test for cache hours set with a string
  3. @b1tr0t
Commits on Jan 8, 2013
  1. @johnboxall
This page is out of date. Refresh to see the latest.
Showing with 41 additions and 13 deletions.
  1. +6 −3 api/resizeImages.js
  2. +35 −10 tests/index.html
View
9 api/resizeImages.js
@@ -19,8 +19,11 @@ var $ = Mobify.$
var bits = [defaults.host];
if (defaults.projectName) {
- var projectId = "project-" + defaults.projectName;
- bits.push(projectId);
+ bits.push("project-" + defaults.projectName);
+ }
+
+ if (options.cacheHours) {
+ bits.push('c' + options.cacheHours);
}
if (options.format) {
@@ -28,7 +31,7 @@ var $ = Mobify.$
}
if (options.maxWidth) {
- bits.push(options.maxWidth)
+ bits.push(options.maxWidth);
if (options.maxHeight) {
bits.push(options.maxHeight);
View
45 tests/index.html
@@ -449,6 +449,15 @@ <h2 id="qunit-userAgent"></h2>
equal(got, vow);
});
+ test('Mobify.getImageURL cached string', 2, function() {
+ var url = 'http://test/image.jpg';
+ var result1 = Mobify.getImageURL(url, {maxWidth: '320', cacheHours: '8'});
+ var result2 = Mobify.getImageURL(url, {maxWidth: 320, cacheHours: 8});
+ var expected = '//ir0.mobify.com/c8/320/' + url;
+ equal(result1, expected);
+ equal(result2, expected);
+ });
+
test('Mobify.getImageURL - projectName set on Mobify.conf.data', function() {
expect(1);
@@ -464,6 +473,22 @@ <h2 id="qunit-userAgent"></h2>
}
});
+ test('Mobify.getImageURL - projectName and cache expiry set', function() {
+ expect(1);
+
+ try {
+ Mobify.$.fn.resizeImages.defaults.projectName = 'testing';
+
+ got = Mobify.getImageURL('http://test/image.jpg',
+ {maxWidth: 320, cacheHours:8});
+ vow = "//ir0.mobify.com/project-testing/c8/320/http://test/image.jpg";
+
+ equal(got, vow);
+ } finally {
+ delete Mobify.$.fn.resizeImages.defaults.projectName;
+ }
+ });
+
module('enhance');
@@ -571,15 +596,15 @@ <h2 id="qunit-userAgent"></h2>
urlmatch = Mobify.urlmatch;
- /*
- * Takes a path, an urlmatch path expression or regular expression and an
- * expectation about whether these should match and calls qunit's
- * `equal()` on the result of running urlmatch with the given expression
+ /*
+ * Takes a path, an urlmatch path expression or regular expression and an
+ * expectation about whether these should match and calls qunit's
+ * `equal()` on the result of running urlmatch with the given expression
* window.location faked to the given path on http://a.com/
*/
testUrlMatch = function(path, expression, expected) {
var match, matched, symbol, url;
-
+
// Set up urlmatch with a mocked window object with a mocked location object
// set to `url`
url = "http://a.com" + path;
@@ -589,10 +614,10 @@ <h2 id="qunit-userAgent"></h2>
matched = !!match();
symbol = (expected != false ? "": "");
- expression = (typeof expression === 'string' ?
+ expression = (typeof expression === 'string' ?
'"' + expression + '"' : expression);
- equal(matched, expected,
+ equal(matched, expected,
symbol + " : " + path + " : " + expression);
};
@@ -626,7 +651,7 @@ <h2 id="qunit-userAgent"></h2>
testUrlMatch("/abb/c", "/a.b/c", false);
testUrlMatch("/aab/c", "/a+b/*/", false);
testUrlMatch("/a$b/c", "/a$b/*", true);
-
+
// RegExp test suite
testUrlMatch("/a", /a/, true);
testUrlMatch("/a/", /^\/a\//, true);
@@ -636,9 +661,9 @@ <h2 id="qunit-userAgent"></h2>
testUrlMatch("/a/some/other/other/stuff/b/", /^\/a.*b(\/)?$/, true)
// Things other than path expressions and RegExps
- equal(urlmatch({}), false,
+ equal(urlmatch({}), false,
"passing the empty object to urlmatch makes it return false");
- equal(urlmatch("/a/b/c")(5), false,
+ equal(urlmatch("/a/b/c")(5), false,
"passing a number to urlmatch makes it return false");
}); //test
Something went wrong with that request. Please try again.