diff --git a/lib/resources/asset.js b/lib/resources/asset.js index f9a49bf8..d4e5ed20 100644 --- a/lib/resources/asset.js +++ b/lib/resources/asset.js @@ -1,13 +1,55 @@ // Generated by CoffeeScript 1.10.0 (function() { - var Asset; + var Asset, BaseChild, + extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, + hasProp = {}.hasOwnProperty; - Asset = (function() { - function Asset() {} + BaseChild = require('./base_child'); + + Asset = (function(superClass) { + extend(Asset, superClass); + + Asset.prototype.parent = '/themes'; + + Asset.prototype.child = '/assets'; + + Asset.prototype.slug = 'asset'; + + function Asset(site) { + Asset.__super__.constructor.call(this, site); + } + + Asset.prototype.create = function(parentId, fields, callback) { + var url; + url = this.resource.queryString(this.prefix + "/" + parentId + this.child); + return this.resource.put(url, this.slug, fields, callback); + }; + + Asset.prototype.update = Asset.prototype.create; + + Asset.prototype.get = function(parentId, params, callback) { + var url; + params.asset || (params.asset = { + key: params.key + }); + delete params.key; + url = this.resource.queryString(this.prefix + "/" + parentId + this.child, params); + return this.resource.get(url, this.slug, callback); + }; + + Asset.prototype["delete"] = function(parentId, key, callback) { + var url; + url = this.resource.queryString(this.prefix + "/" + parentId + this.child, { + asset: { + key: key + } + }); + return this.resource["delete"](url, key, callback); + }; return Asset; - })(); + })(BaseChild); module.exports = Asset; diff --git a/src/resources/asset.coffee b/src/resources/asset.coffee index 2e4552d0..6d183004 100644 --- a/src/resources/asset.coffee +++ b/src/resources/asset.coffee @@ -1,3 +1,28 @@ -class Asset +BaseChild = require './base_child' + +class Asset extends BaseChild + parent: '/themes' + child: '/assets' + slug: 'asset' + + constructor: (site) -> + super(site) + + create: (parentId, fields, callback) -> + url = @resource.queryString "#{@prefix}/#{parentId}#{@child}" + @resource.put url, @slug, fields, callback + + update: @::create + + get: (parentId, params, callback) -> + params.asset || (params.asset = { key: params.key }) + delete params.key + + url = @resource.queryString "#{@prefix}/#{parentId}#{@child}", params + @resource.get url, @slug, callback + + delete: (parentId, key, callback) -> + url = @resource.queryString "#{@prefix}/#{parentId}#{@child}", { asset: { key: key } } + @resource.delete url, key, callback module.exports = Asset diff --git a/test/asset_test.js b/test/asset_test.js new file mode 100644 index 00000000..7d5ca1fa --- /dev/null +++ b/test/asset_test.js @@ -0,0 +1,128 @@ +var common = require('./common.js') + , scope = common.nock(common.test_shop) + , fixtures = {} + , resource; + +common.setObject('asset'); + +[ + 'allResponseBody', + 'singleResponseBody', + 'createRequestBody', + 'createResponseBody', + 'updateRequestBody', + 'updateResponseBody' +].forEach(function (fixture) { + fixtures[fixture] = common.load(fixture); +}); + +resource = new (common.resource())(common.endpoint); + +describe('Asset', function () { + it('should get a list of all assets', function (next) { + var resBody = fixtures.allResponseBody; + + scope.get('/admin/themes/828155753/assets.json') + .reply(200, resBody); + + resource.all(828155753, function (err, res) { + if (err) return next(err); + + res.should.be.eql(resBody.assets); + next(); + }); + }); + + it('should get a single asset by its key (1/2)', function (next) { + var resBody = fixtures.singleResponseBody; + var path = '/admin/themes/828155753/assets.json?' + + 'theme_id=828155753&asset%5Bkey%5D=templates%2Findex.liquid'; + + scope.get(path).reply(200, resBody); + + resource.get(828155753, { + key: 'templates/index.liquid', + theme_id: 828155753 + }, function (err, res) { + if (err) return next(err); + + res.should.be.eql(resBody.asset); + next(); + }); + }); + + it('should get a single asset by its key (2/2)', function (next) { + var resBody = fixtures.singleResponseBody; + var path = '/admin/themes/828155753/assets.json?' + + 'asset%5Bkey%5D=templates%2Findex.liquid&theme_id=828155753'; + + scope.get(path).reply(200, resBody); + + resource.get(828155753, { + asset: { key: 'templates/index.liquid' }, + theme_id: 828155753 + }, function (err, res) { + if (err) return next(err); + + res.should.be.eql(resBody.asset); + next(); + }); + }); + + it('should create an asset', function (next) { + var resBody = fixtures.createResponseBody + , reqBody = fixtures.createRequestBody; + + scope.put('/admin/themes/828155753/assets.json', reqBody) + .reply(200, resBody); + + resource.create(828155753, reqBody.asset, function (err, res) { + if (err) return next(err); + + res.should.be.eql(resBody.asset); + next(); + }); + }); + + it('should update an asset', function (next) { + var resBody = fixtures.updateResponseBody + , reqBody = fixtures.updateRequestBody; + + scope.put('/admin/themes/828155753/assets.json', reqBody) + .reply(200, resBody); + + resource.update(828155753, reqBody.asset, function (err, res) { + if (err) return next(err); + + res.should.be.eql(resBody.asset); + next(); + }); + }); + + it('should remove an asset', function (next) { + var path = '/admin/themes/828155753/assets.json?' + + 'asset%5Bkey%5D=assets%2Fbg-body.gif'; + + scope.delete(path).reply(200, {}); + + resource.delete(828155753, 'assets/bg-body.gif', function (err, res) { + if (err) return next(err); + + res.should.be.exactly('assets/bg-body.gif'); + next(); + }); + }); + + it('should error when deleting an assest that cannot be deleted', function (next) { + var path = '/admin/themes/828155753/assets.json?' + + 'asset%5Bkey%5D=layout%2Ftheme.liquid'; + + scope.delete(path).reply(403, {}); + + resource.delete(828155753, 'layout/theme.liquid', function (err) { + err.should.be.an.Error(); + err.message.should.be.exactly('Status code 403'); + next(); + }); + }); +}); diff --git a/test/fixtures/asset/allResponseBody.json b/test/fixtures/asset/allResponseBody.json new file mode 100644 index 00000000..77fa2949 --- /dev/null +++ b/test/fixtures/asset/allResponseBody.json @@ -0,0 +1,211 @@ +{ + "assets": [ + { + "key": "assets/bg-body-green.gif", + "public_url": "https://cdn.shopify.com/s/files/1/0006/9093/3842/t/1/assets/bg-body-green.gif?7364251289859092031", + "created_at": "2010-07-12T15:31:50-04:00", + "updated_at": "2010-07-12T15:31:50-04:00", + "content_type": "image/gif", + "size": 1542, + "theme_id": 828155753 + }, + { + "key": "assets/bg-body-orange.gif", + "public_url": "https://cdn.shopify.com/s/files/1/0006/9093/3842/t/1/assets/bg-body-orange.gif?7364251289859092031", + "created_at": "2010-07-12T15:31:50-04:00", + "updated_at": "2010-07-12T15:31:50-04:00", + "content_type": "image/gif", + "size": 1548, + "theme_id": 828155753 + }, + { + "key": "assets/bg-body-pink.gif", + "public_url": "https://cdn.shopify.com/s/files/1/0006/9093/3842/t/1/assets/bg-body-pink.gif?7364251289859092031", + "created_at": "2010-07-12T15:31:50-04:00", + "updated_at": "2010-07-12T15:31:50-04:00", + "content_type": "image/gif", + "size": 1562, + "theme_id": 828155753 + }, + { + "key": "assets/bg-body.gif", + "public_url": "https://cdn.shopify.com/s/files/1/0006/9093/3842/t/1/assets/bg-body.gif?7364251289859092031", + "created_at": "2010-07-12T15:31:50-04:00", + "updated_at": "2010-07-12T15:31:50-04:00", + "content_type": "image/gif", + "size": 1571, + "theme_id": 828155753 + }, + { + "key": "assets/bg-content.gif", + "public_url": "https://cdn.shopify.com/s/files/1/0006/9093/3842/t/1/assets/bg-content.gif?7364251289859092031", + "created_at": "2010-07-12T15:31:50-04:00", + "updated_at": "2010-07-12T15:31:50-04:00", + "content_type": "image/gif", + "size": 134, + "theme_id": 828155753 + }, + { + "key": "assets/bg-footer.gif", + "public_url": "https://cdn.shopify.com/s/files/1/0006/9093/3842/t/1/assets/bg-footer.gif?7364251289859092031", + "created_at": "2010-07-12T15:31:50-04:00", + "updated_at": "2010-07-12T15:31:50-04:00", + "content_type": "image/gif", + "size": 1434, + "theme_id": 828155753 + }, + { + "key": "assets/bg-main.gif", + "public_url": "https://cdn.shopify.com/s/files/1/0006/9093/3842/t/1/assets/bg-main.gif?7364251289859092031", + "created_at": "2010-07-12T15:31:50-04:00", + "updated_at": "2010-07-12T15:31:50-04:00", + "content_type": "image/gif", + "size": 297, + "theme_id": 828155753 + }, + { + "key": "assets/bg-sidebar.gif", + "public_url": "https://cdn.shopify.com/s/files/1/0006/9093/3842/t/1/assets/bg-sidebar.gif?7364251289859092031", + "created_at": "2010-07-12T15:31:50-04:00", + "updated_at": "2010-07-12T15:31:50-04:00", + "content_type": "image/gif", + "size": 124, + "theme_id": 828155753 + }, + { + "key": "assets/shop.css", + "public_url": "https://cdn.shopify.com/s/files/1/0006/9093/3842/t/1/assets/shop.css?7364251289859092031", + "created_at": "2010-07-12T15:31:50-04:00", + "updated_at": "2010-07-12T15:31:50-04:00", + "content_type": "text/css", + "size": 14058, + "theme_id": 828155753 + }, + { + "key": "assets/shop.css.liquid", + "public_url": "https://cdn.shopify.com/s/files/1/0006/9093/3842/t/1/assets/shop.css.liquid?7364251289859092031", + "created_at": "2010-07-12T15:31:50-04:00", + "updated_at": "2010-07-12T15:31:50-04:00", + "content_type": "text/x-liquid", + "size": 14675, + "theme_id": 828155753 + }, + { + "key": "assets/shop.js", + "public_url": "https://cdn.shopify.com/s/files/1/0006/9093/3842/t/1/assets/shop.js?7364251289859092031", + "created_at": "2010-07-12T15:31:50-04:00", + "updated_at": "2010-07-12T15:31:50-04:00", + "content_type": "application/javascript", + "size": 348, + "theme_id": 828155753 + }, + { + "key": "assets/sidebar-devider.gif", + "public_url": "https://cdn.shopify.com/s/files/1/0006/9093/3842/t/1/assets/sidebar-devider.gif?7364251289859092031", + "created_at": "2010-07-12T15:31:50-04:00", + "updated_at": "2010-07-12T15:31:50-04:00", + "content_type": "image/gif", + "size": 1016, + "theme_id": 828155753 + }, + { + "key": "assets/sidebar-menu.jpg", + "public_url": "https://cdn.shopify.com/s/files/1/0006/9093/3842/t/1/assets/sidebar-menu.jpg?7364251289859092031", + "created_at": "2010-07-12T15:31:50-04:00", + "updated_at": "2010-07-12T15:31:50-04:00", + "content_type": "image/jpeg", + "size": 1609, + "theme_id": 828155753 + }, + { + "key": "config/settings_data.json", + "public_url": null, + "created_at": "2010-07-12T15:31:50-04:00", + "updated_at": "2010-07-12T15:31:50-04:00", + "content_type": "application/json", + "size": 4570, + "theme_id": 828155753 + }, + { + "key": "config/settings_schema.json", + "public_url": null, + "created_at": "2010-07-12T15:31:50-04:00", + "updated_at": "2010-07-12T15:31:50-04:00", + "content_type": "application/json", + "size": 4570, + "theme_id": 828155753 + }, + { + "key": "layout/theme.liquid", + "public_url": null, + "created_at": "2010-07-12T15:31:50-04:00", + "updated_at": "2010-07-12T15:31:50-04:00", + "content_type": "text/x-liquid", + "size": 3252, + "theme_id": 828155753 + }, + { + "key": "templates/article.liquid", + "public_url": null, + "created_at": "2010-07-12T15:31:50-04:00", + "updated_at": "2010-07-12T15:31:50-04:00", + "content_type": "text/x-liquid", + "size": 2486, + "theme_id": 828155753 + }, + { + "key": "templates/blog.liquid", + "public_url": null, + "created_at": "2010-07-12T15:31:50-04:00", + "updated_at": "2010-07-12T15:31:50-04:00", + "content_type": "text/x-liquid", + "size": 786, + "theme_id": 828155753 + }, + { + "key": "templates/cart.liquid", + "public_url": null, + "created_at": "2010-07-12T15:31:50-04:00", + "updated_at": "2010-07-12T15:31:50-04:00", + "content_type": "text/x-liquid", + "size": 2047, + "theme_id": 828155753 + }, + { + "key": "templates/collection.liquid", + "public_url": null, + "created_at": "2010-07-12T15:31:50-04:00", + "updated_at": "2010-07-12T15:31:50-04:00", + "content_type": "text/x-liquid", + "size": 946, + "theme_id": 828155753 + }, + { + "key": "templates/index.liquid", + "public_url": null, + "created_at": "2010-07-12T15:31:50-04:00", + "updated_at": "2010-07-12T15:31:50-04:00", + "content_type": "text/x-liquid", + "size": 1068, + "theme_id": 828155753 + }, + { + "key": "templates/page.liquid", + "public_url": null, + "created_at": "2010-07-12T15:31:50-04:00", + "updated_at": "2010-07-12T15:31:50-04:00", + "content_type": "text/x-liquid", + "size": 147, + "theme_id": 828155753 + }, + { + "key": "templates/product.liquid", + "public_url": null, + "created_at": "2010-07-12T15:31:50-04:00", + "updated_at": "2010-07-12T15:31:50-04:00", + "content_type": "text/x-liquid", + "size": 2796, + "theme_id": 828155753 + } + ] +} diff --git a/test/fixtures/asset/createRequestBody.json b/test/fixtures/asset/createRequestBody.json new file mode 100644 index 00000000..ba9875f5 --- /dev/null +++ b/test/fixtures/asset/createRequestBody.json @@ -0,0 +1,6 @@ +{ + "asset": { + "key": "assets/empty.gif", + "attachment": "R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==\n" + } +} diff --git a/test/fixtures/asset/createResponseBody.json b/test/fixtures/asset/createResponseBody.json new file mode 100644 index 00000000..ce36ef71 --- /dev/null +++ b/test/fixtures/asset/createResponseBody.json @@ -0,0 +1,11 @@ +{ + "asset": { + "key": "assets/empty.gif", + "public_url": "https://cdn.shopify.com/s/files/1/0006/9093/3842/t/1/assets/empty.gif?16418212202600161583", + "created_at": "2015-09-02T14:51:55-04:00", + "updated_at": "2015-09-02T14:51:55-04:00", + "content_type": "image/gif", + "size": 43, + "theme_id": 828155753 + } +} diff --git a/test/fixtures/asset/singleResponseBody.json b/test/fixtures/asset/singleResponseBody.json new file mode 100644 index 00000000..405f7de9 --- /dev/null +++ b/test/fixtures/asset/singleResponseBody.json @@ -0,0 +1,12 @@ +{ + "asset": { + "key": "templates/index.liquid", + "public_url": null, + "value": "\n
frontpage
and it will show up here.