facilbook 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in facilbook.gemspec
4
+ gemspec
@@ -0,0 +1,36 @@
1
+ ## FacilBook - Simple Facebook Navigation
2
+
3
+ ### Getting Started
4
+
5
+ 0- Prerequisite: You need a facebook app. Have your API Key, Application
6
+ Secret, and Application ID handy.
7
+
8
+ 1- Add `gem 'facilbook'` to your Gemfile and run `bundle install`.
9
+
10
+ 2- Create `config/facilbook_config.yml` with the appropriate environments.
11
+
12
+ production:
13
+ app_id: <your application id>
14
+ app_secret: <your application secret>
15
+ facebook_app_url: http://apps.facebook.com/<your app name>
16
+
17
+ 3- Create `config/initializers/load_facilbook_config.rb` and place the following in it
18
+
19
+ raw_config = File.read("#{Rails.root}/config/facilbook_config.yml")
20
+ FACILBOOK_CONFIG = YAML.load(raw_config)[Rails.env].symbolize_keys
21
+
22
+ 4- Add the following line to your `app/controllers/application_controller.rb`
23
+
24
+ (add it right after the line class `ApplicationController < ActionController::Base` so as to add the Facebooker2 instance methods to the Application controller)
25
+
26
+ include Facilbook::ApplicationControllerMethods
27
+
28
+ 5- Add the following line to your `app/helpers/application_helper.rb`
29
+
30
+ (add it right after the line `module ApplicationHelper`
31
+
32
+ include Facilbook::ApplicationHelperMethods
33
+
34
+ ### Usage
35
+
36
+ TBD
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "facilbook/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "facilbook"
7
+ s.version = Facilbook::VERSION
8
+ s.authors = ["Eric Berry", "Brian Johnson"]
9
+ s.email = ["cavneb@gmail.com", "bjohnson@1on1.com"]
10
+ s.homepage = ""
11
+ s.summary = "Simple (facil) Facebook helpers that allow for navigation"
12
+ s.description = "Simple (facil) Facebook helpers that allow for navigation"
13
+
14
+ s.rubyforge_project = "facilbook"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ # s.add_development_dependency "rspec"
23
+ # s.add_runtime_dependency "rest-client"
24
+ s.add_development_dependency "rails"
25
+ s.add_development_dependency "rake"
26
+ end
@@ -0,0 +1,162 @@
1
+ require "facilbook/version"
2
+
3
+ module Facilbook
4
+
5
+ module Rack
6
+ class Facebook
7
+
8
+ def initialize(app)
9
+ @app = app
10
+ end
11
+
12
+ def call(env)
13
+ request = ::Rack::Request.new(env)
14
+
15
+ if request.POST['signed_request']
16
+ env["REQUEST_METHOD"] = 'GET'
17
+ end
18
+
19
+ return @app.call(env)
20
+ end
21
+ end
22
+ end
23
+
24
+ module ApplicationHelperMethods
25
+
26
+ def facebook_image_tag(uid, options = {})
27
+ options.symbolize_keys!
28
+ image_url = "https://graph.facebook.com/#{uid}/picture"
29
+ if options[:type] && (['square','small','large'].include? options[:type])
30
+ image_url << "?type=#{type}"
31
+ end
32
+
33
+ src = image_url
34
+ options[:src] = src
35
+
36
+ unless src =~ /^cid:/
37
+ options[:alt] = options.fetch(:alt){ File.basename(src, '.*').capitalize }
38
+ end
39
+ if size = options.delete(:size)
40
+ options[:width], options[:height] = size.split("x") if size =~ %{^\d+x\d+$}
41
+ end
42
+ tag("img", options)
43
+ end
44
+
45
+ def link_to_facebook(*args, &block)
46
+ html_options = args.extract_options!.symbolize_keys
47
+ # logger.debug "OPTIONS: #{html_options.inspect}"
48
+ name = args[0]
49
+ path = block_given? ? update_page(&block) : args[1] || ''
50
+ tag_class = html_options[:class] ||= ''
51
+ onclick = "window.top.location='#{FACILBOOK_CONFIG[:facebook_app_url]}#{path}'; return false;"
52
+ content_tag(:a, name, html_options.merge(:href => '#', :onclick => onclick, :class => tag_class))
53
+ end
54
+
55
+ def url_for_facebook(path)
56
+ "#{FACILBOOK_CONFIG[:facebook_app_url]}#{path}"
57
+ end
58
+
59
+ end
60
+
61
+ module ApplicationControllerMethods
62
+
63
+ def self.included(klass)
64
+ klass.class_eval do
65
+ before_filter :parse_signed_request
66
+ helper_method :current_user
67
+ end
68
+ end
69
+
70
+ protected
71
+
72
+ # Note - *args is ignored
73
+ def redirect_to_facebook(target_path, *args)
74
+ raise ActionControllerError.new("Cannot redirect to nil!") if target_path.nil?
75
+ raise ActionControllerError.new("Must use path and not url as target") if target_path =~ /^http/
76
+ url = "#{FACILBOOK_CONFIG[:facebook_app_url]}#{target_path}"
77
+ render :text => "<html><body><script type=\"text/javascript\">window.top.location='#{url}';</script></body></html>"
78
+ end
79
+
80
+ def parse_signed_request
81
+ # IE6-7 fix
82
+ response.headers['P3P'] = 'CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"'
83
+
84
+ # Get the signed request from either the request or the cookie
85
+ signed_request = get_signed_cookie(params[:signed_request])
86
+
87
+ if signed_request
88
+ encoded_sig, payload = signed_request.split(".")
89
+ sig = base64_url_decode(encoded_sig)
90
+
91
+ # Ensure that the request is valid from Facebook
92
+ if OpenSSL::HMAC.digest("sha256", FACILBOOK_CONFIG[:app_secret], payload) == sig
93
+
94
+ decoded = base64_url_decode(payload)
95
+ @signed_request = JSON.parse(decoded)
96
+ @signed_request['signed_request_provided'] = params[:signed_request].present?
97
+ puts "Current Signed Request: #{@signed_request.to_yaml}"
98
+
99
+ else
100
+ logger.info "SIGNED REQUEST AND SIGNATURE DO NOT MATCH!!!"
101
+ @signed_request = nil
102
+ end
103
+
104
+ else
105
+ logger.info "NO SIGNED REQUEST!!! THIS IS NOT VIA FACEBOOK!!!"
106
+ end
107
+ end
108
+
109
+ def current_user
110
+ begin
111
+ if session[:user_id]
112
+ @current_user = User.find(session[:user_id])
113
+ elsif @signed_request && @signed_request['user_id']
114
+ @current_user = User.where(:provider => 'facebook').where(:uid => @signed_request['user_id']).first
115
+ else
116
+ @current_user = nil
117
+ end
118
+ rescue ActiveRecord::RecordNotFound => ex
119
+ @current_user = nil
120
+ end
121
+ end
122
+
123
+ # This takes the signed request and places it into a cookie and returns the
124
+ # signed_request from the cookie. If the signed_request is not provided, it
125
+ # will still return the signed_request placed in the cookie previously.
126
+ # @param [String] signed_request Signed request from Facebook
127
+ # @returns [String] Signed request from cookie
128
+ #
129
+ def get_signed_cookie(signed_request)
130
+ if signed_request
131
+ cookies["sr_#{FACILBOOK_CONFIG[:app_id]}"] = params[:signed_request]
132
+ else
133
+ signed_request = cookies["sr_#{FACILBOOK_CONFIG[:app_id]}"]
134
+ end
135
+ return signed_request
136
+ end
137
+
138
+ # Facebook uses a special base64 encryption. This decodes it.
139
+ # @param [String] encoded_sig Encoded string
140
+ # @returns [String] Decoded string
141
+ #
142
+ def base64_url_decode(str)
143
+ if !str.blank?
144
+ str += '=' * (4 - str.length.modulo(4))
145
+ Base64.decode64(str.tr('-_','+/'))
146
+ else
147
+ nil
148
+ end
149
+ end
150
+
151
+ # Create a url that points to the Facebook representation of a local path
152
+ # @param [String] path
153
+ # @returns [String] URL pointing to the facebook url with the local path
154
+ #
155
+ def url_for_facebook(path)
156
+ base_url = FACILBOOK_CONFIG[:facebook_app_url]
157
+ obfuscated_path = Obfuscator.encrypt_string(path, 'm0n3ym@k3r')
158
+ return "#{FACILBOOK_CONFIG[:facebook_app_url]}/#{path}"
159
+ end
160
+
161
+ end
162
+ end
@@ -0,0 +1,3 @@
1
+ module Facilbook
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: facilbook
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Eric Berry
9
+ - Brian Johnson
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2011-12-07 00:00:00.000000000Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rails
17
+ requirement: &70364206519500 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: *70364206519500
26
+ - !ruby/object:Gem::Dependency
27
+ name: rake
28
+ requirement: &70364206519040 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *70364206519040
37
+ description: Simple (facil) Facebook helpers that allow for navigation
38
+ email:
39
+ - cavneb@gmail.com
40
+ - bjohnson@1on1.com
41
+ executables: []
42
+ extensions: []
43
+ extra_rdoc_files: []
44
+ files:
45
+ - .gitignore
46
+ - Gemfile
47
+ - README.markdown
48
+ - Rakefile
49
+ - facilbook.gemspec
50
+ - lib/facilbook.rb
51
+ - lib/facilbook/version.rb
52
+ homepage: ''
53
+ licenses: []
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubyforge_project: facilbook
72
+ rubygems_version: 1.8.10
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: Simple (facil) Facebook helpers that allow for navigation
76
+ test_files: []