Skip to content

Commit

Permalink
Change API params to ruby standard
Browse files Browse the repository at this point in the history
  • Loading branch information
stefannibrasil committed Sep 27, 2018
1 parent 35449fd commit a16ec11
Show file tree
Hide file tree
Showing 17 changed files with 122 additions and 145 deletions.
48 changes: 21 additions & 27 deletions app/api/srch/search.rb
Expand Up @@ -8,12 +8,12 @@ class Search < Grape::API
helpers SharedParams

# Endpoint definitions
# Basic implementation from classic plots2 SearchController
resource :srch do
# Request URL should be /api/srch/all?srchString=QRY
# Basic implementation from classic plots2 SearchController
# Request URL should be /api/srch/all?query=QRY
desc 'Perform a search of all available resources', hidden: false,
is_array: false,
nickname: 'srchGetAll'
nickname: 'search_all'
params do
use :common
end
Expand Down Expand Up @@ -82,11 +82,10 @@ class Search < Grape::API
end
end

# Request URL should be /api/srch/profiles?srchString=QRY[&sort_by=recent&order_direction=desc&field=username]
# Basic implementation from classic plots2 SearchController
# Request URL should be /api/srch/profiles?query=QRY[&sort_by=recent&order_direction=desc&field=username]
desc 'Perform a search of profiles', hidden: false,
is_array: false,
nickname: 'srchGetProfiles'
nickname: 'search_profiles'

params do
use :common, :sorting, :ordering, :field
Expand All @@ -109,11 +108,10 @@ class Search < Grape::API
end
end

# Request URL should be /api/srch/notes?srchString=QRY
# Basic implementation from classic plots2 SearchController
# Request URL should be /api/srch/notes?query=QRY
desc 'Perform a search of research notes', hidden: false,
is_array: false,
nickname: 'srchGetNotes'
nickname: 'search_notes'

params do
use :common
Expand All @@ -138,11 +136,10 @@ class Search < Grape::API
end
end

# Request URL should be /api/srch/wikis?srchString=QRY
# Basic implementation from classic plots2 SearchController
# Request URL should be /api/srch/wikis?query=QRY
desc 'Perform a search of wikis pages', hidden: false,
is_array: false,
nickname: 'srchGetWikis'
nickname: 'search_wikis'

params do
use :common
Expand All @@ -167,11 +164,10 @@ class Search < Grape::API
end
end

# Request URL should be /api/srch/questions?srchString=QRY
# Basic implementation from classic plots2 SearchController
# Request URL should be /api/srch/questions?query=QRY
desc 'Perform a search of questions tables', hidden: false,
is_array: false,
nickname: 'srchGetQuestions'
nickname: 'search_questions'

params do
use :common
Expand All @@ -197,11 +193,10 @@ class Search < Grape::API
end
end

# Request URL should be /api/srch/tags?srchString=QRY
# Basic implementation from classic plots2 SearchController
# Request URL should be /api/srch/tags?query=QRY
desc 'Perform a search of documents associated with tags within the system', hidden: false,
is_array: false,
nickname: 'srchGetByTags'
nickname: 'search_tags'

params do
use :common
Expand All @@ -226,11 +221,11 @@ class Search < Grape::API
end
end

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

params do
use :common, :additional
Expand Down Expand Up @@ -259,11 +254,11 @@ class Search < Grape::API
end

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

params do
use :common, :additional
Expand Down Expand Up @@ -291,11 +286,10 @@ class Search < Grape::API
end
end

# Request URL should be /api/srch/places?srchString=QRY
# Basic implementation from classic plots2 SearchController
# Request URL should be /api/srch/places?query=QRY
desc 'Perform a search of places', hidden: false,
is_array: false,
nickname: 'srchPlaces'
nickname: 'search_places'

params do
use :common
Expand Down Expand Up @@ -323,7 +317,7 @@ class Search < Grape::API

def self.execute(endpoint, params)
search_type = endpoint
search_criteria = SearchCriteria.from_params(params)
search_criteria = SearchCriteria.new(params)

if search_criteria.valid?
ExecuteSearch.new.by(search_type, search_criteria)
Expand Down
6 changes: 3 additions & 3 deletions app/api/srch/shared_params.rb
Expand Up @@ -5,12 +5,12 @@ module SharedParams
extend Grape::API::Helpers

params :common do
requires :srchString, type: String, documentation: { example: 'Spec' }
optional :limit, type: Integer, documentation: { example: 0 }
requires :query, type: String, documentation: { example: 'Spec' }
optional :limit, type: Integer, documentation: { example: 10 }
end

params :additional do
optional :tagName, type: String, documentation: { example: 'awesome' }
optional :tag, type: String, documentation: { example: 'awesome' }
end

