power-compass 0.5.1 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 719e6a292a665ca861af7bc67fc0e60d9b0bbcc5303b358988450178744568c1
4
- data.tar.gz: 0fdb94018c58431b1bd131fd5c0bc063ab38bcf25d2243fd2408bcc54c2ecbd4
3
+ metadata.gz: e5cf7df2950250c91f36e08150c320557221597db0fedd203a1ab8566c528984
4
+ data.tar.gz: af59dfd4704c1259a1d0325db50392de17287a9ff7e97c70738e0cba40f59507
5
5
  SHA512:
6
- metadata.gz: ac3054cdee5acf69b7f2f4ed71a528940b13728a44568dd800221cb1a11d9028573edf5a491b50fc55b2b59eeed8b01a8ad8320bfec4700fdeea773ea7b5483e
7
- data.tar.gz: 4d9d5785bf1f07fd37dda55fd9e2348735dc7243756d088b43fe13f70f299337c5f0f0302ea4d511c2e36e9f1a6a175939a18cf129a234f406a9eeab24d7f0d8
6
+ metadata.gz: daf30710679b1d2fe384e108c76d06f815a6990b804aece6a7dd9338414caaa2b643694f0b7e3f211c7e06566bf5f45e15b7d2fefcb0c7dbd1e69f84bd306ab6
7
+ data.tar.gz: 479652082ea6161af9c39133eca13124f5e7f6843c42c36465f7c593ceb071cda687715ba0d838e65412fd91036bb59fc1a3921829af1bac8f68bb67c801c6c1
@@ -3,9 +3,6 @@ module Compass
3
3
  #
4
4
  # @private
5
5
  class ApplicationController < ActionController::API
6
- etag { current_context_id }
7
- etag { context_modified_at }
8
-
9
6
  before_action :authenticate
10
7
  before_action :validate_context
11
8
 
@@ -24,17 +21,5 @@ module Compass
24
21
  def current_context_id
25
22
  instance_exec(&Compass.config.context_id)
26
23
  end
27
-
28
- def context_modified_at
29
- instance_exec(&Compass.config.modified_at)
30
- end
31
-
32
- def set_cache_headers(config)
33
- if config
34
- expires_in(*Array(config))
35
- else
36
- expires_now
37
- end
38
- end
39
24
  end
40
25
  end
@@ -1,4 +1,7 @@
1
1
  module Compass
2
+ # Info controller.
3
+ #
4
+ # @private
2
5
  class InfoController < ApplicationController
3
6
  skip_before_action :validate_context
4
7
 
@@ -1,17 +1,18 @@
1
1
  module Compass
2
- # Base compass controller.
2
+ # Menu controller.
3
3
  #
4
4
  # @private
5
5
  class MenuController < ApplicationController
6
+ include Cors
7
+ include ConditionalCache
8
+
6
9
  etag { filter.values.compact }
7
10
  etag { menu_items.maximum(:updated_at) }
8
11
 
9
12
  def index
10
- set_cache_headers(Compass.config.menu.cache)
11
-
12
- return unless stale? menu_items, etag: "compass-menu"
13
-
14
- render json: menu_items, filter: filter
13
+ with_cache(menu_items, Compass.config.menu.cache, etag: "compass-menu") do
14
+ render json: menu_items, filter: filter
15
+ end
15
16
  end
16
17
 
17
18
  private
@@ -1,15 +1,17 @@
1
- require "compass/notification/provider"
2
-
3
1
  module Compass
2
+ # Notification controller.
3
+ #
4
+ # @private
4
5
  class NotificationController < ApplicationController
5
- etag { providers.map(&:hash) }
6
-
7
- def index
8
- set_cache_headers(Compass.config.notification.cache)
6
+ include Cors
7
+ include ConditionalCache
9
8
 
10
- return unless stale?(providers, etag: "compass-notification")
9
+ etag { providers.maximum(:updated_at) }
11
10
 
12
- render json: providers
11
+ def index
12
+ with_cache(providers, Compass.config.notification.cache, etag: "compass-notification") do
13
+ render json: providers
14
+ end
13
15
  end
14
16
 
15
17
  private
@@ -1,7 +1,10 @@
1
- require "compass/search/provider"
2
-
3
1
  module Compass
2
+ # Search controller.
3
+ #
4
+ # @private
4
5
  class SearchController < ApplicationController
6
+ include Cors
7
+
5
8
  def index
6
9
  if params[:q]
7
10
  results = Compass::Search::Provider.global_search(params.require(:q), current_context)
@@ -0,0 +1,27 @@
1
+ module Compass
2
+ # Shared ETag sources and cache header helpers for Compass controllers.
3
+ #
4
+ # @private
5
+ module ConditionalCache
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ etag { current_context_id }
10
+ etag { context_modified_at }
11
+
12
+ private
13
+
14
+ def context_modified_at
15
+ instance_exec(&Compass.config.modified_at)
16
+ end
17
+ end
18
+
19
+ def with_cache(items, config, etag:)
20
+ config ? expires_in(*Array(config)) : expires_now
21
+
22
+ return unless stale? items, etag: etag
23
+
24
+ yield
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,40 @@
1
+ module Compass
2
+ # Shared CORS headers for Compass controllers.
3
+ #
4
+ # @private
5
+ module Cors
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ before_action :set_cors_headers
10
+ skip_before_action :authenticate, only: :cors
11
+ skip_before_action :validate_context, only: :cors
12
+
13
+ private
14
+
15
+ def set_cors_headers
16
+ headers["Access-Control-Allow-Origin"] = request.headers["Origin"] if allowed_origin?
17
+ headers["Access-Control-Allow-Methods"] = "GET, OPTIONS"
18
+ headers["Access-Control-Allow-Headers"] = "Origin, Content-Type, Accept, Authorization, Cache-Control"
19
+ end
20
+
21
+ def allowed_origin?
22
+ return true if request.headers["sec-fetch-site"] == "same-origin"
23
+ return false if request.headers["Origin"].blank?
24
+
25
+ parsed_origin = URI.parse(request.headers["Origin"])
26
+
27
+ Compass.config.backends.any? do |backend|
28
+ backend_uri = URI.parse(backend) rescue nil
29
+ parsed_origin.scheme == backend_uri&.scheme &&
30
+ parsed_origin.host == backend_uri&.host &&
31
+ parsed_origin.port == backend_uri&.port
32
+ end
33
+ rescue URI::InvalidURIError
34
+ false
35
+ end
36
+ end
37
+
38
+ def cors = head :ok
39
+ end
40
+ end
data/config/routes.rb CHANGED
@@ -2,13 +2,19 @@ Compass::Engine.routes.draw do
2
2
  root to: "info#index", as: :root
3
3
 
4
4
  get ":context_id/menu", to: "menu#index", as: :menu
5
+ options ":context_id/menu", to: "menu#cors"
5
6
 
6
7
  get ":context_id/search", to: "search#index", as: :search
8
+ options ":context_id/search", to: "search#cors"
9
+
7
10
  get ":context_id/search/providers", to: "search#providers", as: :search_providers
8
- get ":context_id/search/:provider_name",
9
- to: "search#show",
10
- as: :search_provider,
11
- constraints: { provider_name: /[a-zA-Z_]+/ }
11
+ options ":context_id/search/providers", to: "search#cors"
12
+
13
+ get ":context_id/search/:provider_name", to: "search#show",
14
+ as: :search_provider,
15
+ constraints: { provider_name: /[a-zA-Z_]+/ }
16
+ options ":context_id/search/:provider_name", to: "search#cors"
12
17
 
13
18
  get ":context_id/notifications", to: "notification#index", as: :notifications
19
+ options ":context_id/notifications", to: "notification#cors"
14
20
  end
@@ -18,6 +18,7 @@ module Compass
18
18
  config.paths["app"].skip_eager_load!
19
19
  config.paths.add "app/components", autoload: true
20
20
  config.paths.add "app/components/concerns", autoload: true
21
+ config.paths.add "app/controllers/concerns", autoload: true
21
22
 
22
23
  initializer "compass.logger" do
23
24
  Compass.logger ||= Rails.logger.respond_to?(:tagged) ? Rails.logger.tagged("Compass") : Rails.logger
@@ -1,3 +1,3 @@
1
1
  module Compass
2
- VERSION = "0.5.1"
2
+ VERSION = "0.6.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: power-compass
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carlos Palhares
@@ -98,6 +98,8 @@ files:
98
98
  - app/controllers/compass/menu_controller.rb
99
99
  - app/controllers/compass/notification_controller.rb
100
100
  - app/controllers/compass/search_controller.rb
101
+ - app/controllers/concerns/compass/conditional_cache.rb
102
+ - app/controllers/concerns/compass/cors.rb
101
103
  - config/routes.rb
102
104
  - lib/compass.rb
103
105
  - lib/compass/breadcrumb.rb