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

API for fetching nearby coordinates information #1978

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions app/api/srch/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,30 @@ class Search < Grape::API
sresult
end

# Request URL should be /api/srch/locations?srchString=QRY[&seq=KEYCOUNT&showCount=NUM_ROWS&pageNum=PAGE_NUM]
Copy link
Member

Choose a reason for hiding this comment

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

Super! Perhaps we can add this also to /doc/API.md?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

# Note: Query(QRY as above) must have latitude and longitude as srchString=lat,lon
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 :seq, type: Integer, documentation: { example: 995 }
optional :showCount, type: Integer, documentation: { example: 3 }
optional :pageNum, type: Integer, documentation: { example: 0 }
end
get :locations do
sresult = DocList.new
unless params[:srchString].nil? || params[:srchString] == 0 || !(params[:srchString].include? ",")
sservice = SearchService.new
Copy link

Choose a reason for hiding this comment

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

params[:srchString].nil? || params[:srchString] == 0
this particular code is kind of used in multiple places in this file. can't be move it to a private method and re-use it relevant place ?

Copy link
Member

Choose a reason for hiding this comment

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

I think so -- but perhaps independently from this PR so we address code repetition as its own issues. Would you mind opening a new issue to address this? I agree that the services and api code in particular can be enormously compacted without loss of organization. Thank you!

sresult = sservice.nearbyNodes(params[:srchString])
end
sparms = SearchRequest.fromRequest(params)
sresult.srchParams = sparms
sresult
end


# end endpoint definitions
end
end
Expand Down
25 changes: 25 additions & 0 deletions app/services/search_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,29 @@ def textSearch_questions(srchString)
end
sresult
end

def nearbyNodes(srchString)
sresult = DocList.new
coordinates = srchString.split(",")
lat = coordinates[0]
lon = coordinates[1]

Copy link

Choose a reason for hiding this comment

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

latitude, longitude = coordinates
directly we can assign, ruby will take care of it.

Copy link
Member Author

Choose a reason for hiding this comment

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

You are absolutely right but I think for sake of clarity of all writing one extra line is good

Copy link
Member

Choose a reason for hiding this comment

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

Good suggestion! I always forget to do this myself.

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

nids = nids || []

Copy link

Choose a reason for hiding this comment

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

nids ||= []

Copy link
Member

Choose a reason for hiding this comment

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

:-)

items = Node.includes(:tag)
.references(:node, :term_data)
.where('node.nid IN (?) AND term_data.name LIKE ?', nids, 'lon:' + lon[0..lon.length - 2] + '%')
.limit(200)
.order('node.nid DESC')

items.each do |match|
doc = DocResult.fromSearch(match.nid, 'coordinates', match.path(:items), match.title, 0, match.answers.length.to_i)
sresult.addDoc(doc)
end
sresult
end
end
1 change: 1 addition & 0 deletions doc/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Per-model API endpoints are:
* Questions: https://publiclab.org/api/srch/questions?srchString=foo
* Tags: https://publiclab.org/api/srch/tags?srchString=foo
* Notes: https://publiclab.org/api/srch/notes?srchString=foo
* Locations: https://publiclab.org/api/srch/locations?srchString=lat,lon

We also provide RSS feeds for tags and authors, in the format:

Expand Down