rest-graph 1.1.1 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +33 -0
- data/README.rdoc +50 -11
- data/TODO +3 -15
- data/example/rails/README +4 -0
- data/example/rails/Rakefile +10 -0
- data/example/rails/app/controllers/application_controller.rb +20 -0
- data/example/rails/app/views/rest-graph.erb +16 -0
- data/example/rails/config/boot.rb +110 -0
- data/example/rails/config/environment.rb +43 -0
- data/example/rails/config/environments/development.rb +17 -0
- data/example/rails/config/environments/production.rb +28 -0
- data/example/rails/config/environments/test.rb +28 -0
- data/example/rails/config/initializers/cookie_verification_secret.rb +7 -0
- data/example/rails/config/initializers/new_rails_defaults.rb +21 -0
- data/example/rails/config/initializers/session_store.rb +15 -0
- data/example/rails/config/rest-graph.yaml +5 -0
- data/example/rails/config/routes.rb +43 -0
- data/example/rails/script/console +3 -0
- data/example/rails/script/server +3 -0
- data/init.rb +1 -0
- data/lib/rest-graph.rb +75 -35
- data/lib/rest-graph/load_config.rb +33 -34
- data/lib/rest-graph/rails_controller.rb +100 -0
- data/lib/rest-graph/version.rb +1 -3
- data/rest-graph.gemspec +22 -20
- data/test/common.rb +7 -1
- data/test/test_default.rb +31 -0
- data/test/test_fql.rb +60 -0
- data/test/test_handler.rb +56 -0
- data/test/test_load_config.rb +4 -0
- data/test/test_parse.rb +70 -0
- data/test/test_rest-graph.rb +4 -80
- metadata +40 -10
data/CHANGES
CHANGED
@@ -1,5 +1,38 @@
|
|
1
1
|
= rest-graph changes history
|
2
2
|
|
3
|
+
== rest-graph 1.2.0 -- ?
|
4
|
+
* Add RestGraph#parse_json!
|
5
|
+
* Add RailsController to help you integrate into Rails.
|
6
|
+
* Simplify arguments checking and require dependency.
|
7
|
+
* Now if there's no secret in RestGraph, sig check would always fail.
|
8
|
+
* Now there's a Rails example.
|
9
|
+
http://github.com/cardinalblue/rest-graph/tree/master/example
|
10
|
+
|
11
|
+
* Add error_handler option. Default behavior is raising ::RestGraph::Error.
|
12
|
+
You may want to pass your private controller method to do redirection.
|
13
|
+
Extracted from README:
|
14
|
+
# You may want to do redirect instead of raising exception, for example,
|
15
|
+
# in a Rails application, you might have this private controller method:
|
16
|
+
def redirect_to_authorize error = nil
|
17
|
+
redirect_to @rg.authorize_url(:redirect_uri => request.url)
|
18
|
+
end
|
19
|
+
|
20
|
+
# and you'll use that private method to do error handling:
|
21
|
+
def setup_rest_graph
|
22
|
+
@rg = RestGraph.new(:error_handler => method(:redirect_to_authorize))
|
23
|
+
end
|
24
|
+
|
25
|
+
* Add log_handler option. Default behavior is do nothing.
|
26
|
+
You may want to do this in Rails:
|
27
|
+
RestGraph.new(:log_hanlder => lambda{ |duration, url|
|
28
|
+
Rails.logger.debug("RestGraph " \
|
29
|
+
"spent #{duration} " \
|
30
|
+
"requesting #{url}")
|
31
|
+
})
|
32
|
+
|
33
|
+
* Add RestGraph#fql_multi to do FQL multiquery. Thanks Ethan Czahor
|
34
|
+
Usage: rg.fql_multi(:query1 => 'SELECT ...', :query2 => 'SELECT ...')
|
35
|
+
|
3
36
|
== rest-graph 1.1.1 -- 2010-05-21
|
4
37
|
* Add oauth realted utilites -- authorize_url and authorize!
|
5
38
|
* Fixed a bug that in Ruby 1.8.7-, nil =~ /regexp/ equals to false.
|
data/README.rdoc
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
= rest-graph 1.
|
1
|
+
= rest-graph 1.2.0
|
2
2
|
by Cardinal Blue ( http://cardinalblue.com )
|
3
3
|
|
4
4
|
== LINKS:
|
@@ -9,19 +9,22 @@ by Cardinal Blue ( http://cardinalblue.com )
|
|
9
9
|
|
10
10
|
== DESCRIPTION:
|
11
11
|
|
12
|
-
super simple
|
12
|
+
A super simple Facebook Open Graph API client
|
13
13
|
|
14
14
|
== FEATURES:
|
15
15
|
|
16
|
-
*
|
17
|
-
*
|
18
|
-
*
|
16
|
+
* Simple Graph API call
|
17
|
+
* Simple FQL call
|
18
|
+
* Utility to extract access_token and check sig in cookies
|
19
19
|
|
20
20
|
== SYNOPSIS:
|
21
21
|
|
22
|
+
# If you feel SYNOPSIS is so hard to understand, please read
|
23
|
+
# {examples}[http://github.com/cardinalblue/rest-graph/tree/master/example].
|
24
|
+
|
22
25
|
require 'rest-graph'
|
23
26
|
|
24
|
-
#
|
27
|
+
# Every option is optional.
|
25
28
|
rg = RestGraph.new(:access_token => '...',
|
26
29
|
:graph_server => 'https://graph.facebook.com/',
|
27
30
|
:fql_server => 'https://api.facebook.com/',
|
@@ -29,8 +32,36 @@ by Cardinal Blue ( http://cardinalblue.com )
|
|
29
32
|
:lang => 'en-us', # this affect search
|
30
33
|
:auto_decode => true, # decode by json
|
31
34
|
:app_id => '123',
|
32
|
-
:secret => '1829'
|
35
|
+
:secret => '1829',
|
36
|
+
|
37
|
+
# This handler callback is only called if auto_decode is set to true,
|
38
|
+
# otherwise, it's ignored.
|
39
|
+
:error_handler =>
|
40
|
+
lambda{ |hash| raise ::RestGraph::Error.new(hash) },
|
41
|
+
|
42
|
+
# You may want to do this in Rails to do debug logging:
|
43
|
+
:log_handler =>
|
44
|
+
lambda{ |duration, url|
|
45
|
+
Rails.logger.debug("RestGraph " \
|
46
|
+
"spent #{duration} " \
|
47
|
+
"requesting #{url}")
|
48
|
+
})
|
49
|
+
|
50
|
+
# You may want to do redirect instead of raising exception, for example,
|
51
|
+
# in a Rails application, you might have this private controller method:
|
52
|
+
def redirect_to_authorize error = nil
|
53
|
+
redirect_to @rg.authorize_url(:redirect_uri => request.url)
|
54
|
+
end
|
55
|
+
|
56
|
+
# and you'll use that private method to do error handling:
|
57
|
+
def setup_rest_graph
|
58
|
+
@rg = RestGraph.new(:error_handler => method(:redirect_to_authorize))
|
59
|
+
end
|
33
60
|
|
61
|
+
# This way, you don't have to check if the token is expired or not.
|
62
|
+
# If the token is expired, it will automatically do authorization again.
|
63
|
+
|
64
|
+
# Other simple API call:
|
34
65
|
rg.get('me') # GET https://graph.facebook.com/me?access_token=...
|
35
66
|
rg.get('4/likes') # GET https://graph.facebook.com/4/likes?access_token=...
|
36
67
|
|
@@ -40,20 +71,28 @@ by Cardinal Blue ( http://cardinalblue.com )
|
|
40
71
|
# GET https://graph.facebook.com/me?metadata=1&access_token=...
|
41
72
|
rg.get('me', :metadata => '1')
|
42
73
|
|
43
|
-
# POST https://graph.facebook.com/feed
|
44
|
-
rg.post('feed
|
74
|
+
# POST https://graph.facebook.com/me/feed?message=bread%21&access_token=...
|
75
|
+
rg.post('me/feed', :message => 'bread!')
|
45
76
|
|
46
77
|
# for fully blown cookies hash
|
47
78
|
rg = RestGraph.new(:app_id => '123', :secret => '1829')
|
48
79
|
rg.parse_cookies!(cookies) # auto save access_token if sig checked
|
49
80
|
rg.data['uid'] # => facebook uid
|
50
81
|
|
51
|
-
#
|
82
|
+
# FQL query, same as:
|
52
83
|
# GET https://api.facebook.com/method/fql.query?query=
|
53
84
|
# SELECT+name+FROM+page+WHERE+page_id%3D%22123%22&
|
54
85
|
# format=json&access_token=...
|
55
86
|
rg.fql('SELECT name FROM page WHERE page_id="123"')
|
56
87
|
|
88
|
+
# FQL multiquery, same as:
|
89
|
+
# GET https://api.facebook.com/method/fql.multiquery?query=
|
90
|
+
# %7BSELECT+name+FROM+page+WHERE+page_id%3D%22123%22&%2C
|
91
|
+
# SELECT+name+FROM+page+WHERE+page_id%3D%22456%22&%7D
|
92
|
+
# format=json&access_token=...
|
93
|
+
rg.fql_multi(:q1 => 'SELECT name FROM page WHERE page_id="123"',
|
94
|
+
:q2 => 'SELECT name FROM page WHERE page_id="456"')
|
95
|
+
|
57
96
|
# default setting:
|
58
97
|
class RestGraph
|
59
98
|
def self.default_app_id
|
@@ -90,7 +129,7 @@ by Cardinal Blue ( http://cardinalblue.com )
|
|
90
129
|
|
91
130
|
== REQUIREMENTS:
|
92
131
|
|
93
|
-
*
|
132
|
+
* Tested with MRI 1.8.7 and 1.9.1
|
94
133
|
* gem install rest-client
|
95
134
|
* gem install json (optional)
|
96
135
|
* gem install json_pure (optional)
|
data/TODO
CHANGED
@@ -1,17 +1,5 @@
|
|
1
1
|
= rest-graph todo list
|
2
2
|
|
3
|
-
= 1.
|
4
|
-
*
|
5
|
-
|
6
|
-
class AuthError < RuntimeError; end
|
7
|
-
|
8
|
-
rg = RestGraph.new(:scope => 'publish_stream,read_friendlists',
|
9
|
-
:error_callback => lambda{ |json| raise AuthError } )
|
10
|
-
|
11
|
-
begin
|
12
|
-
rg.get('me')
|
13
|
-
rescue AuthError
|
14
|
-
# ...
|
15
|
-
end
|
16
|
-
|
17
|
-
rg.error_callback = lambda{ |json| redirect request.url }
|
3
|
+
= 1.2
|
4
|
+
* more docs?
|
5
|
+
* more examples?
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# Add your own tasks in files placed in lib/tasks ending in .rake,
|
2
|
+
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
|
3
|
+
|
4
|
+
require(File.join(File.dirname(__FILE__), 'config', 'boot'))
|
5
|
+
|
6
|
+
require 'rake'
|
7
|
+
require 'rake/testtask'
|
8
|
+
require 'rake/rdoctask'
|
9
|
+
|
10
|
+
require 'tasks/rails'
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# Filters added to this controller apply to all controllers in the application.
|
2
|
+
# Likewise, all the methods added will be available for all controllers.
|
3
|
+
|
4
|
+
class ApplicationController < ActionController::Base
|
5
|
+
helper :all # include all helpers, all the time
|
6
|
+
protect_from_forgery # See ActionController::RequestForgeryProtection for details
|
7
|
+
|
8
|
+
# Scrub sensitive parameters from your log
|
9
|
+
# filter_parameter_logging :password
|
10
|
+
|
11
|
+
include RestGraph::RailsController
|
12
|
+
before_filter :setup_iframe
|
13
|
+
before_filter :setup_rest_graph
|
14
|
+
|
15
|
+
def index
|
16
|
+
me = @rg.get('me')
|
17
|
+
return unless me
|
18
|
+
render :text => me.to_json
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
2
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
3
|
+
<html>
|
4
|
+
<head>
|
5
|
+
<script type="text/javascript">
|
6
|
+
window.top.location.href = '<%= @authorize_url %>'
|
7
|
+
</script>
|
8
|
+
<noscript>
|
9
|
+
<meta http-equiv="refresh" content="0;url=<%= h @authorize_url %>" />
|
10
|
+
<meta http-equiv="window-target" content="_top" />
|
11
|
+
</noscript>
|
12
|
+
</head>
|
13
|
+
<body>
|
14
|
+
<div>Please <a href="<%= h @authorize_url %>" target="_top">authorize</a> if this page is not automatically redirected.</div>
|
15
|
+
</body>
|
16
|
+
</html>
|
@@ -0,0 +1,110 @@
|
|
1
|
+
# Don't change this file!
|
2
|
+
# Configure your app in config/environment.rb and config/environments/*.rb
|
3
|
+
|
4
|
+
RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)
|
5
|
+
|
6
|
+
module Rails
|
7
|
+
class << self
|
8
|
+
def boot!
|
9
|
+
unless booted?
|
10
|
+
preinitialize
|
11
|
+
pick_boot.run
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def booted?
|
16
|
+
defined? Rails::Initializer
|
17
|
+
end
|
18
|
+
|
19
|
+
def pick_boot
|
20
|
+
(vendor_rails? ? VendorBoot : GemBoot).new
|
21
|
+
end
|
22
|
+
|
23
|
+
def vendor_rails?
|
24
|
+
File.exist?("#{RAILS_ROOT}/vendor/rails")
|
25
|
+
end
|
26
|
+
|
27
|
+
def preinitialize
|
28
|
+
load(preinitializer_path) if File.exist?(preinitializer_path)
|
29
|
+
end
|
30
|
+
|
31
|
+
def preinitializer_path
|
32
|
+
"#{RAILS_ROOT}/config/preinitializer.rb"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
class Boot
|
37
|
+
def run
|
38
|
+
load_initializer
|
39
|
+
Rails::Initializer.run(:set_load_path)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
class VendorBoot < Boot
|
44
|
+
def load_initializer
|
45
|
+
require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
|
46
|
+
Rails::Initializer.run(:install_gem_spec_stubs)
|
47
|
+
Rails::GemDependency.add_frozen_gem_path
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
class GemBoot < Boot
|
52
|
+
def load_initializer
|
53
|
+
self.class.load_rubygems
|
54
|
+
load_rails_gem
|
55
|
+
require 'initializer'
|
56
|
+
end
|
57
|
+
|
58
|
+
def load_rails_gem
|
59
|
+
if version = self.class.gem_version
|
60
|
+
gem 'rails', version
|
61
|
+
else
|
62
|
+
gem 'rails'
|
63
|
+
end
|
64
|
+
rescue Gem::LoadError => load_error
|
65
|
+
$stderr.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)
|
66
|
+
exit 1
|
67
|
+
end
|
68
|
+
|
69
|
+
class << self
|
70
|
+
def rubygems_version
|
71
|
+
Gem::RubyGemsVersion rescue nil
|
72
|
+
end
|
73
|
+
|
74
|
+
def gem_version
|
75
|
+
if defined? RAILS_GEM_VERSION
|
76
|
+
RAILS_GEM_VERSION
|
77
|
+
elsif ENV.include?('RAILS_GEM_VERSION')
|
78
|
+
ENV['RAILS_GEM_VERSION']
|
79
|
+
else
|
80
|
+
parse_gem_version(read_environment_rb)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def load_rubygems
|
85
|
+
min_version = '1.3.2'
|
86
|
+
require 'rubygems'
|
87
|
+
unless rubygems_version >= min_version
|
88
|
+
$stderr.puts %Q(Rails requires RubyGems >= #{min_version} (you have #{rubygems_version}). Please `gem update --system` and try again.)
|
89
|
+
exit 1
|
90
|
+
end
|
91
|
+
|
92
|
+
rescue LoadError
|
93
|
+
$stderr.puts %Q(Rails requires RubyGems >= #{min_version}. Please install RubyGems and try again: http://rubygems.rubyforge.org)
|
94
|
+
exit 1
|
95
|
+
end
|
96
|
+
|
97
|
+
def parse_gem_version(text)
|
98
|
+
$1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/
|
99
|
+
end
|
100
|
+
|
101
|
+
private
|
102
|
+
def read_environment_rb
|
103
|
+
File.read("#{RAILS_ROOT}/config/environment.rb")
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
# All that for this:
|
110
|
+
Rails.boot!
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# Be sure to restart your server when you modify this file
|
2
|
+
|
3
|
+
# Specifies gem version of Rails to use when vendor/rails is not present
|
4
|
+
RAILS_GEM_VERSION = '2.3.8' unless defined? RAILS_GEM_VERSION
|
5
|
+
|
6
|
+
# Bootstrap the Rails environment, frameworks, and default configuration
|
7
|
+
require File.join(File.dirname(__FILE__), 'boot')
|
8
|
+
|
9
|
+
Rails::Initializer.run do |config|
|
10
|
+
# Settings in config/environments/* take precedence over those specified here.
|
11
|
+
# Application configuration should go into files in config/initializers
|
12
|
+
# -- all .rb files in that directory are automatically loaded.
|
13
|
+
|
14
|
+
# Add additional load paths for your own custom dirs
|
15
|
+
# config.load_paths += %W( #{RAILS_ROOT}/extras )
|
16
|
+
|
17
|
+
# Specify gems that this application depends on and have them installed with rake gems:install
|
18
|
+
# config.gem "bj"
|
19
|
+
# config.gem "hpricot", :version => '0.6', :source => "http://code.whytheluckystiff.net"
|
20
|
+
# config.gem "sqlite3-ruby", :lib => "sqlite3"
|
21
|
+
# config.gem "aws-s3", :lib => "aws/s3"
|
22
|
+
config.gem 'rest-graph'
|
23
|
+
config.gem 'rest-graph', :lib => 'rest-graph/auto_load'
|
24
|
+
|
25
|
+
# Only load the plugins named here, in the order given (default is alphabetical).
|
26
|
+
# :all can be used as a placeholder for all plugins not explicitly named
|
27
|
+
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]
|
28
|
+
|
29
|
+
# Skip frameworks you're not going to use. To use Rails without a database,
|
30
|
+
# you must remove the Active Record framework.
|
31
|
+
config.frameworks -= [ :active_record, :active_resource, :action_mailer ]
|
32
|
+
|
33
|
+
# Activate observers that should always be running
|
34
|
+
# config.active_record.observers = :cacher, :garbage_collector, :forum_observer
|
35
|
+
|
36
|
+
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
|
37
|
+
# Run "rake -D time" for a list of tasks for finding time zone names.
|
38
|
+
config.time_zone = 'UTC'
|
39
|
+
|
40
|
+
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
|
41
|
+
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}')]
|
42
|
+
# config.i18n.default_locale = :de
|
43
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# Settings specified here will take precedence over those in config/environment.rb
|
2
|
+
|
3
|
+
# In the development environment your application's code is reloaded on
|
4
|
+
# every request. This slows down response time but is perfect for development
|
5
|
+
# since you don't have to restart the webserver when you make code changes.
|
6
|
+
config.cache_classes = false
|
7
|
+
|
8
|
+
# Log error messages when you accidentally call methods on nil.
|
9
|
+
config.whiny_nils = true
|
10
|
+
|
11
|
+
# Show full error reports and disable caching
|
12
|
+
config.action_controller.consider_all_requests_local = true
|
13
|
+
config.action_view.debug_rjs = true
|
14
|
+
config.action_controller.perform_caching = false
|
15
|
+
|
16
|
+
# Don't care if the mailer can't send
|
17
|
+
config.action_mailer.raise_delivery_errors = false
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# Settings specified here will take precedence over those in config/environment.rb
|
2
|
+
|
3
|
+
# The production environment is meant for finished, "live" apps.
|
4
|
+
# Code is not reloaded between requests
|
5
|
+
config.cache_classes = true
|
6
|
+
|
7
|
+
# Full error reports are disabled and caching is turned on
|
8
|
+
config.action_controller.consider_all_requests_local = false
|
9
|
+
config.action_controller.perform_caching = true
|
10
|
+
config.action_view.cache_template_loading = true
|
11
|
+
|
12
|
+
# See everything in the log (default is :info)
|
13
|
+
# config.log_level = :debug
|
14
|
+
|
15
|
+
# Use a different logger for distributed setups
|
16
|
+
# config.logger = SyslogLogger.new
|
17
|
+
|
18
|
+
# Use a different cache store in production
|
19
|
+
# config.cache_store = :mem_cache_store
|
20
|
+
|
21
|
+
# Enable serving of images, stylesheets, and javascripts from an asset server
|
22
|
+
# config.action_controller.asset_host = "http://assets.example.com"
|
23
|
+
|
24
|
+
# Disable delivery errors, bad email addresses will be ignored
|
25
|
+
# config.action_mailer.raise_delivery_errors = false
|
26
|
+
|
27
|
+
# Enable threaded mode
|
28
|
+
# config.threadsafe!
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# Settings specified here will take precedence over those in config/environment.rb
|
2
|
+
|
3
|
+
# The test environment is used exclusively to run your application's
|
4
|
+
# test suite. You never need to work with it otherwise. Remember that
|
5
|
+
# your test database is "scratch space" for the test suite and is wiped
|
6
|
+
# and recreated between test runs. Don't rely on the data there!
|
7
|
+
config.cache_classes = true
|
8
|
+
|
9
|
+
# Log error messages when you accidentally call methods on nil.
|
10
|
+
config.whiny_nils = true
|
11
|
+
|
12
|
+
# Show full error reports and disable caching
|
13
|
+
config.action_controller.consider_all_requests_local = true
|
14
|
+
config.action_controller.perform_caching = false
|
15
|
+
config.action_view.cache_template_loading = true
|
16
|
+
|
17
|
+
# Disable request forgery protection in test environment
|
18
|
+
config.action_controller.allow_forgery_protection = false
|
19
|
+
|
20
|
+
# Tell Action Mailer not to deliver emails to the real world.
|
21
|
+
# The :test delivery method accumulates sent emails in the
|
22
|
+
# ActionMailer::Base.deliveries array.
|
23
|
+
config.action_mailer.delivery_method = :test
|
24
|
+
|
25
|
+
# Use SQL instead of Active Record's schema dumper when creating the test database.
|
26
|
+
# This is necessary if your schema can't be completely dumped by the schema dumper,
|
27
|
+
# like if you have constraints or database-specific column types
|
28
|
+
# config.active_record.schema_format = :sql
|