From 0280e04008b47bed61ecf25c48b585c0c426115e Mon Sep 17 00:00:00 2001 From: stveep Date: Sun, 2 Oct 2016 14:28:07 +0100 Subject: [PATCH] Move OAuth2 patching to an initializer --- app/helpers/admin/stripe_helper.rb | 6 ++-- config/initializers/oauth2.rb | 47 ++++++++++++++++++++++++++++++ lib/oauth2/client.rb | 21 ------------- lib/oauth2/strategy/deauthorize.rb | 24 --------------- 4 files changed, 50 insertions(+), 48 deletions(-) create mode 100644 config/initializers/oauth2.rb delete mode 100644 lib/oauth2/client.rb delete mode 100644 lib/oauth2/strategy/deauthorize.rb diff --git a/app/helpers/admin/stripe_helper.rb b/app/helpers/admin/stripe_helper.rb index 9b542dff99..757132aa71 100644 --- a/app/helpers/admin/stripe_helper.rb +++ b/app/helpers/admin/stripe_helper.rb @@ -1,6 +1,6 @@ -require File.join(Rails.root, '/lib/oauth2/strategy/deauthorize') -require File.join(Rails.root, '/lib/oauth2/client') -require 'oauth2' +# require File.join(Rails.root, '/lib/oauth2/strategy/deauthorize') +# require File.join(Rails.root, '/lib/oauth2/client') +# require 'oauth2' module Admin module StripeHelper diff --git a/config/initializers/oauth2.rb b/config/initializers/oauth2.rb new file mode 100644 index 0000000000..2b46cb0e1e --- /dev/null +++ b/config/initializers/oauth2.rb @@ -0,0 +1,47 @@ +Rails.application.config.to_prepare do + OAuth2::Client.class_eval do + def deauthorize_url(params = nil) + connection.build_url(options[:deauthorize_url]).to_s + end + + def deauthorize(account) + client_object = self.dup + client_object.options[:stripe_user_id] = account + @deauthorize ||= OAuth2::Strategy::Deauthorize.new(client_object) + end + + def deauthorize_request(params) + headers = params.delete(:headers) + opts = {} + opts[:body] = params + opts[:headers] = {'Content-Type' => 'application/x-www-form-urlencoded'} + opts[:headers].merge!(headers) if headers + request(:post, deauthorize_url, opts) + end + end + + module OAuth2 + module Strategy + # Deauthorization Strategy -- for Stripe + class Deauthorize < Base + # The required query parameters for the authorize URL + # + def deauthorize_params(params = {}) + params.merge({ 'client_id' => @client.id, + 'stripe_user_id' => @client.options[:stripe_user_id] + }) + end + + def deauthorize_url(params = {}) + @client.deauthorize_url(deauthorize_params.merge(params)) + end + + def deauthorize_request(params = {}) + params = params.merge(deauthorize_params).merge(client_params) + @client.deauthorize_request(params) + end + + end + end + end +end diff --git a/lib/oauth2/client.rb b/lib/oauth2/client.rb deleted file mode 100644 index 6790a1f425..0000000000 --- a/lib/oauth2/client.rb +++ /dev/null @@ -1,21 +0,0 @@ -require 'oauth2' -OAuth2::Client.class_eval do - def deauthorize_url(params = nil) - connection.build_url(options[:deauthorize_url]).to_s - end - - def deauthorize(account) - client_object = self.dup - client_object.options[:stripe_user_id] = account - @deauthorize ||= OAuth2::Strategy::Deauthorize.new(client_object) - end - - def deauthorize_request(params) - headers = params.delete(:headers) - opts = {} - opts[:body] = params - opts[:headers] = {'Content-Type' => 'application/x-www-form-urlencoded'} - opts[:headers].merge!(headers) if headers - request(:post, deauthorize_url, opts) - end -end diff --git a/lib/oauth2/strategy/deauthorize.rb b/lib/oauth2/strategy/deauthorize.rb deleted file mode 100644 index 5d4970e4e2..0000000000 --- a/lib/oauth2/strategy/deauthorize.rb +++ /dev/null @@ -1,24 +0,0 @@ -module OAuth2 - module Strategy - # Deauthorization Strategy -- for Stripe - class Deauthorize < Base - # The required query parameters for the authorize URL - # - def deauthorize_params(params = {}) - params.merge({ 'client_id' => @client.id, - 'stripe_user_id' => @client.options[:stripe_user_id] - }) - end - - def deauthorize_url(params = {}) - @client.deauthorize_url(deauthorize_params.merge(params)) - end - - def deauthorize_request(params = {}) - params = params.merge(deauthorize_params).merge(client_params) - @client.deauthorize_request(params) - end - - end - end -end