Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
cs_comments_service
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
cs_comments_service
Commits
35221024
Commit
35221024
authored
Jul 26, 2012
by
Rocky Duan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactored out helper; more specs
parent
b45a2b8f
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
104 additions
and
52 deletions
+104
-52
Rakefile
+2
-2
app.rb
+8
-44
lib/exceptions.rb
+3
-0
lib/helpers.rb
+43
-0
lib/utils.rb
+0
-0
spec/app_spec.rb
+48
-6
No files found.
Rakefile
View file @
35221024
...
...
@@ -7,8 +7,8 @@ Bundler.require
desc
"Load the environment"
task
:environment
do
env
=
ENV
[
"SINATRA_ENV"
]
||
"development"
Sinatra
::
Base
.
environment
=
env
env
ironment
=
ENV
[
"SINATRA_ENV"
]
||
"development"
Sinatra
::
Base
.
environment
=
env
ironment
Mongoid
.
load!
(
"config/mongoid.yml"
)
Mongoid
.
logger
.
level
=
Logger
::
INFO
module
CommentService
...
...
app.rb
View file @
35221024
...
...
@@ -6,7 +6,8 @@ Bundler.require
env_index
=
ARGV
.
index
(
"-e"
)
env_arg
=
ARGV
[
env_index
+
1
]
if
env_index
env
=
env_arg
||
ENV
[
"SINATRA_ENV"
]
||
"development"
environment
=
env_arg
||
ENV
[
"SINATRA_ENV"
]
||
"development"
RACK_ENV
=
environment
module
CommentService
class
<<
self
;
attr_accessor
:config
;
end
...
...
@@ -20,47 +21,6 @@ Mongoid.logger.level = Logger::INFO
Dir
[
File
.
dirname
(
__FILE__
)
+
'/models/*.rb'
].
each
{
|
file
|
require
file
}
Dir
[
File
.
dirname
(
__FILE__
)
+
'/lib/**/*.rb'
].
each
{
|
file
|
require
file
}
helpers
do
def
commentable
@commentable
||=
Commentable
.
find
(
params
[
:commentable_id
])
end
def
user
# TODO handle 404 if integrated user service
@user
||=
(
User
.
find_or_create_by
(
external_id:
params
[
:user_id
])
if
params
[
:user_id
])
end
def
thread
@thread
||=
CommentThread
.
find
(
params
[
:thread_id
])
end
def
comment
@comment
||=
Comment
.
find
(
params
[
:comment_id
])
end
def
source
@source
||=
case
params
[
"source_type"
]
when
"user"
User
.
find_or_create_by
(
external_id:
params
[
"source_id"
])
when
"thread"
CommentThread
.
find
(
params
[
"source_id"
])
when
"other"
Commentable
.
find
(
params
[
"source_id"
])
end
end
def
vote_for
(
obj
)
user
.
vote
(
obj
,
params
[
"value"
].
to_sym
)
obj
.
reload
.
to_hash
.
to_json
end
def
undo_vote_for
(
obj
)
user
.
unvote
(
obj
)
obj
.
reload
.
to_hash
.
to_json
end
end
delete
'/api/v1/:commentable_id/threads'
do
|
commentable_id
|
commentable
.
comment_threads
.
destroy_all
{}.
to_json
...
...
@@ -176,7 +136,7 @@ get '/api/v1/search' do
end
end
if
env
.
to_s
==
"development"
if
env
ironment
.
to_s
==
"development"
get
'/api/v1/clean'
do
Comment
.
delete_all
CommentThread
.
delete_all
...
...
@@ -188,5 +148,9 @@ if env.to_s == "development"
end
error
BSON
::
InvalidObjectId
do
error
404
error
400
,
"requested object not found"
end
error
ValueError
do
error
400
,
env
[
'sinatra.error'
].
message
end
lib/exceptions.rb
0 → 100644
View file @
35221024
class
ValueError
<
Exception
end
lib/helpers.rb
0 → 100644
View file @
35221024
helpers
do
def
commentable
@commentable
||=
Commentable
.
find
(
params
[
:commentable_id
])
end
def
user
# TODO handle 404 if integrated user service
@user
||=
(
User
.
find_or_create_by
(
external_id:
params
[
:user_id
])
if
params
[
:user_id
])
end
def
thread
@thread
||=
CommentThread
.
find
(
params
[
:thread_id
])
end
def
comment
@comment
||=
Comment
.
find
(
params
[
:comment_id
])
end
def
source
@source
||=
case
params
[
"source_type"
]
when
"user"
User
.
find_or_create_by
(
external_id:
params
[
"source_id"
])
when
"thread"
CommentThread
.
find
(
params
[
"source_id"
])
when
"other"
Commentable
.
find
(
params
[
"source_id"
])
end
end
def
vote_for
(
obj
)
raise
ValueError
,
"must provide user id"
unless
user
raise
ValueError
,
"must provide value"
unless
params
[
"value"
]
raise
ValueError
,
"must provide valid value"
unless
%w[up down]
.
include?
params
[
"value"
]
user
.
vote
(
obj
,
params
[
"value"
].
to_sym
)
obj
.
reload
.
to_hash
.
to_json
end
def
undo_vote_for
(
obj
)
raise
ValueError
,
"must provide user id"
unless
user
user
.
unvote
(
obj
)
obj
.
reload
.
to_hash
.
to_json
end
end
lib/util.rb
→
lib/util
s
.rb
View file @
35221024
File moved
spec/app_spec.rb
View file @
35221024
...
...
@@ -218,9 +218,9 @@ describe "app" do
response_thread
[
"children"
].
length
.
should
==
thread
.
root_comments
.
length
response_thread
[
"children"
].
index
{
|
c
|
c
[
"body"
]
==
thread
.
root_comments
.
first
.
body
}.
should_not
be_nil
end
it
"returns
error message
when the thread does not exist"
do
it
"returns
400
when the thread does not exist"
do
get
"/api/v1/threads/does_not_exist"
last_response
.
status
.
should
==
40
4
last_response
.
status
.
should
==
40
0
end
end
describe
"PUT /api/v1/threads/:thread_id"
do
...
...
@@ -232,9 +232,9 @@ describe "app" do
changed_thread
.
body
.
should
==
"new body"
changed_thread
.
title
.
should
==
"new title"
end
it
"returns
error message
when the thread does not exist"
do
it
"returns
400
when the thread does not exist"
do
put
"/api/v1/threads/does_not_exist"
,
body:
"new body"
,
title:
"new title"
last_response
.
status
.
should
==
40
4
last_response
.
status
.
should
==
40
0
end
end
describe
"POST /api/v1/threads/:thread_id/comments"
do
...
...
@@ -258,9 +258,9 @@ describe "app" do
comment
=
changed_thread
[
"children"
].
select
{
|
c
|
c
[
"body"
]
==
"new comment"
}.
first
comment
.
should_not
be_nil
end
it
"returns
error message
when the thread does not exist"
do
it
"returns
400
when the thread does not exist"
do
post
"/api/v1/threads/does_not_exist/comments"
,
body:
"new comment"
,
course_id:
"1"
,
user_id:
User
.
first
.
id
last_response
.
status
.
should
==
40
4
last_response
.
status
.
should
==
40
0
end
end
describe
"DELETE /api/v1/threads/:thread_id"
do
...
...
@@ -270,6 +270,10 @@ describe "app" do
last_response
.
should
be_ok
CommentThread
.
where
(
title:
thread
[
"title"
]).
first
.
should
be_nil
end
it
"returns 400 when the thread does not exist"
do
delete
"/api/v1/threads/does_not_exist"
last_response
.
status
.
should
==
400
end
end
end
...
...
@@ -299,6 +303,10 @@ describe "app" do
retrieved
[
"children"
].
length
.
should
==
comment
.
children
.
length
retrieved
[
"children"
].
select
{
|
c
|
c
[
"body"
]
==
comment
.
children
.
first
.
body
}.
first
.
should_not
be_nil
end
it
"returns 400 when the comment does not exist"
do
get
"/api/v1/comments/does_not_exist"
last_response
.
status
.
should
==
400
end
end
describe
"PUT /api/v1/comments/:comment_id"
do
it
"update information of the comment"
do
...
...
@@ -309,6 +317,10 @@ describe "app" do
new_comment
.
body
.
should
==
"new body"
new_comment
.
endorsed
.
should
==
true
end
it
"returns 400 when the comment does not exist"
do
put
"/api/v1/comments/does_not_exist"
,
body:
"new body"
,
endorsed:
true
last_response
.
status
.
should
==
400
end
end
describe
"POST /api/v1/comments/:comment_id"
do
it
"create a sub comment to the comment"
do
...
...
@@ -322,6 +334,10 @@ describe "app" do
subcomment
.
should_not
be_nil
subcomment
[
"user_id"
].
should
==
user
.
id
end
it
"returns 400 when the comment does not exist"
do
post
"/api/v1/comments/does_not_exist"
,
body:
"new comment"
,
course_id:
"1"
,
user_id:
User
.
first
.
id
last_response
.
status
.
should
==
400
end
end
describe
"DELETE /api/v1/comments/:comment_id"
do
it
"delete the comment and its sub comments"
do
...
...
@@ -332,6 +348,10 @@ describe "app" do
Comment
.
count
.
should
==
prev_count
-
cnt_comments
Comment
.
all
.
select
{
|
c
|
c
.
id
==
comment
.
id
}.
first
.
should
be_nil
end
it
"returns 400 when the comment does not exist"
do
delete
"/api/v1/comments/does_not_exist"
last_response
.
status
.
should
==
400
end
end
end
describe
"votes"
do
...
...
@@ -347,6 +367,20 @@ describe "app" do
comment
.
up_votes_count
.
should
==
prev_up_votes
-
1
comment
.
down_votes_count
.
should
==
prev_down_votes
+
1
end
it
"returns 400 when the comment does not exist"
do
put
"/api/v1/comments/does_not_exist/votes"
,
user_id:
User
.
first
.
id
,
value:
"down"
last_response
.
status
.
should
==
400
end
it
"returns 400 when user_id is not provided"
do
put
"/api/v1/comments/
#{
Comment
.
first
.
id
}
/votes"
,
value:
"down"
last_response
.
status
.
should
==
400
end
it
"returns 400 when value is not provided or invalid"
do
put
"/api/v1/comments/
#{
Comment
.
first
.
id
}
/votes"
,
user_id:
User
.
first
.
id
last_response
.
status
.
should
==
400
put
"/api/v1/comments/
#{
Comment
.
first
.
id
}
/votes"
,
user_id:
User
.
first
.
id
,
value:
"superdown"
last_response
.
status
.
should
==
400
end
end
describe
"DELETE /api/v1/comments/:comment_id/votes"
do
it
"unvote on the comment"
do
...
...
@@ -359,6 +393,14 @@ describe "app" do
comment
.
up_votes_count
.
should
==
prev_up_votes
-
1
comment
.
down_votes_count
.
should
==
prev_down_votes
end
it
"returns 400 when the comment does not exist"
do
delete
"/api/v1/comments/does_not_exist/votes"
,
user_id:
User
.
first
.
id
last_response
.
status
.
should
==
400
end
it
"returns 400 when user_id is not provided"
do
delete
"/api/v1/comments/
#{
Comment
.
first
.
id
}
/votes"
last_response
.
status
.
should
==
400
end
end
describe
"PUT /api/v1/threads/:thread_id/votes"
do
it
"create or update the vote on the thread"
do
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment