Hey all,
Just starting to dabble with Ruby and XML, initial attempts at using
HTTParty have been amazing, so I'm starting to port some of my
clunkier scripts to Ruby... and now I've run into something that's got
me stumped...
I'm trying to submit a request to Google's location API, (Request
format defined at
http://code.google.com/p/gears/wiki/GeolocationAPI?redir=1).
I'm currently doing this via curl, with a command like:
curl
http://www.google.com/loc/json -H "Pragma: no-cache" -H "Cache-
control: no-cache" -d
'{"version":"1.1.0","host":"
maps.google.com","request_address":true,"address_language":"en_GB","wifi_towers":
[{"mac_address": "xx-xx-xx-xx-xx-xx", "signal_strength": -81,"age":
0}]}'
Usually I have multiple WIFI access points defined in the
"wifi_towers" section, but I'm trying to simplify here.
Anyway, I'm trying to port this to Ruby, and what I have so far is:
---------------------------------------------------------------------------------------------------
#!/usr/bin/ruby
require 'rubygems'
require 'httparty'
class GeoLocate
include HTTParty
debug_output $stdout
format :json
base_uri '
http://www.google.com'
headers "Pragma" => "no-cache", "Cache-controle" => "no-cache"
end
queryHash = {}
queryHash['version']='1.1.0'
queryHash['host']='
maps.google.com'
queryHash['request_address']=true
queryHash['address_language']='en_GB'
queryHash['wifi_towers']={"signal_strength" => -57, "mac_address" =>
"00-24-6c-a9-01-51"}
result = GeoLocate.post('/loc/json',:body => queryHash)
---------------------------------------------------------------------------------------------------
If I pp the queryHash, that looks like what I'm expecting:
{"address_language"=>"en_GB",
"version"=>"1.1.0",
"request_address"=>true,
"host"=>"
maps.google.com",
"wifi_towers"=>{"signal_strength"=>-57, "mac_address"=>"00-24-6c-
a9-01-51"}}
However, when I post that to Google, I get the following response:
---------------------------------------------------------------------------------------------------
opening connection to www.google.com...
opened
<- "POST /loc/json HTTP/1.1
Cache-Controle: no-cache
Content-Type: application/x-www-form-urlencoded
Connection: close
Content-Length: 154
Host:
www.google.com
Pragma: no-cache
"
<-
"address_language=en_GB&version=1.1.0&request_address=true&host=
maps.google.com&wifi_towers[signal_strength]=-57&wifi_towers[mac_address]=00-24-6c-
a9-01-51"
-> "HTTP/1.1 400 Bad Request"
-> "Content-Type: text/plain"
-> "Date: Thu, 23 Jun 2011 00:12:02 GMT"
-> "Expires: Thu, 23 Jun 2011 00:12:02 GMT"
-> "Cache-Control: private, max-age=0"
-> "X-Content-Type-Options: nosniff"
-> "X-Frame-Options: SAMEORIGIN"
-> "X-XSS-Protection: 1; mode=block"
-> "Server: GSE"
-> "Connection: close"
-> ""
reading all...
-> "JSON parsing error.
"
read 20 bytes
Conn close
#<Net::HTTPBadRequest:0x101235b98>
JSON parsing error.
---------------------------------------------------------------------------------------------------
I suppose I could just run curl in the background to get the results,
but I'd really like to start using Ruby object instead of just
dropping to bash commands all the time, not to mention I love how easy
it is to parse the results once HTTParty sorts things out.
Anyway, forgive me if my newby status is making me miss something
glaringly obvious, I'm really hoping that someone out there has an
idea of what I'm missing though.
Thanks in advance,
Jeff