Skip to content

Instantly share code, notes, and snippets.

@baroquebobcat
Created May 9, 2010 04:08
Show Gist options
  • Select an option

  • Save baroquebobcat/394941 to your computer and use it in GitHub Desktop.

Select an option

Save baroquebobcat/394941 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'json/pure'
require 'httparty'
module FB
class BaseClient
include HTTParty
end
module Graph
class Client < BaseClient
base_uri "https://graph.facebook.com"
def self.access_token= value
default_params :access_token => value
end
end
end
module API
class Client < BaseClient
base_uri "https://api.facebook.com"
def self.access_token= value
default_params :token => value
end
end
end
def self.access_token= value
API::Client.access_token= value
Graph::Client.access_token= value
end
end
class GoogleGeocoder
include HTTParty
#required params
# address or latlng
# sensor
#optional
# bounds
# region
# language
#
base_uri "http://maps.google.com/maps/api/geocode/json"
def self.geocode address
get '', :query => {:address => address, :sensor => false}
end
end
access_token = "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"
FB.access_token= access_token
result = FB::Graph::Client.get('/me/friends')
friends = result.delete "data"
result = FB::API::Client.get '/method/users.getInfo', :query => {
:uids => friends.map{|f|f["id"]},
:fields => %w(current_location name)
}
friends_locations = result["users_getInfo_response"]["user"]
puts friends_locations.map {|f| [f["name"],f["current_location"]].inspect}.length
located_friends = friends_locations.reject {|f| f["current_location"]["xsi:nil"]}
locationless_friends = friends_locations.select {|f| f["current_location"]["xsi:nil"]}
friends_by_location = located_friends.inject({}) do |addresses,friend|
address = if friend["current_location"]["name"]
friend["current_location"]["name"]
else
location = friend["current_location"]
"#{location["city"]}, #{location["state"]} #{location["country"]}"
end
addresses[address] ||= {"people"=>[]}
addresses[address]["people"] << friend
addresses
end
friends_by_location.each do |location,attrs|
result = GoogleGeocoder.geocode location
if result["status"] != "OK"
p result
next if result["status"] =="ZERO_RESULTS"
end
attrs["latlng"] = result["results"].first["geometry"]["location"]
end
File.open "output.json","w" do |f|
f.puts "awesome(#{
{:located => friends_by_location.map{|loc,attrs| attrs["name"]=loc;attrs},
:missing => locationless_friends}.to_json})"
end
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html> <head>
<title>Where they at?</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
function awesome(data){
super_data = data
}
map = null;
geocoder = null;
function initialize() {
var latlng = new google.maps.LatLng(40,40);
var myOptions = {
zoom: 2,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
geocoder = new google.maps.Geocoder();
map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
awesome2(super_data);
}
function awesome2(data){
data.located.map(function(location){
console.log(location);
var marker = new google.maps.Marker({
map: map,
position: new google.maps.LatLng(location.latlng.lat, location.latlng.lng),
title: location.people.map(function(p){return p.name;}).join(", ")
});
})
}
</script>
<script type="text/javascript" src="output.json"></script>
</head>
<body onload="initialize()">
<h1>Where They At?</h1>
<div id="map_canvas" style="width:100%; height:100%"></div>
<!-- hhmts start --> Last modified: Sat May 8 21:51:46 MDT 2010 <!-- hhmts end -->
</body> </html>
@benatkin

benatkin commented May 9, 2010

Copy link
Copy Markdown

I really like how you use HTTParty! I was thinking it was great for something like the twitter gem, but for quick, custom use of an API it would make more sense to use something like restclient. Now I see that HTTParty is great for both uses.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment