Skip to content

Commit

Permalink
Added Javascript library for interface. Changed interface names from …
Browse files Browse the repository at this point in the history
…'location' to 'coordinates'.
  • Loading branch information
petewarden committed Mar 16, 2011
1 parent cace4a6 commit 4a2f692
Show file tree
Hide file tree
Showing 12 changed files with 1,282 additions and 642 deletions.
2 changes: 0 additions & 2 deletions docs/amicreation.txt
Expand Up @@ -26,8 +26,6 @@ sudo aptitude update
sudo aptitude install unzip
unzip ec2-ami-tools.zip
sudo mv ec2-ami-tools-1.3-66634 /opt/
export PATH=$PATH:/opt/ec2-ami-tools-1.3-66634/bin
export EC2_HOME=/opt/ec2-ami-tools-1.3-66634/
wget http://s3.amazonaws.com/ec2-downloads/ec2-api-tools.zip
sudo unzip -d /opt/ ec2-api-tools.zip
export PATH=$PATH:/opt/ec2-api-tools-1.4.1.2/bin
Expand Down
4 changes: 4 additions & 0 deletions docs/serversetup.txt
Expand Up @@ -42,12 +42,16 @@ sudo bash -c 'echo "
<VirtualHost *:80>
ServerName www.yourhost.com
DocumentRoot /home/ubuntu/sources/geodictapi/public
RewriteEngine On
RewriteCond %{HTTP_HOST} ^geodictapi.com$ [NC]
RewriteRule ^(.*)$ http://www.geodictapi.com$1 [R=301,L]
<Directory /home/ubuntu/sources/geodictapi/public>
AllowOverride all
Options -MultiViews
</Directory>
</VirtualHost>
" > /etc/apache2/sites-enabled/000-default'
sudo ln -s /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled/rewrite.load

sudo /etc/init.d/apache2 restart

Expand Down
3 changes: 3 additions & 0 deletions geodict_config.rb
Expand Up @@ -44,5 +44,8 @@ module GeodictConfig

# The location of the MaxMind database file holding IP to location mappings
IP_MAPPING_DATABASE = '../geodictdata/GeoLiteCity.dat'

# The version of the API this code implements
API_VERSION = 130

end
101 changes: 79 additions & 22 deletions geodict_server.rb
Expand Up @@ -364,9 +364,40 @@ def placemaker_api_call(params)
return result
end

# A more standard interface to the same Placemaker functionality for pulling locations from text
def text2places(text, callback=nil)

locations = find_locations_in_text(text)

# Convert the raw locations into a form that makes sense for output
output_locations = []
locations.each_with_index do |location_info, index|

found_tokens = location_info[:found_tokens]

match_start_index = found_tokens[0][:start_index]
match_end_index = found_tokens[found_tokens.length-1][:end_index]
matched_string = input_text[match_start_index..match_end_index]

location = found_tokens[0]
output_locations.push({
:type => location[:type],
:name => location[:matched_string],
:latitude => location[:lat].to_s,
:longitude => location[:lon].to_s,
:start_index => location[:start_index].to_s,
:end_index => location[:end_index].to_s,
:matched_string => matched_string
})
end

make_json(output_locations, callback)

end

# Takes an array of IP addresses as input, and looks up their locations using the
# free database from GeoMind
def ip2location(ips, callback=nil)
def ip2coordinates(ips, callback=nil)

geoip = Net::GeoIP.new(GeodictConfig::IP_MAPPING_DATABASE)

Expand Down Expand Up @@ -415,7 +446,7 @@ def ips_list_from_string(ips_string)

# Takes an array of postal addresses as input, and looks up their locations using
# data from the US census
def street2location(addresses, callback=nil)
def street2coordinates(addresses, callback=nil)

db = Geocoder::US::Database.new('../geocoderdata/geocoder.db', {:debug => false})

Expand Down Expand Up @@ -459,7 +490,7 @@ def street2location(addresses, callback=nil)
def addresses_list_from_string(addresses_string, callback=nil)

