API: add new route to search for tags
This commit is contained in:
parent
984b739eb4
commit
00df0b7bda
3 changed files with 60 additions and 1 deletions
|
|
@ -23,6 +23,12 @@ module Api
|
|||
render_paged_api_response posts_page
|
||||
end
|
||||
|
||||
def tag_index
|
||||
tags_page = index_pager(tags_query).response
|
||||
tags_page[:data] = tags_page[:data].pluck(:name)
|
||||
render_paged_api_response tags_page
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def time_pager(query, query_time_field, data_time_field)
|
||||
|
|
@ -53,6 +59,10 @@ module Api
|
|||
Stream::Tag.new(nil, params.require(:tag)).posts
|
||||
end
|
||||
end
|
||||
|
||||
def tags_query
|
||||
ActsAsTaggableOn::Tag.autocomplete(params.require(:query))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -259,6 +259,7 @@ Rails.application.routes.draw do
|
|||
resources :tag_followings, only: %i[index create destroy]
|
||||
get "search/users" => "search#user_index", :as => "user_index"
|
||||
get "search/posts" => "search#post_index", :as => "post_index"
|
||||
get "search/tags" => "search#tag_index", :as => "tag_index"
|
||||
get "streams/activity" => "streams#activity", :as => "activity_stream"
|
||||
get "streams/main" => "streams#multi", :as => "stream"
|
||||
get "streams/tags" => "streams#followed_tags", :as => "followed_tags_stream"
|
||||
|
|
|
|||
|
|
@ -213,13 +213,61 @@ describe Api::V1::SearchController do
|
|||
|
||||
it "fails with bad credentials" do
|
||||
get(
|
||||
"/api/v1/search/users",
|
||||
"/api/v1/search/posts",
|
||||
params: {tag: "tag1", access_token: invalid_token}
|
||||
)
|
||||
expect(response.status).to eq(401)
|
||||
end
|
||||
end
|
||||
|
||||
describe "tag_index" do
|
||||
before do
|
||||
FactoryGirl.create(:tag, name: "apipartyone")
|
||||
FactoryGirl.create(:tag, name: "apipartytwo")
|
||||
FactoryGirl.create(:tag, name: "apipartythree")
|
||||
end
|
||||
|
||||
it "succeeds" do
|
||||
get(
|
||||
"/api/v1/search/tags",
|
||||
params: {query: "apiparty", access_token: access_token_public_only_read_only}
|
||||
)
|
||||
expect(response.status).to eq(200)
|
||||
tags = response_body_data(response)
|
||||
expect(tags.size).to eq(3)
|
||||
|
||||
expect(tags.to_json).to match_json_schema(:api_v1_schema, fragment: "#/definitions/tags")
|
||||
end
|
||||
|
||||
it "does a prefix search" do
|
||||
get(
|
||||
"/api/v1/search/tags",
|
||||
params: {query: "apipartyt", access_token: access_token_public_only_read_only}
|
||||
)
|
||||
expect(response.status).to eq(200)
|
||||
tags = response_body_data(response)
|
||||
expect(tags.size).to eq(2)
|
||||
|
||||
expect(tags.to_json).to match_json_schema(:api_v1_schema, fragment: "#/definitions/tags")
|
||||
end
|
||||
|
||||
it "fails with missing parameters" do
|
||||
get(
|
||||
"/api/v1/search/tags",
|
||||
params: {access_token: access_token}
|
||||
)
|
||||
confirm_api_error(response, 422, "Search request could not be processed")
|
||||
end
|
||||
|
||||
it "fails with bad credentials" do
|
||||
get(
|
||||
"/api/v1/search/tags",
|
||||
params: {query: "apiparty", access_token: invalid_token}
|
||||
)
|
||||
expect(response.status).to eq(401)
|
||||
end
|
||||
end
|
||||
|
||||
def response_body_data(response)
|
||||
JSON.parse(response.body)
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue