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

Refactor typeahead service #3107

Merged
merged 2 commits into from
Jul 20, 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
5 changes: 5 additions & 0 deletions app/api/srch/shared_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,10 @@ module SharedParams
params :additional do
optional :tagName, type: String, documentation: { example: 'awesome' }
end

params :commontypeahead do
requires :srchString, type: String, documentation: { example: 'Spec' }
optional :seq, type: Integer, documentation: { example: 995 }
end
end
end
84 changes: 30 additions & 54 deletions app/api/srch/typeahead.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

module Srch
class Typeahead < Grape::API
# we are using a group of reusable parameters using a shared params helper
# see /app/api/srch/shared_params.rb
helpers SharedParams
# Number of top values of each type to return
TYPEAHEAD_LIMIT = 10

Expand All @@ -14,17 +17,10 @@ class Typeahead < Grape::API
is_array: false,
nickname: 'typeaheadGetAll'
params do
requires :srchString, type: String, documentation: { example: 'Spec' }
optional :seq, type: Integer, documentation: { example: 995 }
use :commontypeahead
end
get :all do
sresult = TagList.new
sparms = SearchRequest.fromRequest(params)
if sparms.valid?
sresult = TypeaheadService.new.search_all(params[:srchString], TYPEAHEAD_LIMIT)
end
sresult.srchParams = sparms
present sresult, with: TagList::Entity
present Typeahead.execute(:all, params), with: TagList::Entity
end

# Request URL should be /api/typeahead/profiles?srchString=QRY&seq=KEYCOUNT
Expand All @@ -33,17 +29,10 @@ class Typeahead < Grape::API
is_array: false,
nickname: 'typeaheadGetProfiles'
params do
requires :srchString, type: String, documentation: { example: 'Spec' }
optional :seq, type: Integer, documentation: { example: 995 }
use :commontypeahead
end
get :profiles do
sresult = TagList.new
sparms = SearchRequest.fromRequest(params)
if sparms.valid?
sresult = TypeaheadService.new.search_profiles(params[:srchString], TYPEAHEAD_LIMIT)
end
sresult.srchParams = sparms
present sresult, with: TagList::Entity
present Typeahead.execute(:profiles, params), with: TagList::Entity
end

# Request URL should be /api/typeahead/notes?srchString=QRY&seq=KEYCOUNT
Expand All @@ -52,17 +41,10 @@ class Typeahead < Grape::API
is_array: false,
nickname: 'typeaheadGetNotes'
params do
requires :srchString, type: String, documentation: { example: 'Spec' }
optional :seq, type: Integer, documentation: { example: 995 }
use :commontypeahead
end
get :notes do
sresult = TagList.new
sparms = SearchRequest.fromRequest(params)
if sparms.valid?
sresult = TypeaheadService.new.search_notes(params[:srchString], TYPEAHEAD_LIMIT)
end
sresult.srchParams = sparms
present sresult, with: TagList::Entity
present Typeahead.execute(:notes, params), with: TagList::Entity
end

# Request URL should be /api/typeahead/questions?srchString=QRY&seq=KEYCOUNT
Expand All @@ -71,17 +53,10 @@ class Typeahead < Grape::API
is_array: false,
nickname: 'typeaheadGetQuestions'
params do
requires :srchString, type: String, documentation: { example: 'Spec' }
optional :seq, type: Integer, documentation: { example: 995 }
use :commontypeahead
end
get :questions do
sresult = TagList.new
sparms = SearchRequest.fromRequest(params)
if sparms.valid?
sresult = TypeaheadService.new.search_questions(params[:srchString], TYPEAHEAD_LIMIT)
end
sresult.srchParams = sparms
present sresult, with: TagList::Entity
present Typeahead.execute(:questions, params), with: TagList::Entity
end

# Request URL should be /api/typeahead/tags?srchString=QRY&seq=KEYCOUNT
Expand All @@ -90,17 +65,10 @@ class Typeahead < Grape::API
is_array: false,
nickname: 'typeaheadGetTags'
params do
requires :srchString, type: String, documentation: { example: 'Spec' }
optional :seq, type: Integer, documentation: { example: 995 }
use :commontypeahead
end
get :tags do
sresult = TagList.new
sparms = SearchRequest.fromRequest(params)
if sparms.valid?
TypeaheadService.new.search_tags(params[:srchString], TYPEAHEAD_LIMIT)
end
sresult.srchParams = sparms
present sresult, with: TagList::Entity
present Typeahead.execute(:tags, params), with: TagList::Entity
end

# Request URL should be /api/typeahead/comments?srchString=QRY&seq=KEYCOUNT
Expand All @@ -109,20 +77,28 @@ class Typeahead < Grape::API
is_array: false,
nickname: 'typeaheadGetComments'
params do
requires :srchString, type: String, documentation: { example: 'Spec' }
optional :seq, type: Integer, documentation: { example: 995 }
use :commontypeahead
end
get :comments do
sresult = TagList.new
sparms = SearchRequest.fromRequest(params)
if sparms.valid?
TypeaheadService.new.search_comments(params[:srchString], TYPEAHEAD_LIMIT)
end
sresult.srchParams = sparms
present sresult, with: TagList::Entity
present Typeahead.execute(:comments, params), with: TagList::Entity
end

# end of endpoint definitions
end

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

if search_criteria.valid?
sresult = ExecuteTypeahead.new.by(search_type, search_criteria, TYPEAHEAD_LIMIT)
end

sparms = SearchRequest.fromRequest(params)
sresult.srchParams = sparms
sresult
end
end
end
28 changes: 28 additions & 0 deletions app/services/execute_typeahead.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class ExecuteTypeahead
def by(type, search_criteria, limit)
execute(type, search_criteria, limit)
end

private