if addresses_string == ''
fatal_error('Empty string passed in to street2location',
fatal_error('Empty string passed in to street2coordinates',
'json', 500, callback)
end

Expand All @@ -484,7 +515,7 @@ def addresses_list_from_string(addresses_string, callback=nil)

# Takes an array of coordinates as input, and looks up what political areas they lie
# within
def location2politics(locations, callback=nil)
def coordinates2politics(locations, callback=nil)

conn = PGconn.connect(GeodictConfig::HOST, GeodictConfig::PORT, '', '', GeodictConfig::REVERSE_GEO_DATABASE, GeodictConfig::USER, GeodictConfig::PASSWORD)

Expand Down Expand Up @@ -587,7 +618,7 @@ def location2politics(locations, callback=nil)
def locations_list_from_string(locations_string, callback=nil)

if locations_string == ''
fatal_error('Empty string passed in to location2politics',
fatal_error('Empty string passed in to coordinates2politics',
'json', 500, callback)
end

Expand All @@ -598,7 +629,7 @@ def locations_list_from_string(locations_string, callback=nil)
else
coordinates = locations_string.split(',')
if coordinates.length != 2
fatal_error('Couldn\t understand string "'+locations_string+'" passed into location2politics',
fatal_error('Couldn\t understand string "'+locations_string+'" passed into coordinates2politics',
'json', 500, callback)
end
result = [{ :latitude => coordinates[0], :longitude => coordinates[1] }]
Expand Down Expand Up @@ -640,6 +671,15 @@ def locations_list_from_string(locations_string, callback=nil)
# API entry points #
########################################

# Returns version information about this server
get '/info' do

callback = params[:callback]

make_json({:version => GeodictConfig::API_VERSION}, callback)

end

# The normal POST interface for Yahoo's Placemaker
post '/v1/document' do
placemaker_api_call(params)
Expand All @@ -650,8 +690,25 @@ def locations_list_from_string(locations_string, callback=nil)
placemaker_api_call(params)
end

# The more standard REST/JSON interface for the Placemaker emulation
post '/text2places' do

# Pull in the raw data in the body of the request
text = request.env['rack.input'].read

text2places(text)
end

# Also support a non-standard GET version of the API for Javascript clients
get '/text2places/*' do
callback = params[:callback]
text = params['splat'][0]

text2places(text, callback)
end

# The POST interface for the IP address to location lookup
post '/ip2location' do
post '/ip2coordinates' do
# Pull in the raw data in the body of the request
ips_string = request.env['rack.input'].read

Expand All @@ -661,11 +718,11 @@ def locations_list_from_string(locations_string, callback=nil)
end
ips_list = ips_list_from_string(ips_string)

ip2location(ips_list)
ip2coordinates(ips_list)
end

# The GET interface for the IP address to location lookup
get '/ip2location/:ips' do
get '/ip2coordinates/:ips' do

callback = params[:callback]
ips_string = params[:ips]
Expand All @@ -676,11 +733,11 @@ def locations_list_from_string(locations_string, callback=nil)

ips_list = ips_list_from_string(ips_string)

ip2location(ips_list, callback)
ip2coordinates(ips_list, callback)
end

# The POST interface for the street address to location lookup
post '/street2location' do
post '/street2coordinates' do
begin
# Pull in the raw data in the body of the request
addresses_string = request.env['rack.input'].read
Expand All @@ -691,15 +748,15 @@ def locations_list_from_string(locations_string, callback=nil)
end
addresses_list = addresses_list_from_string(addresses_string)

street2location(addresses_list)
street2coordinates(addresses_list)
rescue
fatal_error('street2location error: '+$!.inspect + $@.inspect, 'json', 500)
fatal_error('street2coordinates error: '+$!.inspect + $@.inspect, 'json', 500)
end

end

# The GET interface for the street address to location lookup
get '/street2location/*' do
get '/street2coordinates/*' do

callback = params[:callback]

Expand All @@ -712,15 +769,15 @@ def locations_list_from_string(locations_string, callback=nil)

addresses_list = addresses_list_from_string(addresses_string, callback)

street2location(addresses_list, callback)
street2coordinates(addresses_list, callback)
rescue
fatal_error('street2location error: '+$!.inspect + $@.inspect, 'json', 500, callback)
fatal_error('street2coordinates error: '+$!.inspect + $@.inspect, 'json', 500, callback)
end

end

# The POST interface for the location to political areas lookup
post '/location2politics' do
post '/coordinates2politics' do
begin
# Pull in the raw data in the body of the request
locations_string = request.env['rack.input'].read
Expand All @@ -732,16 +789,16 @@ def locations_list_from_string(locations_string, callback=nil)

locations_list = locations_list_from_string(locations_string)

location2politics(locations_list)
coordinates2politics(locations_list)

rescue
fatal_error('location2politics error: '+$!.inspect + $@.inspect, 'json', 500)
fatal_error('coordinates2politics error: '+$!.inspect + $@.inspect, 'json', 500)
end

end

# The GET interface for the location to political areas lookup
get '/location2politics/*' do
get '/coordinates2politics/*' do

callback = params[:callback]

Expand All @@ -754,9 +811,9 @@ def locations_list_from_string(locations_string, callback=nil)

locations_list = locations_list_from_string(locations_string, callback)

location2politics(locations_list, callback)
coordinates2politics(locations_list, callback)
# rescue
# fatal_error('location2politics error: '+$!.inspect + $@.inspect, 'json', 500, callback)
# fatal_error('coordinates2politics error: '+$!.inspect + $@.inspect, 'json', 500, callback)
# end

end
Expand Down
37 changes: 20 additions & 17 deletions geodictapi.xcodeproj/petewarden.mode1v3
Expand Up @@ -275,8 +275,9 @@
<key>PBXSmartGroupTreeModuleOutlineStateSelectionKey</key>
<array>
<array>
<integer>28</integer>
<integer>26</integer>
<integer>18</integer>
<integer>17</integer>
<integer>12</integer>
<integer>0</integer>
</array>
</array>
Expand Down Expand Up @@ -314,26 +315,24 @@
<key>Dock</key>
<array>
<dict>
<key>BecomeActive</key>
<true/>
<key>ContentConfiguration</key>
<dict>
<key>PBXProjectModuleGUID</key>
<string>1CE0B20306471E060097A5F4</string>
<key>PBXProjectModuleLabel</key>
<string>developerdocs.haml</string>
<string>jquery.dstk.js</string>
<key>PBXSplitModuleInNavigatorKey</key>
<dict>
<key>Split0</key>
<dict>
<key>PBXProjectModuleGUID</key>
<string>1CE0B20406471E060097A5F4</string>
<key>PBXProjectModuleLabel</key>
<string>developerdocs.haml</string>
<string>jquery.dstk.js</string>
<key>_historyCapacity</key>
<integer>0</integer>
<key>bookmark</key>
<string>597D8849132FC81F0042C251</string>
<string>597D8883133069180042C251</string>
<key>history</key>
<array>
<string>5929E12313244A2300B81D2E</string>
Expand All @@ -344,7 +343,6 @@
<string>5929E20313258DC800B81D2E</string>
<string>5929E20513258DC800B81D2E</string>
<string>5929E20613258DC800B81D2E</string>
<string>5929E23D1325980D00B81D2E</string>
<string>5929E24D13259AB700B81D2E</string>
<string>5929E2D51325B60D00B81D2E</string>
<string>5929E2F8132964BC00B81D2E</string>
Expand All @@ -361,8 +359,11 @@
<string>597D880C132F17990042C251</string>
<string>597D883D132F26870042C251</string>
<string>597D8841132F382D0042C251</string>
<string>597D8845132F51DC0042C251</string>
<string>597D8846132F51DC0042C251</string>
<string>597D885E133062410042C251</string>
<string>597D885F133062410042C251</string>
<string>597D8860133062410042C251</string>
<string>597D886E133067220042C251</string>
<string>597D886F133067220042C251</string>
</array>
</dict>
<key>SplitCount</key>
Expand All @@ -384,24 +385,26 @@
<string>666pt</string>
</dict>
<dict>
<key>BecomeActive</key>
<true/>
<key>ContentConfiguration</key>
<dict>
<key>PBXProjectModuleGUID</key>
<string>1CE0B20306471E060097A5F4</string>
<key>PBXProjectModuleLabel</key>
<string>layout.haml</string>
<string>welcome.haml</string>
<key>PBXSplitModuleInNavigatorKey</key>
<dict>
<key>Split0</key>
<dict>
<key>PBXProjectModuleGUID</key>
<string>1CE0B20406471E060097A5F4</string>
<key>PBXProjectModuleLabel</key>
<string>layout.haml</string>
<string>welcome.haml</string>
<key>_historyCapacity</key>
<integer>0</integer>
<key>bookmark</key>
<string>597D884A132FC81F0042C251</string>
<string>597D8884133069180042C251</string>
<key>history</key>
<array>
<string>5929E12513244A2300B81D2E</string>
Expand All @@ -424,12 +427,12 @@
<string>5929E3E6132C496100B81D2E</string>
<string>5929E3E8132C496100B81D2E</string>
<string>5929E3FF132C530800B81D2E</string>
<string>597D880F132F17990042C251</string>
<string>597D8810132F17990042C251</string>
<string>597D8811132F17990042C251</string>
<string>597D8812132F17990042C251</string>
<string>597D8830132F218A0042C251</string>
<string>597D8831132F218A0042C251</string>
<string>597D886A133063580042C251</string>
<string>597D8871133067220042C251</string>
<string>597D8872133067220042C251</string>
<string>597D8873133067220042C251</string>
</array>
</dict>
<key>SplitCount</key>
Expand Down

0 comments on commit 4a2f692

Please sign in to comment.