Class: Google::SocialGraph
- Inherits:
-
Object
- Object
- Google::SocialGraph
- Defined in:
- lib/social_graph.rb
Overview
SocialGraph query object. Create with the identity URL(s) to check and pull information from. Note that you shouldn’t mix multiple users’ identities in one SocialGraph object!
Searching the social graph
require 'social_graph'
# We'll use Digg's Kevin Rose as our victim because hey, he's a pretty popular guy!
result = Google::SocialGraph.new "http://www.digg.com/users/kevinrose/"
# Get all people that Kevin refers to as a, acquaintance, contact, friend, or someone he's met
graph.refers_to_as([:acquaintance, :contact, :friend, :met]).inspect
# Now let's get a list of all the people that say they've met Kevin
graph.referred_to_as(:met).inspect
# Anyone can say anything they want, but we want to know where else on the web we KNOW is Kevin.
# This looks for mutual links between sites to each other, with the given XFN identity.
graph.mutual_reference_as(:me).inspect
# We can also find people that Kevin is a friend with (and people who are friends with Kevin back!)
graph.mutual_reference_as([:friend, :met, :contact]).inspect
Constant Summary collapse
- HOST =
"socialgraph.apis.google.com"
- OPT_KEYS =
[:edi, :edo, :fme, :pretty, :callback, :sgn]
- DEFAULT_OPTIONS =
{:edi => true, :edo => true, :fme => true, :pretty => 0, :callback => nil, :sgn => nil}
Instance Method Summary collapse
-
#attributes(attr, options = {}) ⇒ Object
Get a list of attributes from your search.
-
#debug=(b) ⇒ Object
Turn debugging or off.
-
#initialize(urls) ⇒ SocialGraph
constructor
Create a new graph query.
-
#mutual_reference_as(relationship) ⇒ Object
Combines
refers_to_as
andreferred_to_as
to get verified relationships. -
#query(options = nil) ⇒ Object
Perform the query and return the graph info from Google as a Ruby hash.
-
#referred_to_as(relationship) ⇒ Object
Get a list of all people that refer to this person with the given XFN tag.
-
#refers_to_as(relationship) ⇒ Object
Get a list of all people this person refers to with the given XFN tag.
Constructor Details
#initialize(urls) ⇒ SocialGraph
Create a new graph query. Pass a URL or array of URLs to use as the query entry point. For example, if you wanted to find relationships to Kevin Rose, you might use his Digg profile at www.digg.com/users/kevinrose/
Note that any page that supports XFN will work. Pages that don’t support XFN won’t return any useful info, but they won’t cause problems either.
47 48 49 |
# File 'lib/social_graph.rb', line 47 def initialize(urls) @urls = urls end |
Instance Method Details
#attributes(attr, options = {}) ⇒ Object
Get a list of attributes from your search. For example, if you wanted a list of RSS feeds attached to this user, then you would get:
query.attributes(:rss)
81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/social_graph.rb', line 81 def attributes(attr, = {}) result = query results = {} (result["nodes"] || []).each do |node| node_addr = node[0] attr_hash = node[1]["attributes"] if attr_hash.nil? results[node_addr] = nil if [:include_nil] next end results[node_addr] = attr_hash[attr.to_s] unless attr_hash[attr.to_s].nil? end return results end |
#debug=(b) ⇒ Object
Turn debugging or off. When debugging is on, will write to RAILS_DEFAULT_LOGGER (if running under rails), or STDOUT otherwise.
97 98 99 |
# File 'lib/social_graph.rb', line 97 def debug=(b) @debugging = b end |
#mutual_reference_as(relationship) ⇒ Object
Combines refers_to_as
and referred_to_as
to get verified relationships.
For example mutual_reference_as(:me) would return all nodes where any two nodes reference each other with a “me” XFN tag.
123 124 125 |
# File 'lib/social_graph.rb', line 123 def mutual_reference_as(relationship) (relationship_map(relationship, "nodes_referenced") & relationship_map(relationship, "nodes_referenced_by")).uniq end |
#query(options = nil) ⇒ Object
Perform the query and return the graph info from Google as a Ruby hash. You can see the full option reference at code.google.com/apis/socialgraph/docs/api.html
See DEFAULT_OPTIONS for the options passed in by default.
Unless you want the raw info, you don’t need to use this directly. Use refers_to_as
, referred_to_as
, mutual_reference_as
, and attributes
instead.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/social_graph.rb', line 56 def query( = nil) return @result unless @result.nil? ||= DEFAULT_OPTIONS.clone path = "/lookup?" q = {} if @urls.is_a? String then q[:q] = CGI::escape(@urls) elsif @urls.is_a? Array then q[:q] = @urls.collect {|u| CGI::escape u}.join(",") end .delete_if {|k,v| !OPT_KEYS.include? k.to_sym} q = q.merge() query = q.collect {|k, v| "#{k}=#{v.to_s}" }.join("&") path = "/lookup?#{query}" debug_msg("Fetching #{HOST}#{path}") body = Net::HTTP.get(HOST, path) @result = JSON.parse(body) return @result end |
#referred_to_as(relationship) ⇒ Object
Get a list of all people that refer to this person with the given XFN tag. A list of valid tags can be found at gmpg.org/xfn/join
For example, referred_to_as(:me) will get all nodes that refer to this person as him/herself. This can help you discover a user’s identities elsewhere. However, note that anyone can refer to this person with an XFN “me” tag, so don’t trust it unless there’s a mutual connection.
Another example would be referred_to_as(:friend) to get all people that call this person “friend”.
116 117 118 |
# File 'lib/social_graph.rb', line 116 def referred_to_as(relationship) relationship_map(relationship, "nodes_referenced_by") end |
#refers_to_as(relationship) ⇒ Object
Get a list of all people this person refers to with the given XFN tag. A list of valid tags can be found at gmpg.org/xfn/join
For example, refers_to_as(:me) will get all nodes this person identifies as him/herself. This can help you discover a user’s identities elsewhere.
Another example would be refers_to_as(:friend) to get all people this person calls “friend”.
106 107 108 |
# File 'lib/social_graph.rb', line 106 def refers_to_as(relationship) relationship_map(relationship, "nodes_referenced") end |