Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove API Search endpoints duplication #3045

Merged
merged 4 commits into from
Jul 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 45 additions & 95 deletions app/api/srch/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

module Srch
class Search < Grape::API
# we are using a group of reusable parameters using a shared params helper
# see /app/api/srch/shared_params.rb
helpers SharedParams

# Endpoint definitions
resource :srch do
# Request URL should be /api/srch/all?srchString=QRY[&seq=KEYCOUNT&showCount=NUM_ROWS&pageNum=PAGE_NUM]
Expand All @@ -11,159 +15,105 @@ class Search < Grape::API
is_array: false,
nickname: 'srchGetAll'
params do
requires :srchString, type: String, documentation: { example: 'Spec' }
optional :seq, type: Integer, documentation: { example: 995 }
optional :showCount, type: Integer, documentation: { example: 3 }
optional :pageNum, type: Integer, documentation: { example: 0 }
use :common
end
get :all do
sresult = DocList.new
unless params[:srchString].nil? || params[:srchString] == 0
sservice = SearchService.new
sresult = sservice.textSearch_all(params[:srchString])
end
sparms = SearchRequest.fromRequest(params)
sresult.srchParams = sparms
sresult
Search.execute(:all, params)
end

# Request URL should be /api/srch/profiles?srchString=QRY[&seq=KEYCOUNT&showCount=NUM_ROWS&pageNum=PAGE_NUM]
# Basic implementation from classic plots2 SearchController
desc 'Perform a search of profiles', hidden: false,
is_array: false,
nickname: 'srchGetProfiles'

params do
requires :srchString, type: String, documentation: { example: 'Spec' }
optional :seq, type: Integer, documentation: { example: 995 }
optional :showCount, type: Integer, documentation: { example: 3 }
optional :pageNum, type: Integer, documentation: { example: 0 }
use :common
end
get :profiles do
sresult = DocList.new
unless params[:srchString].nil? || params[:srchString] == 0
sservice = SearchService.new
sresult = sservice.textSearch_profiles(params[:srchString])
end
sparms = SearchRequest.fromRequest(params)
sresult.srchParams = sparms
sresult
Search.execute(:profiles, params)
end

# Request URL should be /api/srch/notes?srchString=QRY[&seq=KEYCOUNT&showCount=NUM_ROWS&pageNum=PAGE_NUM]
# Basic implementation from classic plots2 SearchController
desc 'Perform a search of research notes', hidden: false,
is_array: false,
nickname: 'srchGetNotes'

params do
requires :srchString, type: String, documentation: { example: 'Spec' }
optional :seq, type: Integer, documentation: { example: 995 }
optional :showCount, type: Integer, documentation: { example: 3 }
optional :pageNum, type: Integer, documentation: { example: 0 }
use :common
end
get :notes do
sresult = DocList.new
unless params[:srchString].nil? || params[:srchString] == 0
sservice = SearchService.new
sresult = sservice.textSearch_notes(params[:srchString])
end
sparms = SearchRequest.fromRequest(params)
sresult.srchParams = sparms
sresult
Search.execute(:notes, params)
end

# Request URL should be /api/srch/questions?srchString=QRY[&seq=KEYCOUNT&showCount=NUM_ROWS&pageNum=PAGE_NUM]
# Basic implementation from classic plots2 SearchController
desc 'Perform a search of questions tables', hidden: false,
is_array: false,
nickname: 'srchGetQuestions'

params do
requires :srchString, type: String, documentation: { example: 'Spec' }
optional :seq, type: Integer, documentation: { example: 995 }
optional :showCount, type: Integer, documentation: { example: 3 }
optional :pageNum, type: Integer, documentation: { example: 0 }
use :common
end
get :questions do
sresult = DocList.new
unless params[:srchString].nil? || params[:srchString] == 0
sservice = SearchService.new
sresult = sservice.textSearch_questions(params[:srchString])
end
sparms = SearchRequest.fromRequest(params)
sresult.srchParams = sparms
sresult
Search.execute(:questions, params)
end

# Request URL should be /api/srch/tags?srchString=QRY[&seq=KEYCOUNT&showCount=NUM_ROWS&pageNum=PAGE_NUM]
# Basic implementation from classic plots2 SearchController
desc 'Perform a search of documents associated with tags within the system', hidden: false,
is_array: false,
nickname: 'srchGetByTags'

params do
requires :srchString, type: String, documentation: { example: 'Spec' }
optional :seq, type: Integer, documentation: { example: 995 }
optional :showCount, type: Integer, documentation: { example: 3 }
optional :pageNum, type: Integer, documentation: { example: 0 }
use :common
end
get :tags do
sresult = DocList.new
unless params[:srchString].nil? || params[:srchString] == 0
sservice = SearchService.new
sresult = sservice.textSearch_tags(params[:srchString])
end
sparms = SearchRequest.fromRequest(params)
sresult.srchParams = sparms
sresult
Search.execute(:tags, params)
end

