social_graph 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/social_graph.rb +156 -0
- metadata +46 -0
data/lib/social_graph.rb
ADDED
@@ -0,0 +1,156 @@
|
|
1
|
+
# Google Social Graph API interface, v 0.0.1
|
2
|
+
# Author: Chris Heald, Tag Team Interactive ( cheald at gmail / cheald at tagteaminteractive.com )
|
3
|
+
# License: LGPL 2.1
|
4
|
+
# http://creativecommons.org/licenses/LGPL/2.1/
|
5
|
+
|
6
|
+
require 'cgi'
|
7
|
+
require 'rubygems'
|
8
|
+
require 'json'
|
9
|
+
require 'net/http'
|
10
|
+
|
11
|
+
# === About
|
12
|
+
# Provides an interface to Google's SocialGraph API ( http://code.google.com/apis/socialgraph/ )
|
13
|
+
|
14
|
+
|
15
|
+
module Google
|
16
|
+
# SocialGraph query object. Create with the identity URL(s) to check and pull information from.
|
17
|
+
# Note that you shouldn't mix multiple users' identities in one SocialGraph object!
|
18
|
+
# === Searching the social graph
|
19
|
+
#
|
20
|
+
# require 'social_graph'
|
21
|
+
# # We'll use Digg's Kevin Rose as our victim because hey, he's a pretty popular guy!
|
22
|
+
#
|
23
|
+
# result = Google::SocialGraph.new "http://www.digg.com/users/kevinrose/"
|
24
|
+
#
|
25
|
+
# # Get all people that Kevin refers to as a, acquaintance, contact, friend, or someone he's met
|
26
|
+
# graph.refers_to_as([:acquaintance, :contact, :friend, :met]).inspect
|
27
|
+
#
|
28
|
+
# # Now let's get a list of all the people that say they've met Kevin
|
29
|
+
# graph.referred_to_as(:met).inspect
|
30
|
+
#
|
31
|
+
# # Anyone can say anything they want, but we want to know where else on the web we KNOW is Kevin.
|
32
|
+
# # This looks for mutual links between sites to each other, with the given XFN identity.
|
33
|
+
# graph.mutual_reference_as(:me).inspect
|
34
|
+
#
|
35
|
+
# # We can also find people that Kevin is a friend with (and people who are friends with Kevin back!)
|
36
|
+
# graph.mutual_reference_as([:friend, :met, :contact]).inspect
|
37
|
+
|
38
|
+
class SocialGraph
|
39
|
+
HOST = "socialgraph.apis.google.com"
|
40
|
+
OPT_KEYS = [:edi, :edo, :fme, :pretty, :callback, :sgn]
|
41
|
+
DEFAULT_OPTIONS = {:edi => true, :edo => true, :fme => true, :pretty => 0, :callback => nil, :sgn => nil}
|
42
|
+
|
43
|
+
# Create a new graph query. Pass a URL or array of URLs to use as the query entry point.
|
44
|
+
# For example, if you wanted to find relationships to Kevin Rose, you might use his Digg profile at http://www.digg.com/users/kevinrose/
|
45
|
+
#
|
46
|
+
# 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
|
+
def initialize(urls)
|
48
|
+
@urls = urls
|
49
|
+
end
|
50
|
+
|
51
|
+
# Perform the query and return the graph info from Google as a Ruby hash. You can see the full option reference at http://code.google.com/apis/socialgraph/docs/api.html
|
52
|
+
#
|
53
|
+
# See DEFAULT_OPTIONS for the options passed in by default.
|
54
|
+
#
|
55
|
+
# 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
|
+
def query(options = nil)
|
57
|
+
return @result unless @result.nil?
|
58
|
+
|
59
|
+
options ||= DEFAULT_OPTIONS.clone
|
60
|
+
|
61
|
+
path = "/lookup?"
|
62
|
+
q = {}
|
63
|
+
if @urls.is_a? String then
|
64
|
+
q[:q] = CGI::escape(@urls)
|
65
|
+
elsif @urls.is_a? Array then
|
66
|
+
q[:q] = @urls.collect {|u| CGI::escape u}.join(",")
|
67
|
+
end
|
68
|
+
options.delete_if {|k,v| !OPT_KEYS.include? k.to_sym}
|
69
|
+
q = q.merge(options)
|
70
|
+
query = q.collect {|k, v| "#{k}=#{v.to_s}" }.join("&")
|
71
|
+
path = "/lookup?#{query}"
|
72
|
+
debug_msg("Fetching #{HOST}#{path}")
|
73
|
+
body = Net::HTTP.get(HOST, path)
|
74
|
+
@result = JSON.parse(body)
|
75
|
+
return @result
|
76
|
+
end
|
77
|
+
|
78
|
+
# 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:
|
79
|
+
#
|
80
|
+
# query.attributes(:rss)
|
81
|
+
def attributes(attr, options = {})
|
82
|
+
result = query
|
83
|
+
results = {}
|
84
|
+
(result["nodes"] || []).each do |node|
|
85
|
+
node_addr = node[0]
|
86
|
+
attr_hash = node[1]["attributes"]
|
87
|
+
if attr_hash.nil?
|
88
|
+
results[node_addr] = nil if options[:include_nil]
|
89
|
+
next
|
90
|
+
end
|
91
|
+
results[node_addr] = attr_hash[attr.to_s] unless attr_hash[attr.to_s].nil?
|
92
|
+
end
|
93
|
+
return results
|
94
|
+
end
|
95
|
+
|
96
|
+
# Turn debugging or off. When debugging is on, will write to RAILS_DEFAULT_LOGGER (if running under rails), or STDOUT otherwise.
|
97
|
+
def debug=(b)
|
98
|
+
@debugging = b
|
99
|
+
end
|
100
|
+
|
101
|
+
# Get a list of all people this person refers to with the given XFN tag. A list of valid tags can be found at http://gmpg.org/xfn/join
|
102
|
+
#
|
103
|
+
# 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.
|
104
|
+
#
|
105
|
+
# Another example would be refers_to_as(:friend) to get all people this person calls "friend".
|
106
|
+
def refers_to_as(relationship)
|
107
|
+
relationship_map(relationship, "nodes_referenced")
|
108
|
+
end
|
109
|
+
|
110
|
+
# 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 http://gmpg.org/xfn/join
|
111
|
+
#
|
112
|
+
# 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.
|
113
|
+
# 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.
|
114
|
+
#
|
115
|
+
# Another example would be referred_to_as(:friend) to get all people that call this person "friend".
|
116
|
+
def referred_to_as(relationship)
|
117
|
+
relationship_map(relationship, "nodes_referenced_by")
|
118
|
+
end
|
119
|
+
|
120
|
+
# Combines +refers_to_as+ and +referred_to_as+ to get verified relationships.
|
121
|
+
#
|
122
|
+
# For example mutual_reference_as(:me) would return all nodes where any two nodes reference each other with a "me" XFN tag.
|
123
|
+
def mutual_reference_as(relationship)
|
124
|
+
(relationship_map(relationship, "nodes_referenced") & relationship_map(relationship, "nodes_referenced_by")).uniq
|
125
|
+
end
|
126
|
+
|
127
|
+
private
|
128
|
+
|
129
|
+
def relationship_map(relationships, key)
|
130
|
+
relationships = relationships.to_s if relationships.is_a?(Symbol)
|
131
|
+
relationships = relationships.collect {|r| r.to_s} if relationships.is_a?(Array)
|
132
|
+
raise "SocialGraph#Relationships accepts only a string or array of strings." if ![Array,String].include? relationships.class
|
133
|
+
|
134
|
+
nodes = []
|
135
|
+
query["nodes"].each do |node|
|
136
|
+
node[1][key].each do |ref|
|
137
|
+
if relationships.is_a?(Array) and !(ref[1]["types"] & relationships).empty?
|
138
|
+
nodes.push ref[0]
|
139
|
+
elsif relationships.is_a?(String) and ref[1]["types"].include? relationships
|
140
|
+
nodes.push ref[0]
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
return nodes.uniq
|
145
|
+
end
|
146
|
+
|
147
|
+
def debug_msg(s)
|
148
|
+
return unless @debugging
|
149
|
+
if defined? RAILS_DEFAULT_LOGGER
|
150
|
+
RAILS_DEFAULT_LOGGER.debug s
|
151
|
+
else
|
152
|
+
puts s
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.9.4
|
3
|
+
specification_version: 1
|
4
|
+
name: social_graph
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.0.1
|
7
|
+
date: 2008-02-01 00:00:00 -07:00
|
8
|
+
summary: A library for accessing the Google SocialGraph API
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: cheald@gmail.com
|
12
|
+
homepage:
|
13
|
+
rubyforge_project:
|
14
|
+
description: A library for accessing the Google SocialGraph API (http://code.google.com/apis/socialgraph/)
|
15
|
+
autorequire: rake
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: true
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
post_install_message:
|
29
|
+
authors:
|
30
|
+
- Chris Heald
|
31
|
+
files:
|
32
|
+
- lib/social_graph.rb
|
33
|
+
test_files: []
|
34
|
+
|
35
|
+
rdoc_options: []
|
36
|
+
|
37
|
+
extra_rdoc_files: []
|
38
|
+
|
39
|
+
executables: []
|
40
|
+
|
41
|
+
extensions: []
|
42
|
+
|
43
|
+
requirements: []
|
44
|
+
|
45
|
+
dependencies: []
|
46
|
+
|