carrot-facebook 0.0.1

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,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Carrot Creative
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Carrot::Facebook
2
+
3
+ Facebook view helpers and signed request handling.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'carrot-facebook'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install carrot-facebook
18
+
19
+ ## Usage
20
+
21
+ Coming Soon.
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'carrot-facebook/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "carrot-facebook"
8
+ gem.version = Carrot::Facebook::VERSION
9
+ gem.authors = ["Tom Milewski", "Kyle MacDonald"]
10
+ gem.email = ["tmilewski@gmail.com", "kyle@carrotcreative.com"]
11
+ gem.description = %q{Facebook view helpers and signed request handling.}
12
+ gem.summary = %q{Facebook view helpers and signed request handling.}
13
+ gem.homepage = "http://carrotcreative.com/"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency "yajl-ruby"
21
+ gem.add_development_dependency "rails"
22
+ gem.add_development_dependency "rspec"
23
+ end
@@ -0,0 +1,4 @@
1
+ require "carrot-facebook/middleware"
2
+ require "carrot-facebook/version"
3
+ require "carrot-facebook/view_helpers"
4
+ require 'carrot-facebook/railtie' if defined?(Rails)
@@ -0,0 +1,49 @@
1
+ require 'yajl'
2
+
3
+ module Carrot
4
+ module Facebook
5
+ class Middleware
6
+ def initialize(app)
7
+ @app = app
8
+ end
9
+
10
+ def call(env)
11
+ request = Rack::Request.new(env)
12
+ env[:is_iframe_app] = false
13
+
14
+ if request.POST['signed_request']
15
+ env["REQUEST_METHOD"] = 'GET'
16
+ env[:is_iframe_app] = true
17
+ env[:facebook_data] = parsed_signed_request(request.POST['signed_request'])
18
+
19
+ if env[:facebook_data]
20
+ if env[:facebook_data][:app_data].present?
21
+ begin
22
+ path_override = Yajl::Parser.parse(env[:facebook_data][:app_data], symbolize_keys: true)
23
+ env['PATH_INFO'] = path_override[:path] if path_override[:path]
24
+ env[:facebook_data][:app_data] = path_override
25
+ rescue => e
26
+ env[:facebook_data][:app_data] = env[:facebook_data][:app_data]
27
+ end
28
+ else
29
+ env[:facebook_data][:app_data] = nil
30
+ end
31
+ end
32
+ end
33
+
34
+ @app.call(env)
35
+ end
36
+
37
+ private
38
+
39
+ def parsed_signed_request(signed_request)
40
+ begin
41
+ p = signed_request.split('.')[1]
42
+ Yajl::Parser.parse(Base64.decode64(p + "=" * (4 - p.size % 4)), symbolize_keys: true)
43
+ rescue
44
+ nil
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,16 @@
1
+ require 'carrot-facebook/middleware'
2
+ require 'carrot-facebook/view_helpers'
3
+
4
+ module Carrot
5
+ module Facebook
6
+ class Railtie < Rails::Railtie
7
+ initializer "carrot.facebook.middleware" do |app|
8
+ app.middleware.use Middleware
9
+ end
10
+
11
+ initializer "carrot.facebook.view_helpers" do
12
+ ActionView::Base.send :include, ViewHelpers
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,5 @@
1
+ module Carrot
2
+ module Facebook
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,71 @@
1
+ include ActionView::Helpers::TagHelper
2
+
3
+ module Carrot
4
+ module Facebook
5
+ module ViewHelpers
6
+ #extend ActionView::Helpers::AssetTagHelper
7
+ # Public: Generates a user's Facebook avatar.
8
+ #
9
+ # fb_id - The String of the Facebook user's ID.
10
+ # type - The Symbol of the desired image size.
11
+ # [:square (50x50), :small (50xN), :normal (100xN), :large (200xN)]
12
+ #
13
+ # Examples
14
+ #
15
+ # fb_avatar('1234567890', :normal)
16
+ # # => "//graph.facebook.com/1234567890/picture?type=normal"
17
+ #
18
+ # Returns the generated Facebook URL.
19
+ def fb_avatar(fb_id = nil, type = :square)
20
+ if fb_id
21
+ "//graph.facebook.com/#{fb_id}/picture?type=#{type}"
22
+ end
23
+ end
24
+
25
+ # Public: Display a user's Facebook avatar.
26
+ #
27
+ # fb_id - The String of the Facebook user's ID.
28
+ # type - The Symbol of the desired image size.
29
+ # [:square (50x50), :small (50xN), :normal (100xN), :large (200xN)]
30
+ #
31
+ # Examples
32
+ #
33
+ # fb_avatar_tag('1234567890', :normal)
34
+ # # => '<img alt="Facebook Avatar for User 1234567890" src="//graph.facebook.com/1234567890/picture?type=normal" />'
35
+ #
36
+ # Returns the image tag for the Facebook user's avatar.
37
+ def fb_avatar_tag(fb_id = nil, type = :square)
38
+ if fb_id
39
+ tag(:img, src: fb_avatar(fb_id, type), alt: "Facebook Avatar for User #{fb_id}")
40
+ end
41
+ end
42
+
43
+ # Public: Generate Facebook's Javascript
44
+ #
45
+ # options - A Hash potentially consisting of two keys:
46
+ # app_id - A String denoting the Facebook App ID (Also accepts ENV["FACEBOOK_APP_ID"])
47
+ # xfbml - A Boolean denoting whether XFBML should be rendered upon pageload
48
+ #
49
+ # Examples
50
+ #
51
+ # fb_js
52
+ # fb_js(app_id: '1234567890')
53
+ # fb_js(xfbml: false)
54
+ # fb_js(app_id: '1234567890', xfbml: false)
55
+ #
56
+ # Returns and initializes the Facebook Javascript SDK
57
+ def fbjs(*options)
58
+ opts = { :app_id => ENV['FACEBOOK_APP_ID'], :xfbml => true }
59
+ opts.merge!(options.first) unless options.empty?
60
+
61
+ %Q{
62
+ <div id="fb-root"></div>'
63
+ <script src="//connect.facebook.net/en_US/all.js"></script>
64
+ <script>
65
+ FB.init({ appId: '#{opts[:app_id]}', xfbml: '#{opts[:xfbml]}' });
66
+ </script>
67
+ }
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,156 @@
1
+ require 'spec_helper'
2
+
3
+ describe Carrot::Facebook::Middleware do
4
+ before(:all) do
5
+ @headers = { 'Content-Type' => 'text/html' }
6
+ @app = lambda { |env| [200, @headers, []] }
7
+ @valid_facebook_data = {
8
+ algorithm: 'HMAC-SHA256',
9
+ issued_at: 1331665894,
10
+ page: {
11
+ id: '137739982942274',
12
+ liked: false,
13
+ admin: true
14
+ },
15
+ user: {
16
+ country: 'us',
17
+ locale: 'en_US',
18
+ age: {
19
+ min: 21
20
+ }
21
+ }
22
+ }
23
+ end
24
+
25
+ context 'when a normal GET request is received' do
26
+ before(:all) do
27
+ @request = Rack::MockRequest.env_for('/', lint: true, fatal: true, method: 'GET')
28
+ @response = Carrot::Facebook::Middleware.new(@app).call(@request)
29
+ end
30
+
31
+ it 'should be a GET request' do
32
+ expect(@request['REQUEST_METHOD']).to eq 'GET'
33
+ end
34
+
35
+ it 'should keep the headers intact' do
36
+ expect(@response[1]).to eq @headers
37
+ end
38
+
39
+ it 'should be successful' do
40
+ expect(@response[0]).to eq 200
41
+ end
42
+
43
+ it 'should not be considered a canvas application' do
44
+ expect(@request[:is_iframe_app]).to be_false
45
+ end
46
+ end
47
+
48
+ context "when a normal POST request is received" do
49
+ before(:all) do
50
+ @request = Rack::MockRequest.env_for('/', lint: true, fatal: true, method: 'POST')
51
+ @response = Carrot::Facebook::Middleware.new(@app).call(@request)
52
+ end
53
+
54
+ it 'should be a POST request' do
55
+ expect(@request['REQUEST_METHOD']).to eq 'POST'
56
+ end
57
+
58
+ it 'should keep the headers intact' do
59
+ expect(@response[1]).to eq @headers
60
+ end
61
+
62
+ it 'should be successful' do
63
+ expect(@response[0]).to eq 200
64
+ end
65
+
66
+ it 'should not be considered a canvas application' do
67
+ expect(@request[:is_iframe_app]).to be_false
68
+ end
69
+ end
70
+
71
+ context "when is a canvas app and sends an invalid signed_request" do
72
+ before(:all) do
73
+ @signed_request = 'signed_request=kUbujwbyaKAS3EMLSolZki1mJtuzY5-XUrchNN.eyJhbGdvcml0aG0iORtaW4iOnRydWV9LCJ1c2VyIjp7ImNvdW50cnkiOiJ1cyIsImxvY2FsZSI6ImVuX1VTIiwiYWdlIjp7Im1pbiI6MjF9f'
74
+ @request = Rack::MockRequest.env_for('/', lint: true, fatal: true, method: 'POST', input: @signed_request)
75
+ @response = Carrot::Facebook::Middleware.new(@app).call(@request)
76
+ end
77
+
78
+ it 'should be converted to a GET request' do
79
+ expect(@request['REQUEST_METHOD']).to eq 'GET'
80
+ end
81
+
82
+ it 'should keep the headers intact' do
83
+ expect(@response[1]).to eq @headers
84
+ end
85
+
86
+ it 'should be successful' do
87
+ expect(@response[0]).to eq 200
88
+ end
89
+
90
+ it 'should not be considered a canvas application' do
91
+ expect(@request[:is_iframe_app]).to be_true
92
+ end
93
+
94
+ it 'should correctly set :facebook_data' do
95
+ expect(@request[:facebook_data]).to be_nil
96
+ end
97
+ end
98
+
99
+ #pending 'test invalid app data'
100
+
101
+ context "when is a canvas app and sends a valid signed_request" do
102
+ before(:all) do
103
+ @signed_request = 'signed_request=kUbujwbyaKAS3EMLSolZki1mJtuzY5-XUrchNN3MVyI.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsImlzc3VlZF9hdCI6MTMzMTY2NTg5NCwicGFnZSI6eyJpZCI6IjEzNzczOTk4Mjk0MjI3NCIsImxpa2VkIjpmYWxzZSwiYWRtaW4iOnRydWV9LCJ1c2VyIjp7ImNvdW50cnkiOiJ1cyIsImxvY2FsZSI6ImVuX1VTIiwiYWdlIjp7Im1pbiI6MjF9fX0'
104
+ @request = Rack::MockRequest.env_for('/', lint: true, fatal: true, method: 'POST', input: @signed_request)
105
+ @response = Carrot::Facebook::Middleware.new(@app).call(@request)
106
+ end
107
+
108
+ it 'should be converted to a GET request' do
109
+ expect(@request['REQUEST_METHOD']).to eq 'GET'
110
+ end
111
+
112
+ it 'should keep the headers intact' do
113
+ expect(@response[1]).to eq @headers
114
+ end
115
+
116
+ it 'should be successful' do
117
+ expect(@response[0]).to eq 200
118
+ end
119
+
120
+ it 'should not be considered a canvas application' do
121
+ expect(@request[:is_iframe_app]).to be_true
122
+ end
123
+
124
+ it 'should correctly set :facebook_data' do
125
+ expect(@request[:facebook_data]).to eq @valid_facebook_data.merge({ issued_at: 1331665894, app_data: nil })
126
+ end
127
+ end
128
+
129
+ context "when is a canvas app and sends a valid signed_request with app_data" do
130
+ before(:all) do
131
+ @signed_request = 'signed_request=PGQuysjAoFJBxu2Me_HizoVv8BdiPdm5wBhkcdHZaR0.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsImFwcF9kYXRhIjoie1wicGF0aFwiOlwiXC9wYWdlXC90ZXJtc1wifSIsImlzc3VlZF9hdCI6MTMzMTY2OTE4NSwicGFnZSI6eyJpZCI6IjEzNzczOTk4Mjk0MjI3NCIsImxpa2VkIjpmYWxzZSwiYWRtaW4iOnRydWV9LCJ1c2VyIjp7ImNvdW50cnkiOiJ1cyIsImxvY2FsZSI6ImVuX1VTIiwiYWdlIjp7Im1pbiI6MjF9fX0'
132
+ @request = Rack::MockRequest.env_for('/', lint: true, fatal: true, method: 'POST', input: @signed_request)
133
+ @response = Carrot::Facebook::Middleware.new(@app).call(@request)
134
+ end
135
+
136
+ it 'should be converted to a GET request' do
137
+ expect(@request['REQUEST_METHOD']).to eq 'GET'
138
+ end
139
+
140
+ it 'should keep the headers intact' do
141
+ expect(@response[1]).to eq @headers
142
+ end
143
+
144
+ it 'should be successful' do
145
+ expect(@response[0]).to eq 200
146
+ end
147
+
148
+ it 'should not be considered a canvas application' do
149
+ expect(@request[:is_iframe_app]).to be_true
150
+ end
151
+
152
+ it 'should correctly set :facebook_data' do
153
+ expect(@request[:facebook_data]).to eq @valid_facebook_data.merge({ issued_at: 1331669185, app_data: { path: '/page/terms' } })
154
+ end
155
+ end
156
+ end
@@ -0,0 +1,7 @@
1
+ require "rails/all"
2
+ require "carrot-facebook"
3
+
4
+ RSpec.configure do |config|
5
+ config.color_enabled = true
6
+ config.formatter = :documentation
7
+ end
@@ -0,0 +1,80 @@
1
+ require 'spec_helper'
2
+ #include Carrot::Facebook::ViewHelpers
3
+ #include ActionView::Helpers::TagHelper
4
+
5
+ #require 'action_view'
6
+ #require 'active_support'
7
+
8
+ include Carrot::Facebook::ViewHelpers
9
+ #include ActionView::Helpers
10
+ #include ActionView::Helpers::TagHelper
11
+
12
+ describe Carrot::Facebook::ViewHelpers, :type => :helper do
13
+ let(:helper) { Carrot::Facebook::ViewHelpers }
14
+
15
+ context "#fb_avatar" do
16
+ it "should output a default URL with a 'square' format" do
17
+ output = helper.fb_avatar('tmilewski')
18
+ expect(output).to match /\?type=square\z/
19
+ end
20
+
21
+ it "should output a URL for a 'large' format" do
22
+ output = helper.fb_avatar('tmilewski', :large)
23
+ expect(output).to match /\?type=large\z/
24
+ end
25
+
26
+ it "should accept a Facebook ID as an Integer and output a formatted URL" do
27
+ output = helper.fb_avatar(8201087)
28
+ expect(output).to match /.com\/8201087\//
29
+ end
30
+
31
+ it "should accept a Facebook ID as a String and output a formatted URL" do
32
+ output = helper.fb_avatar('tmilewski')
33
+ expect(output).to match /.com\/tmilewski\//
34
+ end
35
+
36
+ it "should not raise an error if a Facebook ID isn't provided" do
37
+ expect(helper.fb_avatar).to_not raise_error(ArgumentError)
38
+ end
39
+
40
+ it "should return nil if a Facebook ID isn't provided" do
41
+ expect(helper.fb_avatar).to be_nil
42
+ end
43
+ end
44
+
45
+ context "#fb_avatar_tag" do
46
+ it "should output the URL within an image tag" do
47
+ output = helper.fb_avatar_tag('tmilewski')
48
+ expect(output.to_str).to eq '<img alt="Facebook Avatar for User tmilewski" src="//graph.facebook.com/tmilewski/picture?type=square" />'
49
+ end
50
+
51
+ it "should not raise an error if a Facebook ID isn't provided" do
52
+ expect(helper.fb_avatar_tag).to_not raise_error(ArgumentError)
53
+ end
54
+
55
+ it "should return nil if a Facebook ID isn't provided" do
56
+ expect(helper.fb_avatar_tag).to be_nil
57
+ end
58
+ end
59
+
60
+ context "#fb_js" do
61
+ it "should output the correct JS using ENV variables" do
62
+ ENV.stub(:[]).with("FACEBOOK_APP_ID").and_return("asdf")
63
+ output = helper.fbjs
64
+ expect(output).to match /appId: 'asdf'/
65
+ expect(output).to match /xfbml: 'true'/
66
+ end
67
+
68
+ it "should allow an override of the ENV variable" do
69
+ ENV.stub(:[]).with("FACEBOOK_APP_ID").and_return("asdf")
70
+ output = helper.fbjs(:app_id => "qwerty")
71
+ expect(output).to match /appId: 'qwerty'/
72
+ end
73
+
74
+ it "should allow a user to disable XFBML rendering" do
75
+ output = helper.fbjs(:app_id => "qwerty", :xfbml => false)
76
+ expect(output).to match /appId: 'qwerty'/
77
+ expect(output).to match /xfbml: 'false'/
78
+ end
79
+ end
80
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: carrot-facebook
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Tom Milewski
9
+ - Kyle MacDonald
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-01-18 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: yajl-ruby
17
+ requirement: &70121394753180 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *70121394753180
26
+ - !ruby/object:Gem::Dependency
27
+ name: rails
28
+ requirement: &70121394752400 !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: *70121394752400
37
+ - !ruby/object:Gem::Dependency
38
+ name: rspec
39
+ requirement: &70121394745980 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *70121394745980
48
+ description: Facebook view helpers and signed request handling.
49
+ email:
50
+ - tmilewski@gmail.com
51
+ - kyle@carrotcreative.com
52
+ executables: []
53
+ extensions: []
54
+ extra_rdoc_files: []
55
+ files:
56
+ - .gitignore
57
+ - Gemfile
58
+ - LICENSE.txt
59
+ - README.md
60
+ - Rakefile
61
+ - carrot-facebook.gemspec
62
+ - lib/carrot-facebook.rb
63
+ - lib/carrot-facebook/middleware.rb
64
+ - lib/carrot-facebook/railtie.rb
65
+ - lib/carrot-facebook/version.rb
66
+ - lib/carrot-facebook/view_helpers.rb
67
+ - spec/middleware_spec.rb
68
+ - spec/spec_helper.rb
69
+ - spec/view_helper_spec.rb
70
+ homepage: http://carrotcreative.com/
71
+ licenses: []
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 1.8.17
91
+ signing_key:
92
+ specification_version: 3
93
+ summary: Facebook view helpers and signed request handling.
94
+ test_files:
95
+ - spec/middleware_spec.rb
96
+ - spec/spec_helper.rb
97
+ - spec/view_helper_spec.rb