Skip to content

Integrate/with mock server #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/default.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
server:
url: localhost:3000
url: http://158.108.236.1:3000/api/event/oninit
cookies:
name: interact-device-code
28 changes: 28 additions & 0 deletions example/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var interact = require('..')
var express = require('express')
var cookieParser = require('cookie-parser')

var app = express()
app.use(cookieParser())
app.use(interact.express('IC9-55938-6', 'hashed-1234'))

app.get('/', function (request, response) {
// Cookies that have not been signed
console.log('Cookies: ', request.cookies)

// Cookies that have been signed
console.log('Signed Cookies: ', request.signedCookies)

// Access USERS
console.log('USERS', interact.USERS)
console.log('initCode', interact.INIT_CODE)

response.send(`
<h1>Hello World</h1>
<script>${interact.INIT_CODE}</script>
`)
})

app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})
43 changes: 43 additions & 0 deletions example/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<html>
<head>
<title>example - collector</title>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<div class="container">
<div class="nav">
<div class="nav-logo">Example</div>
<div class="nav-center">
<div interact-feature="card-1" interact-feature-type="A" class="feature-button">1A</div>
<div interact-feature="card-1" interact-feature-type="B" class="feature-button">1B</div>
<div interact-feature="card-2" interact-feature-type="A" class="feature-button">2A</div>
<div interact-feature="card-2" interact-feature-type="B" class="feature-button">2B</div>
</div>
</div>
<div class="content">
<div class="content-body">
<div interact-feature="card-1" interact-feature-type="A" class="feature feature-a">Feature 1A</div>
<div interact-feature="card-1" interact-feature-type="B" class="feature feature-b">Feature 1B</div>
<div interact-feature="card-2" interact-feature-type="A" class="feature feature-a">Feature 2A</div>
<div interact-feature="card-2" interact-feature-type="B" class="feature feature-b">Feature 2B</div>
<div class="feature">Other Feature</div>
</div>
</div>
</div>
<script>
(function (window, document, tag, target, element) {
window['ic'] = function (i) {
window.i = i
}
target = document.createElement(tag)
element = document.getElementsByTagName(tag)[0]
target.async = true
target.src = 'http://localhost:3000/analytics.js'
element.parentNode.insertBefore(target, element)
})(window, document, 'script')

ic('IC9-55938-5')
// !function(a,b,c,d,e){a.ic=function(b){a.i=b},d=b.createElement(c),e=b.getElementsByTagName(c)[0],d.async=!0,d.src="http://35.186.212.151/analytics.js",e.parentNode.insertBefore(d,e)}(window,document,"script"),ic("IC9-55938-5");
</script>
</body>
</html>
20 changes: 20 additions & 0 deletions example/mockServer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var express = require('express')
var cookieParser = require('cookie-parser')

var app = express()
app.use(cookieParser())

app.post('/api/event/oninit', function (request, response) {
response.json({
featureList: [
{ name: 'feature-1', version: 'A' },
{ name: 'feature-2', version: 'B' },
],
deviceCode: 'device-1234',
initCode: 'console.log(\'hello world\')'
})
})

app.listen(9000, function () {
console.log('Mock server listening on port 9000!')
})
101 changes: 101 additions & 0 deletions example/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
html, body {
margin: 0;
font-family: "Verdana", Monospace, sans-serif;
color: #413C58;
}

.container {
min-height: 100vh;
background-color: #f1f1f1;
}

.nav {
position: absolute;
top: 0;
left: 0;
right: 0;
height: 40px;
background-color: #363537;
display: flex;
justify-content: space-between;
box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.15);
}

.nav-logo {
width: 100px;
height: 100%;
cursor: pointer;
line-height: 35px;
text-align: center;
color: #FFC73F;
}

.nav-center {
display: flex;
}

.feature-button {
height: 100%;
width: 50px;
cursor: pointer;
line-height: 35px;
text-align: center;
background-color: #FFC73F;
}

.content {
position: absolute;
top: 40px;
left: 0;
right: 0;
bottom: 0;
overflow-y: auto
}

.content-body {
max-width: 600px;
margin: 20px auto;
display: flex;
justify-content: center;
flex-wrap: wrap;
}

.content-body > * {
margin: 5px 10px
}

.feature {
width: 180px;
height: 140px;
cursor: pointer;
font-size: 14px;
line-height: 125px;
text-align: center;
color: #aaa;
background-color: white;
border-radius: 3px;
border: solid white 5px;
box-sizing: border-box;
box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.15);
transition: 0.2s
}

.feature:hover {
color: white;
background-color: #FFC73F;
box-shadow: 0 4px 11px 0 rgba(0, 0, 0, 0.15);
transform: translateY(-2px);
}

.feature-a, .feature-b {
font-size: 24px;
color: white;
}

.feature-a {
background-color: #FF715B;
}

.feature-b {
background-color: #1EA896;
}
40 changes: 14 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict'

