jsmestad-frankie 0.4.1 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/VERSION +1 -1
  2. data/frankie.gemspec +1 -1
  3. data/lib/frankie.rb +150 -145
  4. metadata +1 -1
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.1
1
+ 0.4.2
data/frankie.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{frankie}
5
- s.version = "0.4.1"
5
+ s.version = "0.4.2"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Ron Evans", "Justin Smestad"]
data/lib/frankie.rb CHANGED
@@ -13,169 +13,174 @@ gem 'mmangino-facebooker'
13
13
  require 'facebooker'
14
14
 
15
15
  module Sinatra
16
-
17
16
  module Frankie
18
- def load_facebook_config(file, env=:development)
19
- if File.exist?(file)
20
- yaml = YAML.load_file(file)[env.to_s]
21
- ENV['FACEBOOK_API_KEY'] = yaml['api_key']
22
- ENV['FACEBOOK_SECRET_KEY'] = yaml['secret_key']
23
- ENV['FACEBOOKER_RELATIVE_URL_ROOT'] = yaml['canvas_page_name']
17
+
18
+ module Loader
19
+ def load_facebook_config(file, env=:development)
20
+ if File.exist?(file)
21
+ yaml = YAML.load_file(file)[env.to_s]
22
+ ENV['FACEBOOK_API_KEY'] = yaml['api_key']
23
+ ENV['FACEBOOK_SECRET_KEY'] = yaml['secret_key']
24
+ ENV['FACEBOOKER_RELATIVE_URL_ROOT'] = yaml['canvas_page_name']
25
+ end
24
26
  end
25
27
  end
26
28
 
27
- def facebook_session
28
- @facebook_session
29
- end
30
-
31
- def facebook_session_parameters
32
- {:fb_sig_session_key=>params["fb_sig_session_key"]}
33
- end
34
-
35
- def set_facebook_session
36
- session_set = session_already_secured? || secure_with_token! || secure_with_facebook_params!
37
- if session_set
38
- capture_facebook_friends_if_available!
39
- Facebooker::Session.current = facebook_session
29
+ module Helpers
30
+ def facebook_session
31
+ @facebook_session
40
32
  end
41
- session_set
42
- end
43
-
44
- def facebook_params
45
- @facebook_params ||= verified_facebook_params
46
- end
47
-
48
-
49
- def session_already_secured?
50
- (@facebook_session = session[:facebook_session]) && session[:facebook_session].secured?
51
- end
52
-
53
- def secure_with_token!
54
- if params['auth_token']
55
- @facebook_session = new_facebook_session
56
- @facebook_session.auth_token = params['auth_token']
57
- @facebook_session.secure!
58
- session[:facebook_session] = @facebook_session
33
+
34
+ def facebook_session_parameters
35
+ {:fb_sig_session_key=>params["fb_sig_session_key"]}
59
36
  end
60
- end
61
-
62
- def secure_with_facebook_params!
63
- return unless request_is_for_a_facebook_canvas?
64
37
 
65
- if ['user', 'session_key'].all? {|element| facebook_params[element]}
66
- @facebook_session = new_facebook_session
67
- @facebook_session.secure_with!(facebook_params['session_key'], facebook_params['user'], facebook_params['expires'])
68
- session[:facebook_session] = @facebook_session
38
+ def set_facebook_session
39
+ session_set = session_already_secured? || secure_with_token! || secure_with_facebook_params!
40
+ if session_set
41
+ capture_facebook_friends_if_available!
42
+ Facebooker::Session.current = facebook_session
43
+ end
44
+ session_set
69
45
  end
70
- end
71
-
72
- def create_new_facebook_session_and_redirect!
73
- session[:facebook_session] = new_facebook_session
74
- throw :halt, do_redirect(session[:facebook_session].login_url) unless @installation_required
75
- end
76
-
77
- def new_facebook_session
78
- Facebooker::Session.create(Facebooker::Session.api_key, Facebooker::Session.secret_key)
79
- end
80
-
81
- def capture_facebook_friends_if_available!
82
- return unless request_is_for_a_facebook_canvas?
83
- if friends = facebook_params['friends']
84
- facebook_session.user.friends = friends.map do |friend_uid|
85
- Facebooker::User.new(friend_uid, facebook_session)
46
+
47
+ def facebook_params
48
+ @facebook_params ||= verified_facebook_params
49
+ end
50
+
51
+
52
+ def session_already_secured?
53
+ (@facebook_session = session[:facebook_session]) && session[:facebook_session].secured?
54
+ end
55
+
56
+ def secure_with_token!
57
+ if params['auth_token']
58
+ @facebook_session = new_facebook_session
59
+ @facebook_session.auth_token = params['auth_token']
60
+ @facebook_session.secure!
61
+ session[:facebook_session] = @facebook_session
86
62
  end
