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
405bb12b
Commit
405bb12b
authored
Jul 29, 2012
by
Rocky Duan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
api for tag autocompletion
parent
cc9c6081
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
49 additions
and
25 deletions
+49
-25
app.rb
+14
-18
spec/api/comment_thread_spec.rb
+27
-0
spec/api/search_spec.rb
+8
-7
No files found.
app.rb
View file @
405bb12b
...
@@ -21,6 +21,17 @@ Mongoid.logger.level = Logger::INFO
...
@@ -21,6 +21,17 @@ Mongoid.logger.level = Logger::INFO
Dir
[
File
.
dirname
(
__FILE__
)
+
'/models/*.rb'
].
each
{
|
file
|
require
file
}
Dir
[
File
.
dirname
(
__FILE__
)
+
'/models/*.rb'
].
each
{
|
file
|
require
file
}
Dir
[
File
.
dirname
(
__FILE__
)
+
'/lib/**/*.rb'
].
each
{
|
file
|
require
file
}
Dir
[
File
.
dirname
(
__FILE__
)
+
'/lib/**/*.rb'
].
each
{
|
file
|
require
file
}
get
'/api/v1/search/threads'
do
results
=
CommentThread
.
solr_search
do
fulltext
(
params
[
"text"
])
if
params
[
"text"
]
with
(
:commentable_id
,
params
[
"commentable_id"
])
if
params
[
"commentable_id"
]
with
(
:tags
).
all_of
(
params
[
"tags"
].
split
/,/
)
if
params
[
"tags"
]
end
.
results
puts
params
[
"tags"
].
split
/,/
puts
results
results
.
map
(
&
:to_hash
).
to_json
end
delete
'/api/v1/:commentable_id/threads'
do
|
commentable_id
|
delete
'/api/v1/:commentable_id/threads'
do
|
commentable_id
|
commentable
.
comment_threads
.
destroy_all
commentable
.
comment_threads
.
destroy_all
{}.
to_json
{}.
to_json
...
@@ -45,7 +56,9 @@ get '/api/v1/threads/tags' do
...
@@ -45,7 +56,9 @@ get '/api/v1/threads/tags' do
CommentThread
.
tags
.
to_json
CommentThread
.
tags
.
to_json
end
end
get
'/api/v1/threads/tags/autocomplete'
do
CommentThread
.
tags_autocomplete
(
params
[
"value"
],
max:
5
,
sort_by_count:
true
).
map
(
&
:first
).
to_json
end
get
'/api/v1/threads/:thread_id'
do
|
thread_id
|
get
'/api/v1/threads/:thread_id'
do
|
thread_id
|
thread
.
to_hash
(
recursive:
params
[
"recursive"
]).
to_json
thread
.
to_hash
(
recursive:
params
[
"recursive"
]).
to_json
...
@@ -129,23 +142,6 @@ delete '/api/v1/users/:user_id/subscriptions' do |user_id|
...
@@ -129,23 +142,6 @@ delete '/api/v1/users/:user_id/subscriptions' do |user_id|
user
.
unsubscribe
(
source
).
to_hash
.
to_json
user
.
unsubscribe
(
source
).
to_hash
.
to_json
end
end
get
'/api/v1/search/threads'
do
if
params
[
"text"
]
CommentThread
.
solr_search
do
fulltext
(
params
[
"text"
])
if
params
[
"commentable_id"
]
with
(
:commentable_id
,
params
[
"commentable_id"
])
end
end
.
results
.
map
(
&
:to_hash
).
to_json
else
{}.
to_json
end
end
post
'/api/v1/search/threads/tags'
do
CommentThread
.
tagged_with_all
(
params
[
"tags"
]).
map
(
&
:to_hash
).
to_json
end
if
environment
.
to_s
==
"development"
if
environment
.
to_s
==
"development"
get
'/api/v1/clean'
do
get
'/api/v1/clean'
do
Comment
.
delete_all
Comment
.
delete_all
...
...
spec/api/comment_thread_spec.rb
View file @
405bb12b
...
@@ -133,4 +133,31 @@ describe "app" do
...
@@ -133,4 +133,31 @@ describe "app" do
tags
.
length
.
should
==
6
tags
.
length
.
should
==
6
end
end
end
end
describe
"GET /api/v1/threads/tags/autocomplete"
do
def
create_comment_thread
(
tags
)
c
=
CommentThread
.
new
(
title:
"Interesting question"
,
body:
"cool"
)
c
.
course_id
=
"1"
c
.
author
=
User
.
first
c
.
tags
=
tags
c
.
commentable_id
=
"1"
c
.
save!
c
end
it
"returns autocomplete results"
do
CommentThread
.
delete_all
create_comment_thread
"c++, clojure, common-lisp, c#, c, coffeescript"
create_comment_thread
"c++, clojure, common-lisp, c#, c"
create_comment_thread
"c++, clojure, common-lisp, c#"
create_comment_thread
"c++, clojure, common-lisp"
create_comment_thread
"c++, clojure"
create_comment_thread
"c++"
get
"/api/v1/threads/tags/autocomplete"
,
value:
"c"
last_response
.
should
be_ok
results
=
parse
last_response
.
body
results
.
length
.
should
==
5
%w[c++ clojure common-lisp c# c]
.
each_with_index
do
|
tag
,
index
|
results
[
index
].
should
==
tag
end
end
end
end
end
spec/api/search_spec.rb
View file @
405bb12b
...
@@ -3,9 +3,8 @@ require 'spec_helper'
...
@@ -3,9 +3,8 @@ require 'spec_helper'
describe
"app"
do
describe
"app"
do
describe
"search"
do
describe
"search"
do
before
(
:each
)
{
init_without_subscriptions
}
before
(
:each
)
{
init_without_subscriptions
}
describe
"GET /api/v1/search/threads
/tags
"
do
describe
"GET /api/v1/search/threads"
do
it
"returns all threads tagged with all tags"
do
it
"returns all threads tagged with all tags"
do
require
'uri'
thread1
=
CommentThread
.
all
.
to_a
.
first
thread1
=
CommentThread
.
all
.
to_a
.
first
thread2
=
CommentThread
.
all
.
to_a
.
last
thread2
=
CommentThread
.
all
.
to_a
.
last
ai
=
"artificial intelligence"
ai
=
"artificial intelligence"
...
@@ -18,33 +17,35 @@ describe "app" do
...
@@ -18,33 +17,35 @@ describe "app" do
thread2
.
tags
=
[
ai
,
ml
,
random2
].
join
","
thread2
.
tags
=
[
ai
,
ml
,
random2
].
join
","
thread2
.
save
thread2
.
save
post
"/api/v1/search/threads/tags"
,
tags:
[
ai
,
ml
]
Sunspot
.
commit
get
"/api/v1/search/threads"
,
tags:
[
ai
,
ml
].
join
(
","
)
last_response
.
should
be_ok
last_response
.
should
be_ok
threads
=
parse
last_response
.
body
threads
=
parse
last_response
.
body
threads
.
length
.
should
==
2
threads
.
length
.
should
==
2
threads
.
select
{
|
t
|
t
[
"id"
]
==
thread1
.
id
.
to_s
}.
first
.
should_not
be_nil
threads
.
select
{
|
t
|
t
[
"id"
]
==
thread1
.
id
.
to_s
}.
first
.
should_not
be_nil
threads
.
select
{
|
t
|
t
[
"id"
]
==
thread2
.
id
.
to_s
}.
first
.
should_not
be_nil
threads
.
select
{
|
t
|
t
[
"id"
]
==
thread2
.
id
.
to_s
}.
first
.
should_not
be_nil
post
"/api/v1/search/threads/tags"
,
tags:
[
ai
]
get
"/api/v1/search/threads"
,
tags:
[
ai
].
join
(
","
)
last_response
.
should
be_ok
last_response
.
should
be_ok
threads
=
parse
last_response
.
body
threads
=
parse
last_response
.
body
threads
.
length
.
should
==
2
threads
.
length
.
should
==
2
threads
.
select
{
|
t
|
t
[
"id"
]
==
thread1
.
id
.
to_s
}.
first
.
should_not
be_nil
threads
.
select
{
|
t
|
t
[
"id"
]
==
thread1
.
id
.
to_s
}.
first
.
should_not
be_nil
threads
.
select
{
|
t
|
t
[
"id"
]
==
thread2
.
id
.
to_s
}.
first
.
should_not
be_nil
threads
.
select
{
|
t
|
t
[
"id"
]
==
thread2
.
id
.
to_s
}.
first
.
should_not
be_nil
post
"/api/v1/search/threads/tags"
,
tags:
[
ai
,
random1
]
get
"/api/v1/search/threads"
,
tags:
[
ai
,
random1
].
join
(
","
)
last_response
.
should
be_ok
last_response
.
should
be_ok
threads
=
parse
last_response
.
body
threads
=
parse
last_response
.
body
threads
.
length
.
should
==
1
threads
.
length
.
should
==
1
threads
.
select
{
|
t
|
t
[
"id"
]
==
thread1
.
id
.
to_s
}.
first
.
should_not
be_nil
threads
.
select
{
|
t
|
t
[
"id"
]
==
thread1
.
id
.
to_s
}.
first
.
should_not
be_nil
post
"/api/v1/search/threads/tags"
,
tags:
[
random1
]
get
"/api/v1/search/threads"
,
tags:
[
random1
].
join
(
","
)
last_response
.
should
be_ok
last_response
.
should
be_ok
threads
=
parse
last_response
.
body
threads
=
parse
last_response
.
body
threads
.
length
.
should
==
1
threads
.
length
.
should
==
1
threads
.
select
{
|
t
|
t
[
"id"
]
==
thread1
.
id
.
to_s
}.
first
.
should_not
be_nil
threads
.
select
{
|
t
|
t
[
"id"
]
==
thread1
.
id
.
to_s
}.
first
.
should_not
be_nil
post
"/api/v1/search/threads/tags"
,
tags:
[
random1
,
random2
]
get
"/api/v1/search/threads"
,
tags:
[
random1
,
random2
].
join
(
","
)
last_response
.
should
be_ok
last_response
.
should
be_ok
threads
=
parse
last_response
.
body
threads
=
parse
last_response
.
body
threads
.
length
.
should
==
0
threads
.
length
.
should
==
0
...
...
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