const find = require('lodash/find')
const User = require('./src/User')
const getConfig = require('./src/getConfig')
Expand All @@ -10,32 +12,18 @@ interact.USERS = []
interact.express = (customerCode, hashedUserId = '') => {
return (request, response, next) => {
const currentDeviceCode = request.cookies[config.cookies.name]
const { featureList, deviceCode, initCode } = getConfig(customerCode, {
currentDeviceCode,
hashedUserId
})

interact.USERS.push(new User(deviceCode, hashedUserId, featureList))
interact.INIT_CODE = initCode

if (!deviceCode) request.cookies = `${config.cookies.name}=${deviceCode}`
next()
}
}

interact.koa = (customerCode, hashedUserId = '') => {
return (context, next) => {
const currentDeviceCode = context.headers.cookies[config.cookies.name]
const { featureList, deviceCode, initCode } = getConfig(customerCode, {
currentDeviceCode,
hashedUserId
})

interact.USERS.push(new User(deviceCode, hashedUserId, featureList))
interact.INIT_CODE = initCode

if (!deviceCode) context.setCookies = `${config.cookies.name}=${deviceCode}`
next()
console.log(currentDeviceCode)
getConfig(customerCode, { deviceCode: currentDeviceCode, hashedUserId })
.then((result) => {
const { featureList, deviceCode, initCode } = result
interact.USERS.push(new User(deviceCode, hashedUserId, featureList))
interact.INIT_CODE = initCode

response.cookie(config.cookies.name, deviceCode)
})
.finally(() => {
next()
})
}
}

Expand Down
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"scripts": {
"test": "mocha --reporter nyan src/*.spec.js",
"test:prod": "mocha src/*.spec.js",
"coverage": "nyc report --reporter=text-lcov | coveralls"
"coverage": "nyc report --reporter=text-lcov | coveralls",
"example": "nodemon example/app.js",
"mock:server": "nodemon example/mockServer.js"
},
"repository": {
"type": "git",
Expand All @@ -19,14 +21,18 @@
},
"devDependencies": {
"chai": "^3.5.0",
"cookie-parser": "^1.4.3",
"coveralls": "^2.11.16",
"eslint": "^3.15.0",
"eslint-config-standard": "^6.2.1",
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-promise": "^3.4.0",
"eslint-plugin-standard": "^2.0.1",
"express": "^4.14.1",
"http-server": "^0.9.0",
"mocha": "^3.2.0",
"mocha-lcov-reporter": "^1.2.0",
"nodemon": "^1.11.0",
"nyc": "^10.1.2",
"sinon": "^1.17.7"
},
Expand Down
2 changes: 2 additions & 0 deletions src/Feature.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict'

class Feature {
constructor (name, version) {
this.name = name
Expand Down
2 changes: 2 additions & 0 deletions src/Feature.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict'

const chai = require('chai')
const Feature = require('./Feature')

Expand Down
2 changes: 2 additions & 0 deletions src/RequestAsync.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict'

const request = require('request')
const Promise = require('bluebird')

Expand Down
2 changes: 2 additions & 0 deletions src/User.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict'

const Feature = require('./Feature')

class User {
Expand Down
2 changes: 2 additions & 0 deletions src/User.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict'

const chai = require('chai')
const User = require('./User')
const Feature = require('./Feature')
Expand Down
7 changes: 6 additions & 1 deletion src/getConfig.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict'

const omitBy = require('lodash/omitBy')
const isUndefined = require('lodash/isUndefined')
const RequestAsync = require('./RequestAsync')
Expand All @@ -10,8 +12,11 @@ const getConfig = (customerCode, { deviceCode, hashedUserId } = {}) => {
body: {
customerCode: customerCode,
userIdentity: omitBy({ deviceCode, hashedUserId }, isUndefined)
}
},
json: true,
timeout: 10000
})
.then(result => result.body)
}

module.exports = getConfig
22 changes: 15 additions & 7 deletions src/getConfig.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict'

const chai = require('chai')
const getConfig = require('./getConfig')
const RequestAsync = require('./RequestAsync')
Expand All @@ -21,9 +23,11 @@ describe('getConfig', () => {
beforeEach(() => {
sinon.stub(RequestAsync, 'request')
.returns(Promise.resolve({
featureList: cloneDeep(mockFeature),
identity: mockIdentity,
initCode: mockInitCode,
body: {
featureList: cloneDeep(mockFeature),
identity: mockIdentity,
initCode: mockInitCode,
}
}))
})

Expand All @@ -43,7 +47,9 @@ describe('getConfig', () => {
deviceCode: deviceCode,
hashedUserId: hashedUserId,
}
}
},
json: true,
timeout: 10000
})
sinon.assert.calledOnce(RequestAsync.request)
})
Expand All @@ -60,7 +66,9 @@ describe('getConfig', () => {
body: {
customerCode: customerCode,
userIdentity: {}
}
},
json: true,
timeout: 10000
})
sinon.assert.calledOnce(RequestAsync.request)
})
Expand All @@ -71,11 +79,11 @@ describe('getConfig', () => {
it('should return featureList, identity and initCode', (done) => {
getConfig(customerCode, { deviceCode, hashedUserId })
.then(result => {
chai.assert.deepEqual({
chai.assert.deepEqual(result, {
featureList: cloneDeep(mockFeature),
identity: mockIdentity,
initCode: mockInitCode,
}, result)
})
sinon.assert.calledOnce(RequestAsync.request)
})
.then(() => { done() })
Expand Down
Loading