fb_rails 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +16 -0
- data/lib/fb_rails.rb +1 -7
- data/lib/fb_rails/connect.rb +1 -16
- data/lib/fb_rails/cookie.rb +32 -0
- data/lib/fb_rails/integration/action_view.rb +7 -2
- data/test/cookie_test.rb +21 -0
- data/test/integration/action_view_test.rb +13 -0
- data/test/test_helper.rb +6 -1
- metadata +6 -3
data/README.rdoc
CHANGED
@@ -18,6 +18,16 @@ or
|
|
18
18
|
config.facebook.app_id = 'my_app_id'
|
19
19
|
config.facebook.secret = 'my_app_secret'
|
20
20
|
|
21
|
+
Getting the user logged in is out of the scope of this gem. That's what
|
22
|
+
FBML and the Facebook Javascript API are for. Here's an example:
|
23
|
+
|
24
|
+
<div id="fb-root"></div>
|
25
|
+
<script src="http://connect.facebook.net/en_US/all.js"></script>
|
26
|
+
<script>
|
27
|
+
FB.init({appId: '<%= fb.app_id ?>', status: true, cookie: true, xfbml: true});
|
28
|
+
</script>
|
29
|
+
<%= fbml 'login-button' %>
|
30
|
+
|
21
31
|
|
22
32
|
== API
|
23
33
|
|
@@ -36,6 +46,10 @@ Make a request to the Facebook Graph:
|
|
36
46
|
<% result = fb.graph.get '/me' %>
|
37
47
|
my name is <%= result['first_name'] %>
|
38
48
|
|
49
|
+
Retrieve your Facebook application app_id or secret:
|
50
|
+
fb.app_id
|
51
|
+
fb.secret
|
52
|
+
|
39
53
|
|
40
54
|
== Persisting the Facebook User:
|
41
55
|
|
@@ -46,6 +60,7 @@ This user is accessed with 'fb.user':
|
|
46
60
|
|
47
61
|
For this to work, FbRails assumes there is a model called 'User' with a column 'fb_uid'.
|
48
62
|
If your user model name is different than 'User', it can be configured:
|
63
|
+
|
49
64
|
config.facebook.user_class_name = 'Author'
|
50
65
|
|
51
66
|
If the fb_uid for the current user is not in the database, fb.user is an unsaved instance.
|
@@ -68,3 +83,4 @@ connections. This is done with FbRails::Mock:
|
|
68
83
|
mock.add 'me', 'first_name' => 'Matt', 'last_name' => 'Higgins'
|
69
84
|
end
|
70
85
|
|
86
|
+
Now, a call to 'fb.graph.get 'me' returns the above response.
|
data/lib/fb_rails.rb
CHANGED
@@ -2,18 +2,12 @@ module FbRails
|
|
2
2
|
extend ActiveSupport::Autoload
|
3
3
|
|
4
4
|
autoload :Config
|
5
|
+
autoload :Cookie
|
5
6
|
autoload :Connect
|
6
7
|
autoload :Graph
|
7
8
|
autoload :Fb
|
8
9
|
autoload :Mock
|
9
10
|
autoload :Integration
|
10
|
-
|
11
|
-
def self.cookie(uid = 42, access_token = 'abc')
|
12
|
-
param_string = "uid=#{uid}&access_token=#{access_token}"
|
13
|
-
payload = "access_token=#{access_token}uid=#{uid}"
|
14
|
-
sig = Digest::MD5.hexdigest("#{payload}#{FbRails::Config.secret}")
|
15
|
-
{FbRails::Connect.cookie_name => "\"#{param_string}&sig=#{sig}\""}
|
16
|
-
end
|
17
11
|
end
|
18
12
|
|
19
13
|
require 'fb_rails/railtie'
|
data/lib/fb_rails/connect.rb
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
require 'digest/md5'
|
2
|
-
|
3
1
|
module FbRails
|
4
2
|
class Connect
|
5
3
|
class << self
|
@@ -16,20 +14,7 @@ module FbRails
|
|
16
14
|
end
|
17
15
|
|
18
16
|
def cookie
|
19
|
-
|
20
|
-
|
21
|
-
hash = Rack::Utils::parse_query(cookie_string.gsub(/^\"|\"$/, ''))
|
22
|
-
sorted_pairs = hash.sort
|
23
|
-
|
24
|
-
payload = ''
|
25
|
-
sorted_pairs.each do |key, value|
|
26
|
-
payload += "#{key}=#{value}" if key != 'sig'
|
27
|
-
end
|
28
|
-
|
29
|
-
md5 = Digest::MD5.hexdigest("#{payload}#{FbRails::Config.secret}")
|
30
|
-
if md5 == hash['sig']
|
31
|
-
hash
|
32
|
-
end
|
17
|
+
FbRails::Cookie.decode(cookies[self.class.cookie_name], FbRails::Config.secret)
|
33
18
|
end
|
34
19
|
memoize :cookie
|
35
20
|
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'digest/md5'
|
2
|
+
|
3
|
+
module FbRails
|
4
|
+
class Cookie
|
5
|
+
class << self
|
6
|
+
def decode(cookie_string, secret)
|
7
|
+
return if cookie_string.blank?
|
8
|
+
cookie_string = Rack::Utils.unescape(cookie_string)
|
9
|
+
|
10
|
+
hash = Rack::Utils.parse_query(cookie_string.gsub(/^\"|\"$/, ''))
|
11
|
+
|
12
|
+
payload = ''
|
13
|
+
hash.sort.each do |key, value|
|
14
|
+
payload += "#{key}=#{value}" if key != 'sig'
|
15
|
+
end
|
16
|
+
|
17
|
+
md5 = Digest::MD5.hexdigest("#{payload}#{secret}")
|
18
|
+
if md5 == hash['sig']
|
19
|
+
hash
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def encode(attributes, secret)
|
24
|
+
sorted_pairs = attributes.sort.map { |key, value| "#{key}=#{value}" }
|
25
|
+
param_string = sorted_pairs.join('&')
|
26
|
+
payload = sorted_pairs.join('')
|
27
|
+
sig = Digest::MD5.hexdigest("#{payload}#{secret}")
|
28
|
+
"\"#{param_string}&sig=#{sig}\""
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -1,8 +1,13 @@
|
|
1
1
|
module FbRails
|
2
2
|
module Integration
|
3
3
|
module ActionView
|
4
|
-
def fbml(tag, options =
|
5
|
-
|
4
|
+
def fbml(tag, content = '', options = {})
|
5
|
+
if content.is_a?(Hash)
|
6
|
+
options = content
|
7
|
+
content = ''
|
8
|
+
end
|
9
|
+
|
10
|
+
content_tag("fb:#{tag}", content, options)
|
6
11
|
end
|
7
12
|
end
|
8
13
|
end
|
data/test/cookie_test.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class FbRails::CookieTest < ActiveSupport::TestCase
|
4
|
+
test 'decode' do
|
5
|
+
assert_equal(
|
6
|
+
{'a' => '1', 'b' => '2', 'sig' => '25a7f94742b4134695b80f78d4804e35'},
|
7
|
+
FbRails::Cookie.decode("\"a=1&b=2&sig=25a7f94742b4134695b80f78d4804e35\"", 'sekrit')
|
8
|
+
)
|
9
|
+
end
|
10
|
+
|
11
|
+
test 'decode invalid' do
|
12
|
+
assert_nil FbRails::Cookie.decode("\"a=1&b=2&sig=invalid\"", 'sekrit')
|
13
|
+
end
|
14
|
+
|
15
|
+
test 'encode' do
|
16
|
+
assert_equal(
|
17
|
+
"\"a=1&b=2&sig=25a7f94742b4134695b80f78d4804e35\"",
|
18
|
+
FbRails::Cookie.encode({:a => 1, :b => 2}, 'sekrit')
|
19
|
+
)
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class FbRails::Integration::ActionViewTest < ActiveSupport::TestCase
|
4
|
+
include ActionView::Helpers::TagHelper
|
5
|
+
include FbRails::Integration::ActionView
|
6
|
+
|
7
|
+
test 'fbml' do
|
8
|
+
assert_equal '<fb:foo></fb:foo>', fbml('foo')
|
9
|
+
assert_equal '<fb:foo>bar</fb:foo>', fbml('foo', 'bar')
|
10
|
+
assert_equal '<fb:foo faz="baz"></fb:foo>', fbml('foo', 'faz' => 'baz')
|
11
|
+
assert_equal '<fb:foo faz="baz">bar</fb:foo>', fbml('foo', 'bar', 'faz' => 'baz')
|
12
|
+
end
|
13
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -10,4 +10,9 @@ require 'sample/test_controller'
|
|
10
10
|
FbRails::Config.app_id = 'foo'
|
11
11
|
FbRails::Config.secret = 'bar'
|
12
12
|
|
13
|
-
|
13
|
+
FbRails.class_eval do
|
14
|
+
def self.cookie(uid = 42, access_token = 'abc')
|
15
|
+
value = FbRails::Cookie.encode({:uid => uid, :access_token => access_token}, FbRails::Config.secret)
|
16
|
+
{FbRails::Connect.cookie_name => value}
|
17
|
+
end
|
18
|
+
end
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
7
|
+
- 3
|
8
8
|
- 0
|
9
|
-
version: 0.
|
9
|
+
version: 0.3.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Matthew Higgins
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-11-04 00:00:00 -07:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -46,6 +46,7 @@ files:
|
|
46
46
|
- README.rdoc
|
47
47
|
- lib/fb_rails/config.rb
|
48
48
|
- lib/fb_rails/connect.rb
|
49
|
+
- lib/fb_rails/cookie.rb
|
49
50
|
- lib/fb_rails/fb.rb
|
50
51
|
- lib/fb_rails/graph.rb
|
51
52
|
- lib/fb_rails/integration/action_controller.rb
|
@@ -57,9 +58,11 @@ files:
|
|
57
58
|
- lib/fb_rails.rb
|
58
59
|
- test/config_test.rb
|
59
60
|
- test/connect_test.rb
|
61
|
+
- test/cookie_test.rb
|
60
62
|
- test/fb_test.rb
|
61
63
|
- test/graph_test.rb
|
62
64
|
- test/helpers/action_controller_test.rb
|
65
|
+
- test/integration/action_view_test.rb
|
63
66
|
- test/log_subscriber_test.rb
|
64
67
|
- test/mock_test.rb
|
65
68
|
- test/sample/test_controller.rb
|