def execute(type, search_criteria, limit)
sservice = TypeaheadService.new
sresult = TagList.new
result = case type
when :all
sservice.search_all(search_criteria.query, limit)
when :profiles
sservice.search_profiles(search_criteria.query, limit)
when :notes
sservice.search_notes(search_criteria.query, limit)
when :questions
sservice.search_questions(search_criteria.query, limit)
when :tags
sservice.search_tags(search_criteria.query, limit)
when :comments
sservice.search_comments(search_criteria.query, limit)
else
[]
end
end
end
1 change: 0 additions & 1 deletion test/functional/search_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ def app
matcher = JsonExpressions::Matcher.new(pattern)

json = JSON.parse(last_response.body)
puts json.inspect
assert matcher =~ json

end
Expand Down
97 changes: 41 additions & 56 deletions test/functional/typeahead_api_test.rb
Original file line number Diff line number Diff line change
@@ -1,138 +1,123 @@
require 'test_helper'

# TODO: Rework to get better inclusion of JsonExpressions pattern matching
# TODO: Add tests for negative matches--check echo of search parameters and null result
# HACK: Parameterize the 'get' URLs to make passing and changing test values easier

class TypeaheadApiTest < ActiveSupport::TestCase
include Rack::Test::Methods

def app
Rails.application
end

def setup
@stxt = 'lat'
@sprofile = 'adm'
@stags = 'everything'
@sseq = 7
@scom = 'comm'
end

test 'typeahead all functionality' do
get '/api/typeahead/all?srchString=lat&seq=7'
get '/api/typeahead/all?srchString=Blog'
assert last_response.ok?

# Expected typeahead pattern
pattern = {
# TODO: Need more/better understanding and data for the test database
# return will be nil for now
# items: nil,
srchParams: {
srchString: @stxt,
seq: @sseq
srchString: 'Blog',
seq: nil
}.ignore_extra_keys!
}.ignore_extra_keys!

matcher = JsonExpressions::Matcher.new(pattern)

assert matcher =~ JSON.parse(last_response.body)
json = JSON.parse(last_response.body)

assert matcher =~ json
end

test 'typeahead profile functionality' do
get '/api/typeahead/profiles?srchString=adm&seq=7'
get '/api/typeahead/profiles?srchString=Jeff'
assert last_response.ok?

# Expected profile response pattern
pattern = {
# TODO: Need more/better understanding and data for the test database
# return will be nil for now
# items: nil,
srchParams: {
srchString: @sprofile,
seq: @sseq
srchString: 'Jeff',
Copy link
Member

Choose a reason for hiding this comment

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

So much better/more readable!!

seq: nil
}.ignore_extra_keys!
}.ignore_extra_keys!

matcher = JsonExpressions::Matcher.new(pattern)

assert matcher =~ JSON.parse(last_response.body)
json = JSON.parse(last_response.body)

assert matcher =~ json
end

test 'typeahead notes functionality' do
get '/api/typeahead/notes?srchString=lat&seq=7'
get '/api/typeahead/notes?srchString=Blog'
Copy link
Member

Choose a reason for hiding this comment

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

Nice, a more realistic and readable API call too...

assert last_response.ok?

# Expected notes pattern
pattern = {
# TODO: Need more/better understanding and data for the test database
# return will be nil for now
# items: nil,
srchParams: {
srchString: @stxt,
seq: @sseq
srchString: 'Blog',
seq: nil
}.ignore_extra_keys!
}.ignore_extra_keys!

matcher = JsonExpressions::Matcher.new(pattern)

assert matcher =~ JSON.parse(last_response.body)
json = JSON.parse(last_response.body)

assert matcher =~ json
end

test 'typeahead questions functionality' do
get '/api/typeahead/questions?srchString=lat&seq=7'
get '/api/typeahead/questions?srchString=Question'
assert last_response.ok?

# Expected question pattern
# Returns null right now for test--need to set a better search sequence on demo seed data
pattern = {
# TODO: Need more/better understanding and data for the test database
# return will be nil for now
# items: nil,
srchParams: {
srchString: @stxt,
seq: @sseq
}.ignore_extra_keys!
}.ignore_extra_keys!
srchString: 'Question',
seq: nil,
}.ignore_extra_keys!
}.ignore_extra_keys!

matcher = JsonExpressions::Matcher.new(pattern)
matcher = JsonExpressions::Matcher.new(pattern)

assert matcher =~ JSON.parse(last_response.body)
json = JSON.parse(last_response.body)

assert matcher =~ json
end

test 'typeahead tags functionality' do
get '/api/typeahead/tags?srchString=everything&seq=7'
get '/api/typeahead/tags?srchString=everything'
assert last_response.ok?

# Expected tag pattern
pattern = {
# TODO: Need more/better understanding and data for the test database
# return will be nil for now
# items: nil,
srchParams: {
srchString: @stags,
seq: @sseq
srchString: 'everything',
seq: nil
}.ignore_extra_keys!
}.ignore_extra_keys!

matcher = JsonExpressions::Matcher.new(pattern)

assert matcher =~ JSON.parse(last_response.body)
json = JSON.parse(last_response.body)

assert matcher =~ json
end

test 'typeahead comments functionality' do
get '/api/typeahead/comments?srchString=comm&seq=7'
get '/api/typeahead/comments?srchString=comment'
assert last_response.ok?

# Expected comment pattern
pattern = {
srchParams: {
srchString: @scom,
seq: @sseq
srchString: 'comment',
seq: nil
}.ignore_extra_keys!
}.ignore_extra_keys!
}.ignore_extra_keys!

matcher = JsonExpressions::Matcher.new(pattern)

assert matcher =~ JSON.parse(last_response.body)
json = JSON.parse(last_response.body)

assert matcher =~ json
end
end