Rails ::如何加载lib /文件夹中的代码?

时间:2018-01-30 16:27:29

标签: ruby-on-rails ruby shopify-app

我的文件夹是:

app/
-  -controllers/
-        -shopify_app/
-               -webhooks_controller.rb
lib/
-  -shopify_app/
-        -controller_concerns/
-               -webhook_verification.rb

webhook_verification.rb代码为:

  module ShopifyApp
    module WebhookVerification
      extend ActiveSupport::Concern

      included do
        skip_before_action :verify_authenticity_token, raise: false
        before_action :verify_request
      end

      private

      def verify_request
        data = request.raw_post
        return head :unauthorized unless hmac_valid?(data)
      end

      def hmac_valid?(data)
        secret = ShopifyApp.configuration.secret
        digest = OpenSSL::Digest.new('sha256')
        ActiveSupport::SecurityUtils.secure_compare(
          shopify_hmac,
          Base64.encode64(OpenSSL::HMAC.digest(digest, secret, data)).strip
        )
      end

      def shop_domain
        request.headers['HTTP_X_SHOPIFY_SHOP_DOMAIN']
      end

      def shopify_hmac
        request.headers['HTTP_X_SHOPIFY_HMAC_SHA256']
      end
    end
  end

我想在webhooks_controller.rb中包含webhook_verification.rb “include ShopifyApp :: WebhookVerification”,但它不起作用。

如何加载lib文件夹?

注意:我已尝试在我的config / application.rb上写这个但是它似乎不起作用

config.autoload_paths += %W(#{config.root}/lib)
config.autoload_paths += Dir["#{config.root}/lib/**/"]

0 个答案:

没有答案