Skip to content

Commit 9a4e2c4

Browse files
author
Mike Ng
committed
Initial commit.
0 parents  commit 9a4e2c4

14 files changed

+361
-0
lines changed

.eslintrc.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
module.exports = {
2+
"globals": {
3+
"console": true,
4+
"exports": true,
5+
"mocha": true,
6+
"it": true,
7+
"describe": true,
8+
"before": true,
9+
"beforeEach": true,
10+
"after": true,
11+
"afterEach": true,
12+
"sinon": true
13+
},
14+
"rules": {
15+
"strict": 0,
16+
"quotes": [1, "single"],
17+
"no-native-reassign": 0,
18+
"camelcase": 0,
19+
"dot-notation": 0,
20+
"no-debugger": 1,
21+
"comma-dangle": [0, "always-multiline"],
22+
"no-underscore-dangle": 0,
23+
"no-unused-vars": [1, {"vars": "all", "args": "none"}],
24+
"no-trailing-spaces": 1,
25+
"key-spacing": 1,
26+
"no-unused-expressions": 1,
27+
"no-multi-spaces": 1,
28+
"no-use-before-define": 0,
29+
"space-infix-ops": 1,
30+
"no-console": 0,
31+
"comma-spacing": 1,
32+
"no-alert": 1,
33+
"no-empty": 1,
34+
"no-extra-bind": 1,
35+
"eol-last": 1,
36+
"eqeqeq": 1,
37+
"semi": 1,
38+
"no-multi-str": 0,
39+
"no-extra-semi": 1,
40+
"new-cap": 0,
41+
"consistent-return": 0,
42+
"no-extra-boolean-cast": 0,
43+
"no-mixed-spaces-and-tabs": 1,
44+
"no-shadow": 1,
45+
"no-sequences": 1,
46+
"handle-callback-err": 1
47+
}
48+
}