# Request URL should be /api/srch/taglocations?srchString=QRY[&tagName=awesome&seq=KEYCOUNT&showCount=NUM_ROWS&pageNum=PAGE_NUM]
# Note: Query(QRY as above) must have latitude and longitude as srchString=lat,lon
desc 'Perform a search of documents having nearby latitude, longitude and tag values',
hidden: false,
is_array: false,
nickname: 'srchGetTagLocations'
desc 'Perform a search of documents having nearby latitude and longitude tag values', hidden: false,
is_array: false,
nickname: 'srchGetLocations'

params do
requires :srchString, type: String, documentation: { example: 'Spec' }
optional :tagName, type: String, documentation: { example: 'awesome' }
optional :seq, type: Integer, documentation: { example: 995 }
optional :showCount, type: Integer, documentation: { example: 3 }
optional :pageNum, type: Integer, documentation: { example: 0 }
use :common, :additional
end
get :taglocations do
sresult = DocList.new
unless params[:srchString].nil? || params[:srchString] == 0 || !(params[:srchString].include? ",")
sservice = SearchService.new
sresult = sservice.tagNearbyNodes(params[:srchString], params[:tagName])
end
sparms = SearchRequest.fromRequest(params)
sresult.srchParams = sparms
sresult
Search.execute(:taglocations, params)
end

# API TO FETCH QRY RECENT CONTRIBUTORS
# Request URL should be /api/srch/peoplelocations?srchString=QRY[&tagName=group:partsandcrafts&seq=KEYCOUNT&showCount=NUM_ROWS&pageNum=PAGE_NUM]
# QRY should be a number
desc 'Perform a search to show x Recent People',
hidden: false,
is_array: false,
nickname: 'srchGetPeople'
desc 'Perform a search to show x Recent People', hidden: false,
is_array: false,
nickname: 'srchGetPeople'

params do
requires :srchString, type: String, documentation: { example: 'Spec' }
optional :tagName, type: String, documentation: { example: 'group:partsandcrafts' }
optional :seq, type: Integer, documentation: { example: 995 }
optional :showCount, type: Integer, documentation: { example: 3 }
optional :pageNum, type: Integer, documentation: { example: 0 }
use :common, :additional
end
get :peoplelocations do
sresult = DocList.new
unless params[:srchString].nil? || params[:srchString] == 0
sservice = SearchService.new
sresult = sservice.recentPeople(params[:srchString], params[:tagName])
end
sparms = SearchRequest.fromRequest(params)
sresult.srchParams = sparms
sresult
Search.execute(:peoplelocations, params)
end
end

def self.execute(endpoint, params)
sresult = DocList.new
search_query = params[:srchString]
tag_query = params[:tagName]
search_type = endpoint
search_criteria = SearchCriteria.new(search_query, tag_query)

if search_criteria.valid?
sresult = ExecuteSearch.new.by(search_type, search_criteria)
end
# end endpoint definitions
sparms = SearchRequest.fromRequest(params)
sresult.srchParams = sparms
sresult
end
end
end
18 changes: 18 additions & 0 deletions app/api/srch/shared_params.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'grape'

module Srch
module SharedParams
extend Grape::API::Helpers

params :common do
requires :srchString, type: String, documentation: { example: 'Spec' }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fantastic. Thanks!!!

Copy link
Author

@stefannibrasil stefannibrasil Jul 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jywarren we added the test back. We'll probably work more on the tests in the next weeks, but just to be more careful, we think it's best to not remove it for now. Good catch!

we've already done the same strategy here in the Typeahead, we are planning to open a PR for that after this is approved!

Thanks! 💯

optional :seq, type: Integer, documentation: { example: 995 }
optional :showCount, type: Integer, documentation: { example: 3 }
optional :pageNum, type: Integer, documentation: { example: 0 }
end

params :additional do
optional :tagName, type: String, documentation: { example: 'awesome' }
end
end
end
32 changes: 32 additions & 0 deletions app/services/execute_search.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class ExecuteSearch
def by(type, search_criteria)
execute(type, search_criteria)
end

private

def execute(type, search_criteria)
sservice = SearchService.new
sresult = DocList.new
case type
when :all
sresult = sservice.textSearch_all(search_criteria.query)
when :profiles
sresult = sservice.textSearch_profiles(search_criteria.query)
when :notes
sresult = sservice.textSearch_notes(search_criteria.query)
when :questions
sresult = sservice.textSearch_questions(search_criteria.query)
when :tags
sresult = sservice.textSearch_tags(search_criteria.query)
when :peoplelocations
sresult = sservice.recentPeople(search_criteria.query, search_criteria.tag)
when :taglocations
if search_criteria.query.include? ","
sresult = sservice.tagNearbyNodes(search_criteria.query, search_criteria.tag)
end
else
sresult = []
end
end
end
12 changes: 12 additions & 0 deletions app/services/search_criteria.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class SearchCriteria
attr_reader :query, :tag

def initialize(query, tag = nil)
@query = query
@tag = tag
end

def valid?
!query.nil? && query != 0
end
end
Loading