Skip to content

Instantly share code, notes, and snippets.

@n0nick
Created March 24, 2014 11:54
Show Gist options
  • Save n0nick/9738811 to your computer and use it in GitHub Desktop.
Save n0nick/9738811 to your computer and use it in GitHub Desktop.
diff --git a/app/assets/javascripts/teams/app.js.coffee b/app/assets/javascripts/teams/app.js.coffee
index e3a57ef..23cec17 100644
--- a/app/assets/javascripts/teams/app.js.coffee
+++ b/app/assets/javascripts/teams/app.js.coffee
@@ -14,11 +14,15 @@ app.config [
controller: 'TeamListCtrl'
templateUrl: 'partials/list.html'
depth: 1
+ resolve:
+ clientInfo: 'ClientInfoFetcher'
$routeProvider.when '/teams/show/:teamId',
controller: 'TeamCtrl'
templateUrl: 'partials/show.html'
depth: 2
+ resolve:
+ clientInfo: 'ClientInfoFetcher'
]
app.run ($rootScope, $window) ->
@@ -138,13 +142,14 @@ app.factory 'Team', [
app.factory 'APISessionInterceptor', [
'$q'
- ($q) ->
+ 'ClientInfo'
+ ($q, ClientInfo) ->
request: (config) ->
if config.url.match(/^\/(?:\w*-?)WAS\//)
config.params ||= {}
angular.extend config.params,
- cookie: 'chocolate_chip'
- session_id: 12345
+ cookie: ClientInfo.get('WAS.cookie')
+ session_id: ClientInfo.get('WAS.session_id')
return config
]
@@ -153,3 +158,24 @@ app.config [
($httpProvider) ->
$httpProvider.interceptors.push('APISessionInterceptor');
]
+
+app.service 'ClientInfo', ->
+ set: (key, value) ->
+ @data ||= {}
+ @data[key] = value
+
+ get: (key) ->
+ @data ||= {}
+ @data[key]
+
+app.service 'ClientInfoFetcher', [
+ '$location'
+ 'ClientInfo'
+ ($location, ClientInfo) ->
+ params = $location.search()
+ if params.cookie? and params.session_id?
+ ClientInfo.set('WAS.cookie', params.cookie)
+ ClientInfo.set('WAS.session_id', params.session_id)
+ if params.env?
+ ClientInfo.set('env', params.env)
+]
diff --git a/spec/teams/units/services-spec.js.coffee b/spec/teams/units/services-spec.js.coffee
index 757c05f..c92ee1e 100644
--- a/spec/teams/units/services-spec.js.coffee
+++ b/spec/teams/units/services-spec.js.coffee
@@ -1,14 +1,20 @@
describe 'Services', ->
+ mockClientInfo = ->
+ inject (ClientInfo) ->
+ ClientInfo.set('WAS.cookie', 'butter')
+ ClientInfo.set('WAS.session_id', '123')
+
describe 'Team', ->
beforeEach ->
module 'TeamsApp'
+ mockClientInfo()
describe '#find', ->
it 'fires a request to the API', ->
inject (Team, $httpBackend) ->
- $httpBackend.expectGET('/test-WAS/app/groups/info/group?cookie=chocolate_chip&group_id=123&session_id=12345')
+ $httpBackend.expectGET('/test-WAS/app/groups/info/group?cookie=butter&group_id=123&session_id=123')
.respond
name: "My Team"
description: "My Desc"
@@ -32,7 +38,7 @@ describe 'Services', ->
beforeEach ->
inject ($httpBackend) ->
- $httpBackend.expectGET('/test-WAS/app/groups/action/join?cookie=chocolate_chip&group_id=123&session_id=12345')
+ $httpBackend.expectGET('/test-WAS/app/groups/action/join?cookie=butter&group_id=123&session_id=123')
.respond({type: "join", groupId: 123})
it 'fires a request to the API', ->
@@ -58,7 +64,7 @@ describe 'Services', ->
describe '#leave', ->
beforeEach ->
inject ($httpBackend) ->
- $httpBackend.expectGET('/test-WAS/app/groups/action/leave?cookie=chocolate_chip&group_id=123&session_id=12345')
+ $httpBackend.expectGET('/test-WAS/app/groups/action/leave?cookie=butter&group_id=123&session_id=123')
.respond({type: "leave", groupId: 123})
it 'fires a request to the API', ->
@@ -88,7 +94,7 @@ describe 'Services', ->
group1 = {name: 'Group 1'}
group2 = {name: 'Group 2'}
group3 = {name: 'Group 3'}
- $httpBackend.expectGET('/test-WAS/app/groups/list/nearby?cookie=chocolate_chip&session_id=12345')
+ $httpBackend.expectGET('/test-WAS/app/groups/list/nearby?cookie=butter&session_id=123')
.respond({groups: [group1, group2, group3]})
it 'fires a request to the API', ->
@@ -104,7 +110,7 @@ describe 'Services', ->
inject ($httpBackend) ->
group1 = {name: 'Group 1'}
group2 = {name: 'Group 2'}
- $httpBackend.expectGET('/test-WAS/app/groups/list/mine?cookie=chocolate_chip&session_id=12345')
+ $httpBackend.expectGET('/test-WAS/app/groups/list/mine?cookie=butter&session_id=123')
.respond({groups: [group1, group2]})
it 'fires a request to the API', ->
@@ -153,7 +159,7 @@ describe 'Services', ->
beforeEach ->
inject ($httpBackend) ->
group4 = {name: 'Group 4'}
- $httpBackend.expectGET('/test-WAS/app/groups/list/search?cookie=chocolate_chip&search=abc&session_id=12345')
+ $httpBackend.expectGET('/test-WAS/app/groups/list/search?cookie=butter&search=abc&session_id=123')
.respond({groups: [group4]})
it 'fires a request to the API', ->
@@ -169,7 +175,7 @@ describe 'Services', ->
inject ($httpBackend) ->
group4 = {name: 'Group 4', id: 12}
group5 = {name: 'Group 5', id: 13}
- $httpBackend.expectGET('/test-WAS/app/groups/list/specific?cookie=chocolate_chip&ids=12,13&session_id=12345')
+ $httpBackend.expectGET('/test-WAS/app/groups/list/specific?cookie=butter&ids=12,13&session_id=123')
.respond({groups: [group4, group5]})
it 'fires a request to the API', ->
@@ -183,10 +189,11 @@ describe 'Services', ->
describe 'APISessionInterceptor', ->
beforeEach ->
module 'TeamsApp'
+ mockClientInfo()
it 'appends session data to every API request', ->
inject ($http, $httpBackend) ->
- $httpBackend.expectGET('/test-WAS/something?cookie=chocolate_chip&session_id=12345')
+ $httpBackend.expectGET('/test-WAS/something?cookie=butter&session_id=123')
.respond({})
$http.get '/test-WAS/something'
@@ -199,3 +206,29 @@ describe 'Services', ->
$http.get '/something/else'
$httpBackend.flush()
+
+ describe 'ClientInfo', ->
+ beforeEach ->
+ module 'TeamsApp'
+
+ it 'stores values persistently', ->
+ inject (ClientInfo) ->
+ ClientInfo.set('some_key', 'some_value')
+ inject (ClientInfo) ->
+ expect(ClientInfo.get('some_key')).toEqual('some_value')
+
+ describe 'ClientInfoFetcher', ->
+ beforeEach ->
+ module 'TeamsApp'
+
+ it 'stores session info from GET params', ->
+ inject (ClientInfo, $location) ->
+ spyOn($location, 'search')
+ .andReturn
+ session_id: '666'
+ cookie: 'alfajores'
+ env: 'micronesia'
+ inject (ClientInfoFetcher) ->
+ expect(ClientInfo.get('WAS.session_id')).toEqual('666')
+ expect(ClientInfo.get('WAS.cookie')).toEqual('alfajores')
+ expect(ClientInfo.get('env')).toEqual('micronesia')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment