Created
May 19, 2011 19:33
-
-
Save natebenes/981535 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "rubygems" | |
require "crack" | |
require "json" | |
require "sinatra" | |
require "open-uri" | |
# (c)2011 Nate Benes | |
# Curbside JSON server | |
# | |
# This program is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation, either version 3 of the License, or | |
# (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program. If not, see <http://www.gnu.org/licenses/>. | |
get '/' do | |
callback = params.delete('callback') | |
lat = params.delete('lat') | |
# lng? really? you just *had* to save the extra letter | |
lng = params.delete('lng') | |
if lat && lng # make sure we've got the required params | |
# don't need to sanitize too much, we'll let OWH do that | |
url = "http://services.dataomaha.com/omahaneighborhoods/xml/lng="+lng+"&lat="+lat | |
# parse us some XML | |
xmlResponse = Crack::XML.parse(open(url).read) | |
json = xmlResponse.to_json | |
if callback # Its JSONP, wrap response in a function | |
content_type :js | |
response = "#{callback}(#{json})" | |
else # its a normal request | |
content_type :json | |
response = json | |
end | |
else # the user forgot a param or two | |
response = '{"error":"malformed query"}' | |
end | |
# return | |
response | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment