Skip to content
Browse files

added structure for sprd

  • Loading branch information...
1 parent 3a2a5da commit 99fbe1fb0584743a60453eb02991d6c79c0c38ab @it-ony it-ony committed
Showing with 207 additions and 0 deletions.
  1. +2 −0 README.md
  2. +43 −0 package.json
  3. +162 −0 sprd/data/SprdApiDataSource.js
View
2 README.md
@@ -0,0 +1,2 @@
+# rAppid.js-sprd - rAppid.js library for accessing Spreadshirt API
+
View
43 package.json
@@ -0,0 +1,43 @@
+{
+ "name": "rAppid.js-sprd",
+ "preferGlobal": "false",
+ "version": "0.1.0",
+ "author": "rAppid <support@rappidjs.com> (http://rappidjs.com)",
+ "contributors": [
+ "Tony Findeisen <tony@rappidjs.com>",
+ "Marcus Krejpowicz <marcus@rappidjs.com>"
+ ],
+ "description": "Spreadshirt library for rAppid.js.",
+ "bin": {
+ "rappidjs": "./bin/rappidjs"
+ },
+ "lib": "sprd",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/it-ony/rAppid.js-sprd.git"
+ },
+ "keywords": [
+ "rAppid",
+ "sprd",
+ "Spreadshirt"
+ ],
+ "dependencies": {
+ "rAppid.js": "latest"
+ },
+ "devDependencies": {
+ "chai": "*",
+ "mocha": "*"
+ },
+ "license": "CC BY-NC-SA 3.0",
+ "engine": {
+ "node": ">=0.4"
+ },
+ "engines": {
+ "node": "*"
+ },
+ "homepage": "http://rappidjs.com",
+ "optionalDependencies": {},
+ "rAppidDependencies": {
+ "rAppid.js": "latest"
+ }
+}
View
162 sprd/data/SprdApiDataSource.js
@@ -0,0 +1,162 @@
+var requirejs = (typeof requirejs === "undefined" ? require("requirejs") : requirejs);
+
+requirejs(["rAppid"], function (rAppid) {
+ rAppid.defineClass("sprd.data.SprdApiDataSource", ["js.data.RestDataSource"], function (RestDataSource) {
+
+ var SprdApiContext = RestDataSource.RestContext.inherit({
+ getQueryParameter: function () {
+
+ var parameter = {
+ mediaType: "json"
+ };
+
+ if (this.$properties && this.$properties.locale) {
+ parameter.locale = this.$properties.locale;
+ }
+
+ return rAppid._.defaults(parameter, this.callBase());
+ }
+ });
+
+ var ShopContext = SprdApiContext.inherit({
+ // some sugar for the developer here
+ getPathComponents: function () {
+ return ['shops', this.$properties.shopId];
+ }
+ });
+
+ var UserContext = SprdApiContext.inherit({
+ // some sugar for the developer here
+ getPathComponents: function () {
+ return ['users', this.$properties.userId];
+ }
+ });
+
+ var contextParameterExtractor = /.*(shops|users)\/(\d+)\/.+$/i;
+
+ return RestDataSource.inherit({
+
+ ctor: function () {
+ this.callBase();
+ },
+
+ defaults: {
+ endPoint: "http://api.spreadshirt.net/api/v1",
+ gateway: "api",
+ apiKey: null,
+ secret: null,
+ collectionPageSize: 100
+ },
+
+ initialize: function () {
+ this.callBase();
+
+ if (!this.$.apiKey) {
+ console.log("No apiKey for SprdApiDataSource definied");
+ }
+ },
+
+ createContext: function (properties, parentContext) {
+
+ if (properties) {
+ if (properties.shopId) {
+ return new ShopContext(this, properties, parentContext);
+ }
+
+ if (properties.userId) {
+ return new UserContext(this, properties, parentContext);
+ }
+ }
+
+ return new SprdApiContext(this, properties, parentContext);
+ },
+
+ createContextCacheId: function (properties, parentProperties) {
+ // only use locale from parentProperties
+ var parent = {};
+ if (parentProperties && parentProperties.locale) {
+ parent.locale = parentProperties.locale;
+ }
+
+ return this.callBase(properties, parent);
+ },
+
+ getContextPropertiesFromReference: function (reference) {
+
+ var match = contextParameterExtractor.exec(reference);
+ if (match) {
+ if (match[1] == "shops") {
+ return {
+ shopId: match[2]
+ };
+ }
+
+ if (match[1] == "users") {
+ return {
+ userId: match[2]
+ }
+ }
+ }
+
+ return null;
+ },
+
+ getQueryParams: function () {
+ return rAppid._.defaults({
+ mediaType: "json"
+ }, this.callBase());
+ },
+
+
+ extractListMetaData: function (list, payload, options) {
+ return payload;
+ },
+
+ extractListData: function (list, payload, options) {
+ for (var key in payload) {
+ if (payload.hasOwnProperty(key)) {
+ if (rAppid._.isArray(payload[key])) {
+ return payload[key];
+ }
+ }
+ }
+ },
+
+ shop: function (shopId, locale) {
+
+ shopId = shopId || this.$.shopId;
+ locale = locale || this.$.locale;
+
+ var properties = {
+ shopId: shopId
+ };
+
+ if (locale) {
+ properties.locale = locale;
+ }
+
+ return this.getContext(properties);
+ },
+ user: function (userId, locale) {
+ userId = userId || this.$.shopId;
+ locale = locale || this.$.locale;
+
+
+ var properties = {
+ userId: userId
+ };
+
+ if (locale) {
+ properties.locale = locale;
+ }
+
+ return this.getContext(properties);
+ },
+ root: function() {
+ return this.getContext();
+ }
+ });
+ })
+ ;
+})
+;

0 comments on commit 99fbe1f

Please sign in to comment.
Something went wrong with that request. Please try again.