params :ordering do
Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/atwho_autocomplete.js
Expand Up @@ -4,7 +4,7 @@
at: "@",
callbacks: {
remoteFilter: function(query, callback) {
$.getJSON("/api/srch/profiles?srchString=" + query + "&sort_by=recent&field=username", {}, function(data) {
$.getJSON("/api/srch/profiles?query=" + query + "&sort_by=recent&field=username", {}, function(data) {
if (data.hasOwnProperty('items') && data.items.length > 0) {
callback(data.items.map(function(i) { return i.docTitle }));
}
Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/restful_typeahead.js
Expand Up @@ -14,7 +14,7 @@ $(function() {
autoSelect: false,
source: function (query, process) {
var encoded_query = encodeURIComponent(query);
return $.getJSON('/api/srch/all?srchString=' + encoded_query, function (data) {
return $.getJSON('/api/srch/all?query=' + encoded_query, function (data) {
return process(data.items);
},'json');
},
Expand Down
20 changes: 8 additions & 12 deletions app/models/search_request.rb
Expand Up @@ -3,36 +3,32 @@ class SearchRequest
# Minimum query length, or we return an empty result
MIN_QUERY_LENGTH = 3

attr_accessor :srchString, :seq, :showCount, :pageNum, :tagName
attr_accessor :query, :seq, :tag

def initialize; end

def self.fromRequest(rparams)
obj = new
obj.srchString = rparams[:srchString]
obj.query = rparams[:query]
obj.seq = rparams[:seq]
obj.showCount = rparams[:showCount]
obj.pageNum = rparams[:pageNum]
obj.tagName = rparams[:tagName]
obj.tag = rparams[:tag]
obj
end

# Check the query string to make sure it is not blank (null, empty string, or ' ')
# and make sure it is at least 3 characters
def valid?
isValid = true
isValid &&= !srchString.blank?
isValid &&= srchString.length >= MIN_QUERY_LENGTH
isValid &&= !query.blank?
isValid &&= query.length >= MIN_QUERY_LENGTH
isValid
end

# This subclass is used to auto-generate the RESTful data structure. It is generally not useful for internal Ruby usage
# but must be included for full RESTful functionality.
class Entity < Grape::Entity
expose :srchString, documentation: { type: 'String', desc: 'Search Query text.' }
expose :seq, documentation: { type: 'Integer', desc: 'Sequence value passed from client through to the SearchResult. For client sequencing usage' }
expose :showCount, documentation: { type: 'Integer', desc: 'The requested number of records to show per page' }
expose :pageNum, documentation: { type: 'Integer', desc: 'Which page (zero-based counting, as in Array indexes) to show paginated data.' }
expose :tagName, documentation: { type: 'String', desc: 'To search users having specified tagName.' }
expose :query, documentation: { type: 'String', desc: 'Search Query text.' }
expose :seq, documentation: { type: 'Integer', desc: 'Sequence value passed from client through to the SearchResult. For client sequencing usage' }
expose :tag, documentation: { type: 'String', desc: 'Refine search by specified tag.' }
end
end
11 changes: 0 additions & 11 deletions app/services/search_criteria.rb
Expand Up @@ -11,17 +11,6 @@ def initialize(args)
@limit = args[:limit] || 10
end

def self.from_params(params)
args = {
query: params[:srchString],
tag: params[:tagName],
sort_by: params[:sort_by],
field: params[:field],
limit: params[:limit]
}
new(args)
end

def valid?
!query.nil? && query != 0
end
Expand Down
21 changes: 10 additions & 11 deletions app/services/search_service.rb
Expand Up @@ -26,7 +26,6 @@ def search_all(search_criteria)

# Search profiles for matching text with optional order_by=recent param and
# sorted direction DESC by default
# then the list is packaged up as a DocResult

# If no sort_by value present, then it returns a list of profiles ordered by id DESC
# a recent activity may be a node creation or a node revision
Expand Down Expand Up @@ -63,8 +62,8 @@ def search_maps(input, limit = 25, order = :natural, type = :boolean)

# The search string that is passed in is split into tokens, and the tag names are compared and
# chained to the notes that are tagged with those values
def search_tags(srchString, limit = 10)
sterms = srchString.split(' ')
def search_tags(query, limit = 10)
sterms = query.split(' ')
tlist = Tag.where(name: sterms)
.joins(:node_tag)
.joins(:node)
Expand All @@ -73,7 +72,7 @@ def search_tags(srchString, limit = 10)
.limit(limit)
end

# Search question entries for matching text and package up as a DocResult
# Search question entries for matching text
def search_questions(input, limit = 25, order = :natural, type = :boolean)
Node.search(query: input, order: order, type: type, limit: limit)
.where("`node`.`type` = 'note'")
Expand All @@ -84,17 +83,17 @@ def search_questions(input, limit = 25, order = :natural, type = :boolean)

# Search nearby nodes with respect to given latitude, longitute and tags
# and package up as a DocResult
def tagNearbyNodes(srchString, tagName, limit = 10)
raise("Must separate coordinates with ,") unless srchString.include? ","
def tagNearbyNodes(query, tag, limit = 10)
raise("Must separate coordinates with ,") unless query.include? ","

lat, lon = srchString.split(',')
lat, lon = query.split(',')

nodes_scope = NodeTag.joins(:tag)
.where('name LIKE ?', 'lat:' + lat[0..lat.length - 2] + '%')

if tagName.present?
if tag.present?
nodes_scope = NodeTag.joins(:tag)
.where('name LIKE ?', tagName)
.where('name LIKE ?', tag)
.where(nid: nodes_scope.select(:nid))
end

Expand All @@ -119,7 +118,7 @@ def tagNearbyNodes(srchString, tagName, limit = 10)
# returned and as optional parameter a user tag. If the user tag
# is present, the method returns only the location of people
# with that specific user tag.
def people_locations(srchString, user_tag = nil)
def people_locations(query, user_tag = nil)
user_locations = User.where('rusers.status <> 0')\
.joins(:user_tags)\
.where('value LIKE "lat:%"')\
Expand All @@ -132,7 +131,7 @@ def people_locations(srchString, user_tag = nil)
.where(id: user_locations.select("rusers.id"))
end

user_locations.limit(srchString)
user_locations.limit(query)
end

def find_users(query, limit, type = nil)
Expand Down
12 changes: 6 additions & 6 deletions app/views/editor/questionRich.html.erb
Expand Up @@ -44,11 +44,11 @@
<br />
<div>
<input class="form-control input-lg" type="text" placeholder="What's your question? Be specific." value="<%= if @node then @node.title else params[:title] end %>" />
</div>
</div>

</div>
</div>

</div>
</div>
<!-- end title module -->

<!-- body module -->
Expand Down Expand Up @@ -108,7 +108,7 @@
</div>

</div>

</div>
</div>

Expand Down Expand Up @@ -164,8 +164,8 @@
titleModule: {
suggestRelated: true,
fetchRelated: function(show) {
$.getJSON("/api/srch/notes?srchString=" + editor.titleModule.value(), function(response) {
/* API provides:
$.getJSON("/api/srch/notes?query=" + editor.titleModule.value(), function(response) {
/* API provides:
{"items":[{
"docId":14022,
"docType":"file",
Expand Down
2 changes: 1 addition & 1 deletion app/views/editor/rich.html.erb
Expand Up @@ -255,7 +255,7 @@
titleModule: {
suggestRelated: true,
fetchRelated: function(show) {
$.getJSON("/api/srch/notes?srchString=" + editor.titleModule.value(), function(response) {
$.getJSON("/api/srch/notes?query=" + editor.titleModule.value(), function(response) {
/* API provides:
{"items":[{
"docId":14022,
Expand Down
2 changes: 1 addition & 1 deletion app/views/editor/wikiRich.html.erb
Expand Up @@ -213,7 +213,7 @@
titleModule: {
suggestRelated: true,
fetchRelated: function(show) {
$.getJSON("/api/srch/notes?srchString=" + editor.titleModule.value(), function(response) {
$.getJSON("/api/srch/notes?query=" + editor.titleModule.value(), function(response) {
/* API provides:
{"items":[{
"docId":14022,
Expand Down
19 changes: 9 additions & 10 deletions app/views/map/_leaflet.html.erb
Expand Up @@ -15,28 +15,28 @@
<div class="leaflet-map" id="map<%= unique_id %>"></div>
<% if defined? people %><p><i><small>Share your own location on <a href='/profile'>your profile</a>.</small></i></p><% end %>
<script>

var bounds = new L.LatLngBounds(new L.LatLng(84.67351257 , -172.96875) , new L.LatLng(-54.36775852 , 178.59375)) ;

var map<%= unique_id %> = L.map('map<%= unique_id %>' , {
maxBounds: bounds ,
maxBounds: bounds ,
maxBoundsViscosity: 0.75
}).on('load', onMapLoad).setView([<%= lat %>,<%= lon %>], <%= lat.to_s.length.to_i %> + 6);

var map_lat = <%= lat %> ;
var map_lon = <%= lon %> ;
var map_lon = <%= lon %> ;

setupFullScreen(map<%= unique_id %> , map_lat , map_lon) ;
setupFullScreen(map<%= unique_id %> , map_lat , map_lon) ;
setupLEL(map<%= unique_id %> ) ;
// var hash = new L.Hash(map< unique_id > );
// var hash = new L.Hash(map< unique_id > );

function onMapLoad(e){
var lat = <%= lat %> ;
var lon = <%= lon %> ;
var s = lat+","+lon ;
$.getJSON("/api/srch/locations?srchString="+s , function(data){
$.getJSON("/api/srch/locations?query="+s , function(data){
if (!!data.items){
for (i = 0; i < data.items.length ; i++) {
for (i = 0; i < data.items.length ; i++) {
var url = data.items[i].docUrl ;
var title = data.items[i].docTitle ;
var default_url = PLmarker_default() ;
Expand All @@ -46,4 +46,3 @@
});
}
</script>

0 comments on commit a16ec11

Please sign in to comment.