Skip to content

Commit 57911e0

Browse files
committedAug 22, 2014
Merge pull request #8 from groupon/refactor
Refactor
2 parents 4a90320 + c549fba commit 57911e0

17 files changed

+71
-77
lines changed
 

‎public/js/gscreen.js

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

‎src/client/resources/alert.coffee

-5
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@ angular.module("GScreen").factory "Alert", ($resource) ->
1616
resource = $resource "/api/alerts/:id", null,
1717
update:
1818
method: "PUT"
19-
remove:
20-
method: "POST"
21-
url: "/api/alerts/:id/remove"
22-
params:
23-
id: "@id"
2419

2520
get: (id) ->
2621
resource.get id: id

‎src/client/resources/channel.coffee

-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ angular.module("GScreen").factory "Channel", ($resource) ->
1616
resource = $resource "/api/channels/:id", null,
1717
update:
1818
method: "PUT"
19-
remove:
20-
method: "DELETE"
2119

2220
get: (id) ->
2321
resource.get id: id

‎src/client/resources/chromecast.coffee

-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ angular.module("GScreen").factory "Chromecast", ($resource) ->
1616
resource = $resource "/api/receivers/:id", null,
1717
update:
1818
method: "PUT"
19-
remove:
20-
method: "DELETE"
2119

2220
get: (id) ->
2321
resource.get id: id

‎src/client/resources/takeover.coffee

-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ angular.module("GScreen").factory "Takeover", ($resource) ->
1616
resource = $resource "/api/takeover", null,
1717
update:
1818
method: "PUT"
19-
remove:
20-
method: "POST"
21-
url: "/api/takeover/remove"
2219

2320
get: (id) ->
2421
resource.get()

‎src/server/couch-db.coffee ‎src/server/couchdb/index.coffee

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ Redistribution and use in source and binary forms, with or without modification,
1313
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.###
1414

1515
cradle = require "cradle"
16-
views = require "./couch-db-views"
17-
validations = require "./couch-db-validations"
16+
views = require "./views"
17+
validations = require "./validations"
1818

1919
module.exports = class CouchDB
2020
constructor: (config) ->
File renamed without changes.
File renamed without changes.

‎src/server/db.coffee

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Redistribution and use in source and binary forms, with or without modification,
1212
1313
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.###
1414

15-
CouchDB = require "./couch-db"
15+
CouchDB = require "./couchdb"
1616
config = require "./config"
1717

1818
module.exports = new CouchDB(config.couch)

‎src/server/alert.coffee ‎src/server/models/alert.coffee

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Redistribution and use in source and binary forms, with or without modification,
1313
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.###
1414

1515
Model = require "./model"
16-
db = require "./db"
16+
db = require "../db"
1717

1818
module.exports = class Alert extends Model
1919
@type: "alert"
File renamed without changes.

‎src/server/model.coffee ‎src/server/models/model.coffee

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Redistribution and use in source and binary forms, with or without modification,
1212
1313
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.###
1414

15-
db = require "./db"
15+
db = require "../db"
1616
_ = require "underscore"
1717

1818
module.exports = class Model

‎src/server/receiver.coffee ‎src/server/models/receiver.coffee

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Alert = require "./alert"
1717
Channel = require "./channel"
1818
Model = require "./model"
1919
Takeover = require "./takeover"
20-
db = require "./db"
20+
db = require "../db"
2121

2222
module.exports = class Receiver extends Model
2323
@type: "receiver"

‎src/server/takeover.coffee ‎src/server/models/takeover.coffee

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
1414

1515
_ = require "underscore"
1616
Model = require "./model"
17-
db = require "./db"
17+
db = require "../db"
1818

1919
module.exports = class Takeover extends Model
2020
@type: "takeover"
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
###
23
Copyright (c) 2014, Groupon
34
All rights reserved.
@@ -13,32 +14,36 @@ Redistribution and use in source and binary forms, with or without modification,
1314
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.###
1415

1516
pathLib = require "path"
16-
Alert = require "./alert"
17-
Channel = require "./channel"
18-
Receiver = require "./receiver"
19-
Takeover = require "./takeover"
17+
express = require "express"
18+
Alert = require "../models/alert"
19+
Channel = require "../models/channel"
20+
Receiver = require "../models/receiver"
21+
Takeover = require "../models/takeover"
2022

2123
module.exports = (app, sockets) ->
24+
25+
router = express.Router()
26+
2227
addResources = (basePath, resourceClass) ->
23-
app.get "#{basePath}", (req, res) ->
28+
router.get "#{basePath}", (req, res) ->
2429
resourceClass.all (err, resources) ->
2530
if err then res.send err else res.send resources
2631

27-
app.get "#{basePath}/:id", (req, res) ->
32+
router.get "#{basePath}/:id", (req, res) ->
2833
resourceClass.findById req.param("id"), (err, resource) ->
2934
return res.status(404).send(null) if err && err.error == "not_found"
3035
return res.status(500).send(err) if err
3136
res.send resource
3237

33-
app.post "#{basePath}", (req, res) ->
38+
router.post "#{basePath}", (req, res) ->
3439
data = req.body
3540
resource = new resourceClass(data)
3641
resource.save (err) ->
3742
return res.status(500).send(err) if err
3843
sockets.emit "#{resourceClass.type}-created", resource
3944
res.send resource
4045

