Module: RestGraph::RailsUtil
- Defined in:
- lib/rest-graph/rails_util.rb
Defined Under Namespace
Modules: Helper
Class Method Summary collapse
- .included(controller) ⇒ Object
- .init(app = Rails) ⇒ Object
- .rest_graph_auto_authorize? ⇒ Boolean
-
.rest_graph_check_code ⇒ Object
exchange the code with access_token.
-
.rest_graph_check_cookie ⇒ Object
if we’re not in canvas nor code passed, we could check out cookies as well.
-
.rest_graph_check_params_session ⇒ Object
if the code is bad or not existed, check if there’s one in session, meanwhile, there the sig and access_token is correct, that means we’re in the context of canvas.
-
.rest_graph_check_params_signed_request ⇒ Object
begin facebook check ======================.
- .rest_graph_check_rg_cookies ⇒ Object
- .rest_graph_check_rg_fbs ⇒ Object
- .rest_graph_check_rg_handler(handler = rest_graph_oget(:check_handler)) ⇒ Object
- .rest_graph_check_rg_session ⇒ Object
-
.rest_graph_cleanup ⇒ Object
begin misc ================================.
- .rest_graph_extract_options(options, method) ⇒ Object
- .rest_graph_filter_uri(uri) ⇒ Object
- .rest_graph_in_canvas? ⇒ Boolean
- .rest_graph_normalized_request_uri ⇒ Object
-
.rest_graph_oget(key) ⇒ Object
begin options utility =======================.
- .rest_graph_options_ctl ⇒ Object
- .rest_graph_options_new ⇒ Object
-
.rest_graph_storage_key ⇒ Object
begin check ================================.
- .rest_graph_write_rg_cookies ⇒ Object
-
.rest_graph_write_rg_fbs ⇒ Object
end check ================================ ==================== begin write ================================.
- .rest_graph_write_rg_handler(handler = rest_graph_oget(:write_handler)) ⇒ Object
- .rest_graph_write_rg_session ⇒ Object
Instance Method Summary collapse
-
#rest_graph ⇒ Object
override this if you need different app_id and secret.
- #rest_graph_authorize(error = nil, force_redirect = true) ⇒ Object
- #rest_graph_authorize_body(redirect_url = @rest_graph_authorize_url) ⇒ Object
-
#rest_graph_authorize_redirect ⇒ Object
override this if you want the simple redirect_to.
- #rest_graph_js_redirect(redirect_url, body = '') ⇒ Object
- #rest_graph_on_access_token_error(error = nil) ⇒ Object (also: #rest_graph_on_error)
- #rest_graph_setup(options = {}) ⇒ Object
Class Method Details
.included(controller) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/rest-graph/rails_util.rb', line 59 def self.included controller # skip if included already, any better way to detect this? return if controller.respond_to?(:rest_graph, true) controller.rescue_from(RestGraph::Error::AccessToken, :with => :rest_graph_on_access_token_error) controller.helper(RestGraph::RailsUtil::Helper) controller.instance_methods.select{ |method| method.to_s =~ /^rest_graph/ }.each{ |method| controller.send(:protected, method) } end |
.init(app = Rails) ⇒ Object
48 49 50 51 |
# File 'lib/rest-graph/rails_util.rb', line 48 def self.init app=Rails ActiveSupport::Cache::Store.send(:include, RestGraph::RailsCache) RestGraph::ConfigUtil.load_config_for_rails(app) end |
.rest_graph_auto_authorize? ⇒ Boolean
355 356 357 358 359 |
# File 'lib/rest-graph/rails_util.rb', line 355 def !rest_graph_oget(:auto_authorize_scope) .blank? || !rest_graph_oget(:auto_authorize_options).blank? || rest_graph_oget(:auto_authorize) end |
.rest_graph_check_code ⇒ Object
exchange the code with access_token
236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/rest-graph/rails_util.rb', line 236 def rest_graph_check_code return if rest_graph. || !params[:code] rest_graph.(:code => params[:code], :redirect_uri => rest_graph_normalized_request_uri) logger.debug( "DEBUG: RestGraph: detected code with " \ "#{rest_graph_normalized_request_uri}, " \ "parsed: #{rest_graph.data.inspect}") rest_graph_write_rg_fbs if rest_graph. end |
.rest_graph_check_cookie ⇒ Object
if we’re not in canvas nor code passed, we could check out cookies as well.
223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/rest-graph/rails_util.rb', line 223 def return if rest_graph. || (!["fbsr_#{rest_graph.app_id}"] && !["fbs_#{rest_graph.app_id}"]) rest_graph.() logger.debug("DEBUG: RestGraph: detected cookies, parsed:" \ " #{rest_graph.data.inspect}") rest_graph_write_rg_fbs if rest_graph. end |
.rest_graph_check_params_session ⇒ Object
if the code is bad or not existed, check if there’s one in session, meanwhile, there the sig and access_token is correct, that means we’re in the context of canvas
207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/rest-graph/rails_util.rb', line 207 def rest_graph_check_params_session return if rest_graph. || !params[:session] rest_graph.parse_json!(params[:session]) logger.debug("DEBUG: RestGraph: detected session, parsed:" \ " #{rest_graph.data.inspect}") if rest_graph. rest_graph_write_rg_fbs else logger.warn("WARN: RestGraph: bad session: #{params[:session]}") end end |
.rest_graph_check_params_signed_request ⇒ Object
begin facebook check ======================
188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/rest-graph/rails_util.rb', line 188 def rest_graph_check_params_signed_request return if rest_graph. || !params[:signed_request] rest_graph.parse_signed_request!(params[:signed_request]) logger.debug("DEBUG: RestGraph: detected signed_request, parsed:" \ " #{rest_graph.data.inspect}") if rest_graph. rest_graph_write_rg_fbs else logger.warn( "WARN: RestGraph: bad signed_request: #{params[:signed_request]}") end end |
.rest_graph_check_rg_cookies ⇒ Object
278 279 280 281 282 283 284 |
# File 'lib/rest-graph/rails_util.rb', line 278 def return if rest_graph. || !rest_graph_oget(:write_cookies) || !(fbs = [rest_graph_storage_key]) rest_graph.parse_fbs!(fbs) logger.debug("DEBUG: RestGraph: detected rest-graph cookies, parsed:" \ " #{rest_graph.data.inspect}") end |
.rest_graph_check_rg_fbs ⇒ Object
257 258 259 260 261 |
# File 'lib/rest-graph/rails_util.rb', line 257 def rest_graph_check_rg_fbs rest_graph_check_rg_handler # custom method to store fbs rest_graph_check_rg_session # prefered way to store fbs # in canvas, session might not work.. end |
.rest_graph_check_rg_handler(handler = rest_graph_oget(:check_handler)) ⇒ Object
263 264 265 266 267 268 |
# File 'lib/rest-graph/rails_util.rb', line 263 def rest_graph_check_rg_handler handler=rest_graph_oget(:check_handler) return if rest_graph. || !handler rest_graph.parse_fbs!(handler.call) logger.debug("DEBUG: RestGraph: called check_handler, parsed:" \ " #{rest_graph.data.inspect}") end |
.rest_graph_check_rg_session ⇒ Object
270 271 272 273 274 275 276 |
# File 'lib/rest-graph/rails_util.rb', line 270 def rest_graph_check_rg_session return if rest_graph. || !rest_graph_oget(:write_session) || !(fbs = session[rest_graph_storage_key]) rest_graph.parse_fbs!(fbs) logger.debug("DEBUG: RestGraph: detected rest-graph session, parsed:" \ " #{rest_graph.data.inspect}") end |
.rest_graph_cleanup ⇒ Object
begin misc ================================
315 316 317 318 319 320 |
# File 'lib/rest-graph/rails_util.rb', line 315 def rest_graph_cleanup .delete("fbs_#{rest_graph.app_id}") .delete("fbsr_#{rest_graph.app_id}") .delete(rest_graph_storage_key) session.delete(rest_graph_storage_key) end |
.rest_graph_extract_options(options, method) ⇒ Object
361 362 363 364 |
# File 'lib/rest-graph/rails_util.rb', line 361 def , method # Hash[] is for ruby 1.8.7 Hash[.send(method){ |(k, v)| RestGraph::Attributes.member?(k) }] end |
.rest_graph_filter_uri(uri) ⇒ Object
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
# File 'lib/rest-graph/rails_util.rb', line 335 def rest_graph_filter_uri uri URI.parse(uri).tap{ |uri| uri.query = uri.query.split('&').reject{ |q| q =~ /^(code|session|signed_request)\=/ }.join('&') if uri.query uri.query = nil if uri.query.blank? }.to_s rescue URI::InvalidURIError => e if @rest_graph_facebook_filter_uri_retry raise e else @rest_graph_facebook_filter_uri_retry = uri = URI.encode(uri) retry end end |
.rest_graph_in_canvas? ⇒ Boolean
351 352 353 |
# File 'lib/rest-graph/rails_util.rb', line 351 def rest_graph_in_canvas? !rest_graph_oget(:canvas).blank? end |
.rest_graph_normalized_request_uri ⇒ Object
322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/rest-graph/rails_util.rb', line 322 def rest_graph_normalized_request_uri uri = if rest_graph_in_canvas? # rails 3 uses newer rack which has fullpath "http://apps.facebook.com/#{rest_graph_oget(:canvas)}" + (request.respond_to?(:fullpath) ? request.fullpath : request.request_uri) else request.url end rest_graph_filter_uri(uri) end |
.rest_graph_oget(key) ⇒ Object
begin options utility =======================
168 169 170 171 172 173 174 |
# File 'lib/rest-graph/rails_util.rb', line 168 def rest_graph_oget key if .has_key?(key) [key] else RestGraph.send("default_#{key}") end end |
.rest_graph_options_ctl ⇒ Object
176 177 178 |
# File 'lib/rest-graph/rails_util.rb', line 176 def @rest_graph_options_ctl ||= {} end |
.rest_graph_options_new ⇒ Object
180 181 182 |
# File 'lib/rest-graph/rails_util.rb', line 180 def @rest_graph_options_new ||= {} end |
.rest_graph_storage_key ⇒ Object
begin check ================================
253 254 255 |
# File 'lib/rest-graph/rails_util.rb', line 253 def rest_graph_storage_key "rest_graph_fbs_#{rest_graph_oget(:app_id)}" end |
.rest_graph_write_rg_cookies ⇒ Object
305 306 307 308 309 |
# File 'lib/rest-graph/rails_util.rb', line 305 def return if !rest_graph_oget(:write_cookies) [rest_graph_storage_key] = fbs = rest_graph.fbs logger.debug("DEBUG: RestGraph: wrote cookies: fbs => #{fbs}") end |
.rest_graph_write_rg_fbs ⇒ Object
end check ================================
begin write ================================
287 288 289 290 291 |
# File 'lib/rest-graph/rails_util.rb', line 287 def rest_graph_write_rg_fbs rest_graph_write_rg_handler rest_graph_write_rg_session end |
.rest_graph_write_rg_handler(handler = rest_graph_oget(:write_handler)) ⇒ Object
293 294 295 296 297 |
# File 'lib/rest-graph/rails_util.rb', line 293 def rest_graph_write_rg_handler handler=rest_graph_oget(:write_handler) return if !handler handler.call(fbs = rest_graph.fbs) logger.debug("DEBUG: RestGraph: called write_handler: fbs => #{fbs}") end |
.rest_graph_write_rg_session ⇒ Object
299 300 301 302 303 |
# File 'lib/rest-graph/rails_util.rb', line 299 def rest_graph_write_rg_session return if !rest_graph_oget(:write_session) session[rest_graph_storage_key] = fbs = rest_graph.fbs logger.debug("DEBUG: RestGraph: wrote session: fbs => #{fbs}") end |
Instance Method Details
#rest_graph ⇒ Object
override this if you need different app_id and secret
97 98 99 |
# File 'lib/rest-graph/rails_util.rb', line 97 def rest_graph @rest_graph ||= RestGraph.new() end |
#rest_graph_authorize(error = nil, force_redirect = true) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/rest-graph/rails_util.rb', line 107 def error=nil, force_redirect=true logger.info("INFO: RestGraph: #{error.inspect}") if force_redirect || @rest_graph_authorize_url = rest_graph.( {:redirect_uri => rest_graph_normalized_request_uri, :scope => rest_graph_oget(:auto_authorize_scope)}. merge(rest_graph_oget(:auto_authorize_options))) logger.debug("DEBUG: RestGraph: redirect to #{@rest_graph_authorize_url}") rest_graph_cleanup end end |
#rest_graph_authorize_body(redirect_url = @rest_graph_authorize_url) ⇒ Object
155 156 157 158 159 160 161 162 163 |
# File 'lib/rest-graph/rails_util.rb', line 155 def redirect_url=@rest_graph_authorize_url <<-HTML <div> Please <a href="#{CGI.escapeHTML(redirect_url)}" target="_top">authorize</a> if this page is not automatically redirected. </div> HTML end |
#rest_graph_authorize_redirect ⇒ Object
override this if you want the simple redirect_to
124 125 126 127 128 129 130 131 |
# File 'lib/rest-graph/rails_util.rb', line 124 def unless rest_graph_in_canvas? redirect_to @rest_graph_authorize_url else rest_graph_js_redirect(@rest_graph_authorize_url, ) end end |
#rest_graph_js_redirect(redirect_url, body = '') ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/rest-graph/rails_util.rb', line 133 def rest_graph_js_redirect redirect_url, body='' render :inline => <<-HTML <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <script type="text/javascript"> window.top.location.href = '#{redirect_url}' </script> <noscript> <meta http-equiv="refresh" content="0;url=#{ CGI.escapeHTML(redirect_url)}"/> <meta http-equiv="window-target" content="_top"/> </noscript> </head> <body> #{body} </bodt> </html> HTML end |
#rest_graph_on_access_token_error(error = nil) ⇒ Object Also known as: rest_graph_on_error
101 102 103 |
# File 'lib/rest-graph/rails_util.rb', line 101 def rest_graph_on_access_token_error error=nil (error, false) end |
#rest_graph_setup(options = {}) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/rest-graph/rails_util.rb', line 71 def rest_graph_setup ={} .merge!((, :reject)) .merge!((, :select)) # we'll need to reinitialize rest_graph with the new options, # otherwise if you're calling rest_graph before rest_graph_setup, # you'll end up with default options without the ones you've passed # into rest_graph_setup. rest_graph.send(:initialize, ) rest_graph_check_params_signed_request # canvas rest_graph_check_params_session # i think it would be deprecated rest_graph_check_code # oauth api rest_graph_check_rg_fbs # check rest-graph storage # for js sdk (canvas or not) if rest_graph_oget(:ensure_authorized) && !rest_graph. ('ensure authorized') false # action halt, redirect to do authorize, # eagerly, as opposed to auto_authorize else true # keep going end end |