87
63
  end
88
- end
89
-
90
- def blank?(value)
91
- (value == '0' || value.nil? || value == '')
92
- end
64
+
65
+ def secure_with_facebook_params!
66
+ return unless request_is_for_a_facebook_canvas?
67
+
68
+ if ['user', 'session_key'].all? {|element| facebook_params[element]}
69
+ @facebook_session = new_facebook_session
70
+ @facebook_session.secure_with!(facebook_params['session_key'], facebook_params['user'], facebook_params['expires'])
71
+ session[:facebook_session] = @facebook_session
72
+ end
73
+ end
74
+
75
+ def create_new_facebook_session_and_redirect!
76
+ session[:facebook_session] = new_facebook_session
77
+ throw :halt, do_redirect(session[:facebook_session].login_url) unless @installation_required
78
+ end
79
+
80
+ def new_facebook_session
81
+ Facebooker::Session.create(Facebooker::Session.api_key, Facebooker::Session.secret_key)
82
+ end
83
+
84
+ def capture_facebook_friends_if_available!
85
+ return unless request_is_for_a_facebook_canvas?
86
+ if friends = facebook_params['friends']
87
+ facebook_session.user.friends = friends.map do |friend_uid|
88
+ Facebooker::User.new(friend_uid, facebook_session)
89
+ end
90
+ end
91
+ end
92
+
93
+ def blank?(value)
94
+ (value == '0' || value.nil? || value == '')
95
+ end
93
96
 
94
- def verified_facebook_params
95
- facebook_sig_params = params.inject({}) do |collection, pair|
96
- collection[pair.first.sub(/^fb_sig_/, '')] = pair.last if pair.first[0,7] == 'fb_sig_'
97
- collection
97
+ def verified_facebook_params
98
+ facebook_sig_params = params.inject({}) do |collection, pair|
99
+ collection[pair.first.sub(/^fb_sig_/, '')] = pair.last if pair.first[0,7] == 'fb_sig_'
100
+ collection
101
+ end
102
+ verify_signature(facebook_sig_params, params['fb_sig'])
103
+ facebook_sig_params.inject(Hash.new) do |collection, pair|
104
+ collection[pair.first] = facebook_parameter_conversions[pair.first].call(pair.last)
105
+ collection
106
+ end
98
107
  end
99
- verify_signature(facebook_sig_params, params['fb_sig'])
100
- facebook_sig_params.inject(Hash.new) do |collection, pair|
101
- collection[pair.first] = facebook_parameter_conversions[pair.first].call(pair.last)
102
- collection
108
+
109
+ # 48.hours.ago in sinatra
110
+ def earliest_valid_session
111
+ now = Time.now
112
+ now -= (60 * 60 * 48)
113
+ now
103
114
  end
104
- end
105
-
106
- # 48.hours.ago in sinatra
107
- def earliest_valid_session
108
- now = Time.now
109
- now -= (60 * 60 * 48)
110
- now
111
- end
112
-
113
- def verify_signature(facebook_sig_params,expected_signature)
114
- raw_string = facebook_sig_params.map{ |*args| args.join('=') }.sort.join
115
- actual_sig = Digest::MD5.hexdigest([raw_string, Facebooker::Session.secret_key].join)
116
- raise Facebooker::Session::IncorrectSignature if actual_sig != expected_signature
117
- raise Facebooker::Session::SignatureTooOld if Time.at(facebook_sig_params['time'].to_f) < earliest_valid_session
118
- true
119
- end
120
-
121
- def facebook_parameter_conversions
122
- @facebook_parameter_conversions ||= Hash.new do |hash, key|
123
- lambda{|value| value}
124
- end.merge(
125
- 'time' => lambda{|value| Time.at(value.to_f)},
126
- 'in_canvas' => lambda{|value| !blank?(value)},
127
- 'added' => lambda{|value| !blank?(value)},
115
+
116
+ def verify_signature(facebook_sig_params,expected_signature)
117
+ raw_string = facebook_sig_params.map{ |*args| args.join('=') }.sort.join
118
+ actual_sig = Digest::MD5.hexdigest([raw_string, Facebooker::Session.secret_key].join)
119
+ raise Facebooker::Session::IncorrectSignature if actual_sig != expected_signature
120
+ raise Facebooker::Session::SignatureTooOld if Time.at(facebook_sig_params['time'].to_f) < earliest_valid_session
121
+ true
122
+ end
123
+
124
+ def facebook_parameter_conversions
125
+ @facebook_parameter_conversions ||= Hash.new do |hash, key|
126
+ lambda{|value| value}
127
+ end.merge(
128
+ 'time' => lambda{|value| Time.at(value.to_f)},
129
+ 'in_canvas' => lambda{|value| !blank?(value)},
130
+ 'added' => lambda{|value| !blank?(value)},
128
131
  'expires' => lambda{|value| blank?(value) ? nil : Time.at(value.to_f)},
129
132
  'friends' => lambda{|value| value.split(/,/)}
130
- )
131
- end
132
-
133
- def do_redirect(*args)
134
- if request_is_for_a_facebook_canvas?
135
- fbml_redirect_tag(args)
136
- else
137
- redirect args[0]
133
+ )
138
134
  end