41-
app.put "#{basePath}/:id", (req, res) ->
46+
router.put "#{basePath}/:id", (req, res) ->
4247
resourceClass.findById req.param("id"), (err, resource) ->
4348
return res.status(404).send(null) if err && err.error == "not_found"
4449
return res.status(500).send(err) if err
@@ -47,7 +52,7 @@ module.exports = (app, sockets) ->
4752
sockets.emit "#{resourceClass.type}-updated", resource
4853
res.send resource
4954

50-
app.delete "#{basePath}/:id", (req, res) ->
55+
router.delete "#{basePath}/:id", (req, res) ->
5156
resourceClass.findById req.param("id"), (err, resource) ->
5257
return res.status(404).send(null) if err && err.error == "not_found"
5358
return res.status(500).send(err) if err
@@ -57,13 +62,13 @@ module.exports = (app, sockets) ->
5762
res.send status: "success"
5863

5964
addResource = (basePath, resourceClass, id) ->
60-
app.get "#{basePath}", (req, res) ->
65+
router.get "#{basePath}", (req, res) ->
6166
resourceClass.findById id, (err, resource) ->
6267
return res.status(404).send(null) if err && err.error == "not_found"
6368
return res.status(500).send(err) if err
6469
res.send resource
6570

66-
app.post "#{basePath}", (req, res) ->
71+
router.post "#{basePath}", (req, res) ->
6772
data = req.body
6873
data.id = id
6974
resource = new resourceClass(data)
@@ -72,7 +77,7 @@ module.exports = (app, sockets) ->
7277
sockets.emit "#{resourceClass.type}-created", resource
7378
res.send resource
7479

75-
app.put "#{basePath}", (req, res) ->
80+
router.put "#{basePath}", (req, res) ->
7681
resourceClass.findById id, (err, resource) ->
7782
return res.status(404).send(null) if err && err.error == "not_found"
7883
return res.status(500).send(err) if err
@@ -81,7 +86,7 @@ module.exports = (app, sockets) ->
8186
sockets.emit "#{resourceClass.type}-updated", resource
8287
res.send resource
8388

84-
app.post "#{basePath}/remove", (req, res) ->
89+
router.delete "#{basePath}", (req, res) ->
8590
resourceClass.findById id, (err, resource) ->
8691
return res.status(404).send(null) if err && err.error == "not_found"
8792
return res.status(500).send(err) if err
@@ -90,23 +95,9 @@ module.exports = (app, sockets) ->
9095
sockets.emit "#{resourceClass.type}-deleted", resource
9196
res.send status: "success"
9297

93-
addResources "/api/alerts", Alert
94-
addResources "/api/channels", Channel
95-
addResources "/api/receivers", Receiver
96-
addResource "/api/takeover", Takeover, "takeover-singleton"
97-
98-
app.get "/chromecasts/new", (req, res) ->
99-
res.sendfile pathLib.resolve("#{__dirname}/../../public/index.html")
100-
101-
app.get "/chromecasts/:id", (req, res) ->
102-
res.sendfile pathLib.resolve("#{__dirname}/../../public/setup-chromecast.html")
103-
104-
app.get "/channels/new", (req, res) ->
105-
res.sendfile pathLib.resolve("#{__dirname}/../../public/index.html")
106-
107-
app.get "/channels/:id", (req, res) ->
108-
res.sendfile pathLib.resolve("#{__dirname}/../../public/channel.html")
98+
addResources "/alerts", Alert
99+
addResources "/channels", Channel
100+
addResources "/receivers", Receiver
101+
addResource "/takeover", Takeover, "takeover-singleton"
109102

110-
# Wildcard all GET routes, send to the Angular app
111-
app.get "*", (req, res) ->
112-
res.sendfile pathLib.resolve("#{__dirname}/../../public/index.html")
103+
app.use('/api', router)

‎src/server/routes/routes.coffee

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
###
2+
Copyright (c) 2014, Groupon
3+
All rights reserved.
4+
5+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6+
7+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8+
9+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
10+
11+
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
12+
13+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.###
14+
15+
pathLib = require "path"
16+
17+
module.exports = (app) ->
18+
app.get "/chromecasts/new", (req, res) ->
19+
res.sendfile pathLib.resolve("#{__dirname}/../../../public/index.html")
20+
21+
app.get "/chromecasts/:id", (req, res) ->
22+
res.sendfile pathLib.resolve("#{__dirname}/../../../public/setup-chromecast.html")
23+
24+
app.get "/channels/new", (req, res) ->
25+
res.sendfile pathLib.resolve("#{__dirname}/../../../public/index.html")
26+
27+
app.get "/channels/:id", (req, res) ->
28+
res.sendfile pathLib.resolve("#{__dirname}/../../../public/channel.html")
29+
30+
# Wildcard all GET routes, send to the Angular app
31+
app.get "*", (req, res) ->
32+
res.sendfile pathLib.resolve("#{__dirname}/../../../public/index.html")

‎src/server/server.coffee

+4-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ morgan = require "morgan"
1818
bodyParser = require "body-parser"
1919
methodOverride = require "method-override"
2020
config = require "./config"
21-
routes = require "./routes"
21+
api = require("./routes/api")
22+
routes = require "./routes/routes"
2223
socketIO = require "socket.io"
2324

2425
app = express()
@@ -32,7 +33,8 @@ app.use bodyParser.json()
3233
app.use methodOverride()
3334
app.use express.static("#{__dirname}/../../public")
3435

35-
routes(app, sockets)
36+
api(app, sockets)
37+
routes(app)
3638

3739
port = process.env.PORT || config.server?.port || 4994
3840
server.listen port

0 commit comments

Comments
 (0)
Please sign in to comment.