buddy 0.6.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
4
+ buddy.tmproj
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in buddy.gemspec
4
+ gemspec
data/README.md CHANGED
@@ -1,14 +1,52 @@
1
1
  # Buddy
2
2
 
3
- Buddy is a lightweight Facebook library for Ruby.
3
+ Buddy is a lightweight Facebook library for Rails 3.
4
4
 
5
5
  ## Overview
6
6
 
7
7
  * Support for iFrame Apps
8
- * Uses signed_request parameters for authentication
8
+ * Uses signed\_request parameters for authentication
9
9
  * Supports the Graph API and old REST API
10
10
  * Fully compatible with **Rails 3** and **Ruby 1.9.2**
11
11
 
12
+ ## Install
13
+
14
+ Add the following to your Gemfile
15
+
16
+ gem 'buddy'
17
+
18
+ or for the latest Git version
19
+
20
+ gem 'buddy', :git => 'git://github.com/buddybrand/buddy.git'
21
+
22
+ Generate and edit config/buddy.yml
23
+
24
+ rails generate buddy_config
25
+
26
+ ## Usage
27
+
28
+ Buddy adds all parameters contained in _signed\_request_ to the params[:fb] hash. See [Canvas Authentication](http://developers.facebook.com/docs/authentication/canvas)
29
+
30
+ To require the user to install the application add this to your ApplicationController:
31
+
32
+ before_filter :ensure_application_is_installed
33
+
34
+ If the user authorized the application you can do API calls like this:
35
+
36
+ # Publish to Graph API:
37
+ facebook_session.post('/me/feed', :message => 'Hi there!')
38
+
39
+ # Reading from Graph API:
40
+ facebook_session.get('/me')
41
+
42
+ # REST API
43
+ facebook_session.call('users.getInfo')
44
+
45
+
46
+ ## Todo
47
+
48
+ * Add tests
49
+ * Add some examples
12
50
 
13
51
  ## License
14
52
 
@@ -22,5 +60,7 @@ Copyright (c) 2010:
22
60
  Some concepts and code adapted from [Facebooker](http://github.com/mmangino/facebooker)
23
61
 
24
62
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
63
+
25
64
  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
65
+
26
66
  THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
data/buddy.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "buddy/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "buddy"
7
+ s.version = Buddy::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Ole Riesenberg"]
10
+ s.email = ["or@buddybrand.com"]
11
+ s.homepage = "http://rubygems.org/gems/buddy"
12
+ s.summary = %q{buddybrand's facebook library}
13
+ s.description = %q{buddybrand's facebook library}
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ Gem::Specification.new do |s|
21
+ s.add_dependency(%q<mini_fb>, [">= 0.2.2"])
22
+ s.add_dependency(%q<yajl-ruby>, [">= 0"])
23
+ s.add_dependency(%q<httparty>, [">= 0"])
24
+ end
25
+ end
@@ -1,41 +1,10 @@
1
- module Rack
2
- # This Rack middleware checks the signature of Facebook params, and
3
- # converts them to Ruby objects when appropiate. Also, it converts
4
- # the request method from the Facebook POST to the original HTTP
5
- # method used by the client.
6
- #
7
- # If the signature is wrong, it returns a "400 Invalid Facebook Signature".
8
- #
9
- # Optionally, it can take a block that receives the Rack environment
10
- # and returns a value that evaluates to true when we want the middleware to
11
- # be executed for the specific request.
12
- #
13
- # == Usage
14
- #
15
- # In your config.ru:
16
- #
17
- # require 'rack/facebook'
18
- # use Rack::Facebook, "my_facebook_secret_key"
19
- #
20
- # Using a block condition:
21
- #
22
- # use Rack::Facebook, "my_facebook_secret_key" do |env|
23
- # env['REQUEST_URI'] =~ /^\/facebook_only/
24
- # end
25
- #
26
-
27
- module Facebook
28
- class RemoteIp
29
- def initialize(app)
30
- @app = app
31
- end
32
-
33
- def call(env)
34
- env['REMOTE_ADDR'] = env['X-FB-USER-REMOTE-ADDR'] if env['X-FB-USER-REMOTE-ADDR']
35
- @app.call(env)
36
- end
37
- end
1
+ module Buddy
2
+ module Middleware
38
3
 
4
+ # This Rack middleware checks the signed_request, and
5
+ # appends the payload to Rails params.
6
+ #
7
+ # If the signature is wrong, it returns a "400 Invalid Facebook Signature".
39
8
  class ParamsParser
40
9
  def initialize(app, &condition)
41
10
  @app = app
@@ -47,6 +16,7 @@ module Rack
47
16
 
48
17
  request = Rack::Request.new(env)
49
18
 
19
+ request.params["signed_request"] = env['HTTP_X_SIGNED_REQUEST'] if env['HTTP_X_SIGNED_REQUEST'] && !request.params["signed_request"]
50
20
  signed_request = request.params["signed_request"]
51
21
  if signed_request
52
22
  signature, signed_params = signed_request.split('.')
@@ -56,7 +26,7 @@ module Rack
56
26
  end
57
27
 
58
28
  signed_params = Yajl::Parser.new.parse(base64_url_decode(signed_params))
59
- request.params[:fb] = signed_params
29
+ request.params[:fb] = signed_params
60
30
  end
61
31
 
62
32
  @app.call(env)
@@ -1,7 +1,6 @@
1
1
  module Buddy
2
2
  module Rails
3
3
  module Controller
4
- include Buddy::Rails::BackwardsCompatibleParamChecks
5
4
  include Buddy::Rails::UrlHelper
6
5
 
7
6
  def self.included(controller)
@@ -25,6 +24,10 @@ module Buddy
25
24
  @facebook_session
26
25
  end
27
26
 
27
+ def request_comes_from_facebook?
28
+ !params[:signed_request].blank? # It already has been verified in middleware, so we can trust it here.
29
+ end
30
+
28
31
  def application_is_installed?
29
32
  !params[:fb][:oauth_token].blank?
30
33
  end
@@ -2,30 +2,10 @@ module ::ActionController
2
2
  class Base
3
3
  def self.inherited_with_buddy(subclass)
4
4
  inherited_without_buddy(subclass)
5
- #if subclass.to_s == "ApplicationController"
6
- subclass.send(:include, Buddy::Rails::Controller)
7
- #subclass.helper Buddy::Rails::Helpers
8
- #end
5
+ subclass.send(:include, Buddy::Rails::Controller)
9
6
  end
10
7
  class << self
11
8
  alias_method_chain :inherited, :buddy
12
9
  end
13
10
  end
14
11
  end
15
-
16
-
17
- # When making get requests, Facebook sends fb_sig parameters both in the query string
18
- # and also in the post body. We want to ignore the query string ones because they are one
19
- # request out of date
20
- # We only do thise when there are POST parameters so that IFrame linkage still works
21
- #class ActionController::Request
22
- # def query_parameters_with_buddy
23
- # if request_parameters.blank?
24
- # query_parameters_without_buddy
25
- # else
26
- # (query_parameters_without_buddy||{}).reject {|key,value| key.to_s =~ /^fb_sig/}
27
- # end
28
- # end
29
-
30
- # alias_method_chain :query_parameters, :buddy
31
- #end
@@ -2,19 +2,16 @@ module Buddy
2
2
  module Rails
3
3
  module UrlHelper
4
4
  def url_for(options = {})
5
- options ||= {}
6
- opts = case options
7
- when Hash
8
- options.merge!({ :only_path => true }) #unless options[:canvas] == false
9
- else
10
- options
11
- end
12
- url = super(opts)
5
+ only_path = options[:only_path] || false
6
+ options.merge!({ :only_path => true }) if options.is_a?(Hash) && options[:canvas] != false
7
+
8
+ url = super(options)
13
9
 
14
- if url.include?("http://") #todo: make it better
10
+ if url[0..6] == 'http://'
15
11
  url
16
12
  elsif options.is_a?(Hash) && options[:canvas] == false
17
- "#{Buddy.current_config["callback_url"]}#{url}"
13
+ base = only_path ? '' : Buddy.current_config["callback_url"]
14
+ base + url
18
15
  else
19
16
  "http://apps.facebook.com/#{Buddy.current_config["canvas_page_name"]}#{url}"
20
17
  end
data/lib/buddy/railtie.rb CHANGED
@@ -5,7 +5,7 @@ require 'rails'
5
5
  module Buddy
6
6
  class Railtie < ::Rails::Railtie
7
7
  initializer "buddy.configure_rails_initialization" do |app|
8
- app.config.middleware.insert_before(ActionDispatch::ParamsParser, ::Rack::Facebook::ParamsParser)
8
+ app.config.middleware.insert_before(ActionDispatch::ParamsParser, Buddy::Middleware::ParamsParser)
9
9
  ActionView::Helpers::UrlHelper.send(:include, Buddy::Rails::UrlHelper)
10
10
  Buddy.logger = ::Rails.logger
11
11
  end
@@ -0,0 +1,3 @@
1
+ module Buddy
2
+ VERSION = "1.0.0"
3
+ end
data/lib/buddy.rb CHANGED
@@ -6,7 +6,7 @@ require 'yajl'
6
6
  require 'mini_fb'
7
7
  require 'httparty'
8
8
 
9
- require 'rack/facebook'
9
+ require 'buddy/middleware'
10
10
 
11
11
  require 'buddy/user'
12
12
  require 'buddy/session'
@@ -62,7 +62,6 @@ BUDDY = Buddy.load_configuration(buddy_config)
62
62
  Buddy.logger = Rails.logger
63
63
  Buddy.caller = Buddy::Service::Caller.new
64
64
 
65
- require 'buddy/rails/backwards_compatible_param_checks'
66
65
  require 'buddy/rails/url_helper'
67
66
  require 'buddy/rails/controller'
68
67
  require 'buddy/rails/controller_extensions'
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Generates the buddy config
3
+
4
+ Example:
5
+ rails generate buddy_config
6
+
7
+ This will create:
8
+ config/buddy.yml
@@ -0,0 +1,7 @@
1
+ class BuddyConfigGenerator < Rails::Generators::Base
2
+ source_root File.expand_path('../templates', __FILE__)
3
+
4
+ def create_config
5
+ template 'buddy.yml', File.join('config', "buddy.yml")
6
+ end
7
+ end
@@ -0,0 +1,23 @@
1
+ development:
2
+ default:
3
+ app_id: # your facebook app id
4
+ secret: # your app secret
5
+ canvas_page_name: # your canvas name
6
+ callback_url: # your app callback url, e.g. http://example.com
7
+ perms: # a comma separated list of required extended permissions, e.g. user_photos,email
8
+
9
+ test:
10
+ default:
11
+ app_id:
12
+ secret:
13
+ canvas_page_name:
14
+ callback_url:
15
+ perms:
16
+
17
+ production:
18
+ default:
19
+ app_id:
20
+ secret:
21
+ canvas_page_name:
22
+ callback_url:
23
+ perms:
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: buddy
3
3
  version: !ruby/object:Gem::Version
4
- hash: 3
5
- prerelease: false
4
+ hash: 23
5
+ prerelease:
6
6
  segments:
7
+ - 1
7
8
  - 0
8
- - 6
9
- - 2
10
- version: 0.6.2
9
+ - 0
10
+ version: 1.0.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ole Riesenberg
@@ -15,64 +15,27 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-11-17 00:00:00 +01:00
18
+ date: 2011-02-17 00:00:00 +01:00
19
19
  default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
22
- name: mini_fb
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- hash: 19
30
- segments:
31
- - 0
32
- - 2
33
- - 2
34
- version: 0.2.2
35
- type: :runtime
36
- version_requirements: *id001
37
- - !ruby/object:Gem::Dependency
38
- name: yajl-ruby
39
- prerelease: false
40
- requirement: &id002 !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
43
- - - ">="
44
- - !ruby/object:Gem::Version
45
- hash: 3
46
- segments:
47
- - 0
48
- version: "0"
49
- type: :runtime
50
- version_requirements: *id002
51
- - !ruby/object:Gem::Dependency
52
- name: httparty
53
- prerelease: false
54
- requirement: &id003 !ruby/object:Gem::Requirement
55
- none: false
56
- requirements:
57
- - - ">="
58
- - !ruby/object:Gem::Version
59
- hash: 3
60
- segments:
61
- - 0
62
- version: "0"
63
- type: :runtime
64
- version_requirements: *id003
20
+ dependencies: []
21
+
65
22
  description: buddybrand's facebook library
66
- email: labs@buddybrand.de
23
+ email:
24
+ - or@buddybrand.com
67
25
  executables: []
68
26
 
69
27
  extensions: []
70
28
 
71
- extra_rdoc_files:
72
- - README.md
29
+ extra_rdoc_files: []
30
+
73
31
  files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - README.md
35
+ - Rakefile
36
+ - buddy.gemspec
74
37
  - lib/buddy.rb
75
- - lib/buddy/rails/backwards_compatible_param_checks.rb
38
+ - lib/buddy/middleware.rb
76
39
  - lib/buddy/rails/controller.rb
77
40
  - lib/buddy/rails/controller_extensions.rb
78
41
  - lib/buddy/rails/url_helper.rb
@@ -80,11 +43,13 @@ files:
80
43
  - lib/buddy/service.rb
81
44
  - lib/buddy/session.rb
82
45
  - lib/buddy/user.rb
83
- - lib/rack/facebook.rb
46
+ - lib/buddy/version.rb
47
+ - lib/generators/buddy_config/USAGE
48
+ - lib/generators/buddy_config/buddy_config_generator.rb
49
+ - lib/generators/buddy_config/templates/buddy.yml
84
50
  - test/test_buddy.rb
85
- - README.md
86
51
  has_rdoc: true
87
- homepage: http://buddybrand.de
52
+ homepage: http://rubygems.org/gems/buddy
88
53
  licenses: []
89
54
 
90
55
  post_install_message:
@@ -113,7 +78,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
113
78
  requirements: []
114
79
 
115
80
  rubyforge_project:
116
- rubygems_version: 1.3.7
81
+ rubygems_version: 1.4.1
117
82
  signing_key:
118
83
  specification_version: 3
119
84
  summary: buddybrand's facebook library
@@ -1,33 +0,0 @@
1
- module Buddy
2
- module Rails
3
- module BackwardsCompatibleParamChecks
4
- def one_or_true( value )
5
- case value
6
- when String then
7
- value == "1"
8
- when Numeric then
9
- value.to_f == 1.0
10
- when TrueClass then
11
- true
12
- else
13
- false
14
- end
15
- end
16
-
17
- def zero_or_false( value )
18
- case value
19
- when String then
20
- value.empty? || value == "0"
21
- when Numeric then
22
- value.to_f == 0.0
23
- when FalseClass then
24
- true
25
- when NilClass then
26
- true
27
- else
28
- false
29
- end
30
- end
31
- end
32
- end
33
- end