facebook_party 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,60 @@
1
+ = FacebookParty
2
+
3
+ I needed a simple wrapper for the Facebook REST API. HTTParty to the rescue!
4
+
5
+ <tt>method_missing</tt> magic is used to handle any api method available now and in the future.
6
+
7
+ == Installation
8
+
9
+ sudo gem install facebook_party
10
+
11
+ == Usage
12
+
13
+ fb = FacebookParty(API_KEY, SECRET)
14
+ # call this method: http://wiki.developers.facebook.com/index.php/Auth.createToken
15
+ response = fb.auth.createToken
16
+
17
+ == Feedback
18
+
19
+ I'd love to hear from you if you have suggestions for improvement, bug fixes,
20
+ or whatever. Email me at tim@timmorgan.org or fork the project and send a
21
+ pull request.
22
+
23
+ To run the tests, do this:
24
+
25
+ FACEBOOK_API_KEY="YOURKEY" \
26
+ FACEBOOK_API_SECRET="YOURSECRET" \
27
+ ruby -rrubygems test/facebook_party_test.rb
28
+
29
+ == Simple Auth
30
+
31
+ By far the easiest way I've found to do simple server-side auth
32
+ is to use the <tt>auth_url</tt> method of FacebookParty.
33
+ Send the user to the returned url. Once authenticated and authorized,
34
+ the user will be redirected to the specified <tt>next_url</tt>, and
35
+ details including the <tt>session_key</tt> will be included in the
36
+ url parameters.
37
+
38
+ == License
39
+
40
+ The MIT License
41
+
42
+ Copyright (c) 2010 Tim Morgan
43
+
44
+ Permission is hereby granted, free of charge, to any person obtaining a copy
45
+ of this software and associated documentation files (the "Software"), to deal
46
+ in the Software without restriction, including without limitation the rights
47
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
48
+ copies of the Software, and to permit persons to whom the Software is
49
+ furnished to do so, subject to the following conditions:
50
+
51
+ The above copyright notice and this permission notice shall be included in
52
+ all copies or substantial portions of the Software.
53
+
54
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
55
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
56
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
57
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
58
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
59
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
60
+ THE SOFTWARE.
@@ -0,0 +1,74 @@
1
+ require 'httparty'
2
+ require 'md5'
3
+ require 'uri'
4
+
5
+ class FacebookParty
6
+
7
+ ENDPOINT = 'http://api.facebook.com/restserver.php'
8
+ API_VERSION = '1.0'
9
+ LOGIN_URL = 'http://www.facebook.com/login.php'
10
+ LOGIN_SUCCESS_URL = 'http://www.facebook.com/connect/login_success.html'
11
+ LOGIN_CANCEL_URL = 'http://www.facebook.com/connect/login_failure.html'
12
+
13
+ include HTTParty
14
+ format :xml
15
+
16
+ attr_accessor :token
17
+
18
+ # Create a new FacebookParty instance.
19
+ # Pass in the api key and api secret for your app assigned by Facebook.
20
+ # The <tt>method</tt> argument is used internally -- don't pass anything into this argument.
21
+ def initialize(api_key, secret, method=nil)
22
+ @api_key = api_key
23
+ @secret = secret
24
+ @method = method
25
+ end
26
+
27
+ # Call any api method directly on the instance, e.g.:
28
+ # fb = FacebookParty.new(API_KEY, SECRET)
29
+ # response = fb.auth.createToken
30
+ def method_missing(method_name, args={}, test=nil)
31
+ if @method
32
+ args = self.class.stringify_hash_keys(args)
33
+ args.merge!('method' => @method + '.' + method_name.to_s, 'api_key' => @api_key, 'v' => API_VERSION)
34
+ args.merge!('sig' => generate_sig(args))
35
+ self.class.post(ENDPOINT, :body => args)
36
+ else
37
+ self.class.new(@api_key, @secret, method_name.to_s)
38
+ end
39
+ end
40
+
41
+ # Build an authentication/authorization url for Facebook Connect apps
42
+ # (typically used for Desktop apps)
43
+ # See http://wiki.developers.facebook.com/index.php/Authorization_and_Authentication_for_Desktop_Applications
44
+ # <tt>req_params</tt> one or more options from list http://wiki.developers.facebook.com/index.php/Extended_permissions
45
+ def auth_url(options={})
46
+ options = {
47
+ 'api_key' => @api_key,
48
+ 'fbconnect' => true,
49
+ 'v' => API_VERSION,
50
+ 'connect_display' => 'page',
51
+ 'return_session' => true,
52
+ 'session_key_only' => true,
53
+ 'next' => LOGIN_SUCCESS_URL,
54
+ 'cancel_url' => LOGIN_CANCEL_URL,
55
+ 'req_perms' => ['read_stream', 'publish_stream', 'offline_access'],
56
+ }.merge(self.class.stringify_hash_keys(options))
57
+ options['req_perms'] = options['req_perms'].join(',')
58
+ args = options.map { |k, v| "#{k}=#{URI.escape(v.to_s)}" }.join('&')
59
+ "#{LOGIN_URL}?#{args}"
60
+ end
61
+
62
+ # Generate the MD5 signature from passed in hash of arguments
63
+ def generate_sig(args)
64
+ MD5.hexdigest(args.sort.map { |k, v| "#{k}=#{v}" }.join + @secret)
65
+ end
66
+
67
+ def self.stringify_hash_keys(hash) #:nodoc:
68
+ hash.inject({}) do |options, (key, value)|
69
+ options[key.to_s] = value
70
+ options
71
+ end
72
+ end
73
+
74
+ end
@@ -0,0 +1,25 @@
1
+ require File.dirname(__FILE__) + '/../lib/facebook_party'
2
+ require 'test/unit'
3
+ require 'md5'
4
+
5
+ class FacebookPartyTest < Test::Unit::TestCase
6
+
7
+ def setup
8
+ if ENV['FACEBOOK_API_KEY'] and ENV['FACEBOOK_API_SECRET']
9
+ @fb = FacebookParty.new(ENV['FACEBOOK_API_KEY'], ENV['FACEBOOK_API_SECRET'])
10
+ else
11
+ puts 'Must set FACEBOOK_API_KEY and FACEBOOK_API_SECRET env variables before running.'
12
+ exit(1)
13
+ end
14
+ end
15
+
16
+ def test_sig
17
+ assert_equal MD5.hexdigest("arg1=val1arg2=val2#{ENV['FACEBOOK_API_SECRET']}"), @fb.generate_sig({'arg1' => 'val1', 'arg2' => 'val2'})
18
+ end
19
+
20
+ def test_create_token
21
+ response = @fb.auth.createToken
22
+ assert response['auth_createToken_response']
23
+ end
24
+
25
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: facebook_party
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tim Morgan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-26 00:00:00 -06:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: httparty
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description:
26
+ email: tim@timmorgan.org
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files: []
32
+
33
+ files:
34
+ - README.rdoc
35
+ - lib/facebook_party.rb
36
+ - test/facebook_party_test.rb
37
+ has_rdoc: true
38
+ homepage: http://github.com/seven1m/facebook_party
39
+ licenses: []
40
+
41
+ post_install_message:
42
+ rdoc_options: []
43
+
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ requirements: []
59
+
60
+ rubyforge_project:
61
+ rubygems_version: 1.3.5
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: Lightweight wrapper for Facebook API using HTTParty
65
+ test_files: []
66
+