jsmestad-frankie 0.4.0 → 0.4.1

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 +121 -131
  4. metadata +1 -1
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.0
1
+ 0.4.1
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.0"
5
+ s.version = "0.4.1"
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
@@ -12,9 +12,9 @@ require 'uri'
12
12
  gem 'mmangino-facebooker'
13
13
  require 'facebooker'
14
14
 
15
- module Frankie
15
+ module Sinatra
16
16
 
17
- module Configure
17
+ module Frankie
18
18
  def load_facebook_config(file, env=:development)
19
19
  if File.exist?(file)
20
20
  yaml = YAML.load_file(file)[env.to_s]
@@ -23,9 +23,7 @@ module Frankie
23
23
  ENV['FACEBOOKER_RELATIVE_URL_ROOT'] = yaml['canvas_page_name']
24
24
  end
25
25
  end
26
- end
27
26
 
28
- module Helpers
29
27
  def facebook_session
30
28
  @facebook_session
31
29
  end
@@ -47,145 +45,137 @@ module Frankie
47
45
  @facebook_params ||= verified_facebook_params
48
46
  end
49
47
 
50
- private
51
48
 
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
62
- end
63
- 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
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
78
59
  end
60
+ end
61
+
62
+ def secure_with_facebook_params!
63
+ return unless request_is_for_a_facebook_canvas?
79
64
 
80
- def new_facebook_session
81
- Facebooker::Session.create(Facebooker::Session.api_key, Facebooker::Session.secret_key)
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
82
69
  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
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)
90
86
  end
91
87
  end
92
-
93
- def blank?(value)
94
- (value == '0' || value.nil? || value == '')
95
- end
88
+ end
89
+
90
+ def blank?(value)
91
+ (value == '0' || value.nil? || value == '')
92
+ end
96
93
 
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
107
- end
108
-
109
- # 48.hours.ago in sinatra
110
- def earliest_valid_session
111
- now = Time.now
112
- now -= (60 * 60 * 48)
113
- now
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
114
98
  end
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)},
131
- 'expires' => lambda{|value| blank?(value) ? nil : Time.at(value.to_f)},
132
- 'friends' => lambda{|value| value.split(/,/)}
133
- )
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
134
103
  end
135
-
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"
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)},
128
+ 'expires' => lambda{|value| blank?(value) ? nil : Time.at(value.to_f)},
129
+ '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]
151
138
  end
152
-
153
- def application_is_installed?
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?
154
151
  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
152
+ end
171
153
 
172
- def set_fbml_format
173
- params['format']="fbml" if request_is_for_a_facebook_canvas?
174
- end
175
-
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
182
- end
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
183
172
 
184
- def self.registered(app)
185
- app.configure Frankie::Configure
186
- app.helpers Frankie::Helpers
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}"
178
+ end
187
179
  end
188
-
180
+ register Frankie
189
181
  end
190
-
191
- Sinatra::Base.register Frankie
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.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ron Evans