.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.DS_STORE
2+
npm-debug.log
3+
node_modules
4+
5+
coverage/
6+
7+
.idea/*

.npmignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.DS_STORE
2+
.coveralls.yml
3+
.travis.yml
4+
npm-debug.log
5+
node_modules
6+
dist
7+
coverage/
8+
9+
.idea/*

.travis.yml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
language: node_js
2+
node_js:
3+
- "5"
4+
- "0.10"
5+
- "node"
6+
branches:
7+
only:
8+
- master
9+
install: "npm install"
10+
script:
11+
- "npm test"

CHANGELOG

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-------------------------------------------------------------------------------
2+
0.1.0
3+
-------------------------------------------------------------------------------
4+
* Beta release of the Javascript SDK for our Optimizely testing solution
5+
-------------------------------------------------------------------------------

CONTRIBUTING.md

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#Contributing to the Optimizely JavaScript SDK
2+
3+
We welcome contributions and feedback! Please read the [README](README.md) to set up your development environment, then read the guidelines below for information on submitting your code.
4+
5+
## Development process
6+
7+
1. Create a branch off of `master`: `git checkout -b YOUR_NAME/branch_name`.
8+
2. Commit your changes. Make sure to add tests!
9+
3. Run `npm run lint` to ensure there are no lint errors.
10+
4. `git push` your changes to GitHub.
11+
5. Make sure that all unit tests are passing and that there are no merge conflicts between your branch and `master`.
12+
6. Open a pull request from `YOUR_NAME/branch_name` to `master`.
13+
7. A repository maintainer will review your pull request and, if all goes well, merge it!
14+
15+
##Pull request acceptance criteria
16+
17+
* **All code must have test coverage.** We use Mocha's chai assertion library and Sinon. Changes in functionality should have accompanying unit tests. Bug fixes should have accompanying regression tests.
18+
* Tests are located in the `tests.js` file.
19+
* Please don't change the `package.json` or `VERSION`. We'll take care of bumping the version when we next release.
20+
* Lint your code with our `npm run lint` before submitting.
21+
22+
##Style
23+
To enforce style rules, we use ESLint. See our [.eslintrc.js](.eslintrc.js) for more information on our specific style rules.
24+
25+
##License
26+
27+
By contributing your code, you agree to license your contribution under the terms of the [Apache License v2.0](http://www.apache.org/licenses/LICENSE-2.0). Your contributions should also include the following header:
28+
29+
```
30+
/**
31+
* Copyright 2016, Optimizely
32+
*
33+
* Licensed under the Apache License, Version 2.0 (the "License");
34+
* you may not use this file except in compliance with the License.
35+
* You may obtain a copy of the License at
36+
*
37+
* http://www.apache.org/licenses/LICENSE-2.0
38+
*
39+
* Unless required by applicable law or agreed to in writing, software
40+
* distributed under the License is distributed on an "AS IS" BASIS,
41+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
42+
* See the License for the specific language governing permissions and
43+
* limitations under the License.
44+
*/
45+
```
46+
47+
##Contact
48+
If you have questions, please contact [email protected].

LICENSE

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Copyright 2016 Optimizely
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.

README.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#Optimizely JavaScript SDK
2+
3+
This repository houses the JavaScript SDK for Optimizely's server-side testing product, which is currently in private beta.
4+
5+
##Getting Started
6+
7+
###Installing the SDK
8+
9+
The SDK is available through [npm](https://npmjs.com/package/optimizely-client-sdk). To install:
10+
11+
```
12+
npm install optimizely-client-sdk --save
13+
```
14+
15+
###Using the SDK
16+
See the Optimizely server-side testing [developer documentation](http://developers.optimizely.com/server/reference/index) to learn how to set up your first custom project and use the SDK. **Please note that you must be a member of the private server-side testing beta to create custom projects and use this SDK.**
17+
18+
##Development
19+
20+
###Installing dependencies
21+
22+
```npm install```
23+
24+
###Unit tests
25+
26+
You can run all unit tests with:
27+
```
28+
npm test
29+
```
30+
31+
###Contributing
32+
33+
Please see [CONTRIBUTING](CONTRIBUTING.md).

dist/optimizely.min.js

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
var _ = require('lodash/core');
2+
var configValidator = require('optimizely-server-sdk/lib/utils/config_validator');
3+
var defaultErrorHandler = require('optimizely-server-sdk/lib/plugins/error_handler');
4+
var defaultEventDispatcher = require('./lib/plugins/event_dispatcher');
5+
var enums = require('optimizely-server-sdk/lib/utils/enums');
6+
var logger = require('optimizely-server-sdk/lib/plugins/logger');
7+
8+
var Optimizely = require('optimizely-server-sdk/lib/optimizely');
9+
10+
/**
11+
* Entry point into the Optimizely Node testing SDK
12+
*/
13+
module.exports = {
14+
/**
15+
* Creates an instance of the Optimizely class
16+
* @param {Object} config
17+
* @param {Object} config.datafile
18+
* @param {Object} config.errorHandler
19+
* @param {Object} config.eventDispatcher
20+
* @param {Object} config.logger
21+
* @return {Object} the Optimizely object
22+
* @throws If any of the config options that were passed in are invalid
23+
*/
24+
createInstance: function(config) {
25+
var defaultLogger = logger.createLogger({ logLevel: enums.LOG_LEVEL.INFO });
26+
if (config) {
27+
try {
28+
configValidator.validate(config);
29+
} catch (ex) {
30+
defaultLogger.log(enums.LOG_LEVEL.ERROR, ex.message);
31+
}
32+
}
33+
34+
config = _.assignIn({
35+
errorHandler: defaultErrorHandler,
36+
eventDispatcher: defaultEventDispatcher,
37+
logger: logger.createLogger({ logLevel: enums.LOG_LEVEL.INFO }),
38+
}, config);
39+
40+
return new Optimizely(config);
41+
}
42+
};

lib/plugins/event_dispatcher/index.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
var _ = require('lodash/core');
2+
var bluebird = require('bluebird');
3+
4+
module.exports = {
5+
/**
6+
* Sample event dispatcher implementation for tracking impression and conversions
7+
* Users of the SDK can provide their own implementation
8+
* @param {string} url
9+
* @param {Object} params
10+
* @return {Promise<Object>}
11+
*/
12+
dispatchEvent: function(url, params) {
13+
return new bluebird(function(resolve, reject) {
14+
if (params) {
15+
url += '?' + toQueryString(params);
16+
}
17+
18+
var req = new XMLHttpRequest();
19+
req.open('GET', url, true);
20+
req.addEventListener('load', function(evt) {
21+
var responseObj = JSON.parse(evt.target.responseText);
22+
resolve(responseObj);
23+
});
24+
req.send();
25+
});
26+
},
27+
};
28+
29+
var toQueryString = function(obj) {
30+
return _.map(obj, function(v, k){
31+
return encodeURIComponent(k) + '=' + encodeURIComponent(v);
32+
}).join('&');
33+
};

package.json

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "optimizely-client-sdk",
3+
"version": "0.1.1",
4+
"description": "Javascript SDK for client testing",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "./node_modules/.bin/mocha ./tests.js",
8+
"lint": "eslint lib/**"
9+
},
10+
"repository": {
11+
"type": "git",
12+
"url": "git+https://github.com/optimizely/javascript-sdk.git"
13+
},
14+
"keywords": [
15+
"optimizely"
16+
],
17+
"bugs": {
18+
"url": "https://github.com/optimizely/javascript-sdk/issues"
19+
},
20+
"homepage": "https://github.com/optimizely/javascript-sdk#readme",
21+
"dependencies": {
22+
"bluebird": "^3.4.0",
23+
"lodash": "^4.13.1",
24+
"optimizely-server-sdk": "^0.1.0"
25+
},
26+
"devDependencies": {
27+
"chai": "^3.5.0",
28+
"eslint": "^2.9.0",
29+
"json-loader": "^0.5.4",
30+
"mocha": "^2.5.3",
31+
"sinon": "^1.17.4",
32+
"webpack": "^1.13.1"
33+
}
34+
}

tests.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
var configValidator = require('optimizely-server-sdk/lib/utils/config_validator');
2+
var Optimizely = require('optimizely-server-sdk/lib/optimizely');
3+
var optimizelyFactory = require('./');
4+
5+
var chai = require('chai');
6+
var assert = chai.assert;
7+
var sinon = require('sinon');
8+
9+
describe('optimizely-testing-sdk-javascript', function() {
10+
describe('APIs', function() {
11+
describe('createInstance', function() {
12+
var fakeErrorHandler = { handleError: function() {}};
13+
var fakeEventDispatcher = { dispatchEvent: function() {}};
14+
var fakeLogger = { log: function() {}};
15+
16+
beforeEach(function() {
17+
sinon.spy(console, 'error');
18+
sinon.stub(configValidator, 'validate');
19+
});
20+
21+
afterEach(function() {
22+
console.error.restore();
23+
configValidator.validate.restore();
24+
});
25+
26+
it('should not throw if the provided config is not valid', function() {
27+
configValidator.validate.throws(new Error('Invalid config or something'));
28+
assert.doesNotThrow(function() {
29+
optimizelyFactory.createInstance({
30+
datafile: {},
31+
});
32+
});
33+
});
34+
35+
it('should create an instance of optimizely', function() {
36+
var optlyInstance = optimizelyFactory.createInstance({
37+
datafile: {},
38+
errorHandler: fakeErrorHandler,
39+
eventDispatcher: fakeEventDispatcher,
40+
logger: fakeLogger,
41+
});
42+
43+
assert.instanceOf(optlyInstance, Optimizely);
44+
});
45+
});
46+
});
47+
});

webpack.config.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var webpack = require('webpack');
2+
3+
module.exports = {
4+
module: {
5+
loaders: [
6+
{ test: /\.json/, loader: 'json-loader' },
7+
],
8+
},
9+
plugins: [
10+
new webpack.optimize.UglifyJsPlugin({
11+
compress: {
12+
warnings: false,
13+
},
14+
output: {
15+
comments: false,
16+
},
17+
}),
18+
new webpack.optimize.DedupePlugin(),
19+
],
20+
entry: './index.js',
21+
output: {
22+
filename: 'optimizely.min.js',
23+
path: './dist'
24+
},
25+
};

0 commit comments

Comments
 (0)