139
- end
140
-
141
- def fbml_redirect_tag(url)
142
- "<fb:redirect url=\"#{url}\" />"
143
- end
144
-
145
- def request_is_for_a_facebook_canvas?
146
- return false if params["fb_sig_in_canvas"].nil?
147
- params["fb_sig_in_canvas"] == "1"
148
- end
149
-
150
- def application_is_installed?
151
- facebook_params['added']
152
- end
153
135
 
154
- def ensure_authenticated_to_facebook
155
- set_facebook_session || create_new_facebook_session_and_redirect!
156
- end
157
-
158
- def ensure_application_is_installed_by_facebook_user
159
- @installation_required = true
160
- authenticated_and_installed = ensure_authenticated_to_facebook && application_is_installed?
161
- application_is_not_installed_by_facebook_user unless authenticated_and_installed
162
- authenticated_and_installed
163
- end
164
-
165
- def application_is_not_installed_by_facebook_user
166
- throw :halt, do_redirect(session[:facebook_session].install_url)
167
- end
168
-
169
- def set_fbml_format
170
- params['format']="fbml" if request_is_for_a_facebook_canvas?
171
- end
136
+ def do_redirect(*args)
137
+ if request_is_for_a_facebook_canvas?
138
+ fbml_redirect_tag(args)
139
+ else
140
+ redirect args[0]
141
+ end
142
+ end
143
+
144
+ def fbml_redirect_tag(url)
145
+ "<fb:redirect url=\"#{url}\" />"
146
+ end
147
+
148
+ def request_is_for_a_facebook_canvas?
149
+ return false if params["fb_sig_in_canvas"].nil?
150
+ params["fb_sig_in_canvas"] == "1"
151
+ end
152
+
153
+ def application_is_installed?
154
+ facebook_params['added']
155
+ end
156
+
157
+ def ensure_authenticated_to_facebook
158
+ set_facebook_session || create_new_facebook_session_and_redirect!
159
+ end
160
+
161
+ def ensure_application_is_installed_by_facebook_user
162
+ @installation_required = true
163
+ authenticated_and_installed = ensure_authenticated_to_facebook && application_is_installed?
164
+ application_is_not_installed_by_facebook_user unless authenticated_and_installed
165
+ authenticated_and_installed
166
+ end
167
+
168
+ def application_is_not_installed_by_facebook_user
169
+ throw :halt, do_redirect(session[:facebook_session].install_url)
170
+ end
171
+
172
+ def set_fbml_format
173
+ params['format']="fbml" if request_is_for_a_facebook_canvas?
174
+ end
172
175
 
173
- def fb_url_for(url)
174
- url = "" if url == "/"
175
- url = URI.escape(url)
176
- return url if !request_is_for_a_facebook_canvas?
177
- "http://apps.facebook.com/#{ENV['FACEBOOKER_RELATIVE_URL_ROOT']}/#{url}"
176
+ def fb_url_for(url)
177
+ url = "" if url == "/"
178
+ url = URI.escape(url)
179
+ return url if !request_is_for_a_facebook_canvas?
180
+ "http://apps.facebook.com/#{ENV['FACEBOOKER_RELATIVE_URL_ROOT']}/#{url}"
181
+ end
178
182
  end
179
183
  end
180
- register Frankie
184
+ register Frankie::Loader
185
+ helpers Frankie::Helpers
181
186
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsmestad-frankie
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ron Evans