lita 4.0.1 → 4.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 82f0c8bba8bcf252debec8df0ab1aee07e239cdc
4
- data.tar.gz: 9d398df86aa81a27c97089dd5367a2b3516c5cbb
3
+ metadata.gz: 587288247dc60338bb91eb3914f9d40d3ca8a59a
4
+ data.tar.gz: d015472e6b10d7cecc199b89e4164d8430297f06
5
5
  SHA512:
6
- metadata.gz: c7fcfae5ea03659d83c477249e2df8a40f013f1e6e927a1bb18993e6c475fc56662b8fc5a74a14b2d8eaf65ead10177c2f98c843db3d62e50ed225cbc14565bc
7
- data.tar.gz: d91453339c92d22a74bc75090b0e03e31cc43909da422c8dc1bb89d86fa78cc1d328bc2f3a8a57e501b70d9b8d7f2dfed9263fffa725bddb612ad76908408d59
6
+ metadata.gz: 506d911b55c472c89625f9de45a6d22054b958ad68092e1d873844e4b453c77ed6c635b5c8447a5405ebec0249e4b21dd7d35ef1393b948802ccbb6a583d68bb
7
+ data.tar.gz: 0ea3074f7d44da4b24f8440ecf909aa9b7b5ee1b1c011f603d57e6f9c97e4b7288d24ac210d042c56b9471990db74aa028034a5b54745f27e9f17054bc683b9e
@@ -82,6 +82,7 @@ require_relative "lita/common"
82
82
  require_relative "lita/errors"
83
83
  require_relative "lita/config"
84
84
  require_relative "lita/configuration_builder"
85
+ require_relative "lita/middleware_registry"
85
86
  require_relative "lita/default_configuration"
86
87
  require_relative "lita/configuration_validator"
87
88
  require_relative "lita/util"
@@ -45,7 +45,7 @@ module Lita
45
45
  @robot = robot
46
46
  ensure_required_configs
47
47
  end
48
- #
48
+
49
49
  # The adapter's configuration object.
50
50
  # @return [Lita::Configuration] The adapter's configuration object.
51
51
  # @since 4.0.0
@@ -98,6 +98,13 @@ module Lita
98
98
  end
99
99
  end
100
100
 
101
+ # The Lita logger.
102
+ # @return [Lita::Logger] The Lita logger.
103
+ # @since 4.0.2
104
+ def log
105
+ Lita.logger
106
+ end
107
+
101
108
  # Formats a name for "mentioning" a user in a group chat. Override this
102
109
  # method in child classes to customize the mention format for the chat
103
110
  # service.
@@ -26,7 +26,7 @@ module Lita
26
26
  robot_config
27
27
  end
28
28
 
29
- # Processes the {Lita::ConfigurationBuilder} object to return a {Lita:Configuration}.
29
+ # Processes the {Lita::ConfigurationBuilder} object to return a {Lita::Configuration}.
30
30
  # @return [Lita::Configuration] The built configuration object.
31
31
  def build
32
32
  final_config = root.build
@@ -114,7 +114,7 @@ module Lita
114
114
  config :port, type: [Integer, String], default: 8080
115
115
  config :min_threads, type: [Integer, String], default: 0
116
116
  config :max_threads, type: [Integer, String], default: 16
117
- config :middleware, type: Array, default: []
117
+ config :middleware, type: MiddlewareRegistry, default: MiddlewareRegistry.new
118
118
  end
119
119
  end
120
120
 
@@ -0,0 +1,35 @@
1
+ module Lita
2
+ # Stores Rack middleware for later use in a +Rack::Builder+.
3
+ # @since 4.0.2
4
+ # @api private
5
+ class MiddlewareRegistry
6
+ # A Rack middleware and its initialization arguments.
7
+ class MiddlewareWrapper < Struct.new(:middleware, :args, :block); end
8
+
9
+ extend Forwardable
10
+
11
+ def_delegators :@registry, :each, :empty?
12
+
13
+ def initialize
14
+ @registry = []
15
+ end
16
+
17
+ # Adds a Rack middleware with no initialization arguments.
18
+ # @param middleware [#call] A Rack middleware.
19
+ # @return [void]
20
+ def push(middleware)
21
+ @registry << MiddlewareWrapper.new(middleware, [], nil)
22
+ end
23
+ alias_method :<<, :push
24
+
25
+ # Adds a Rack middleware with initialization argumens. Uses the same interface as
26
+ # +Rack::Builder#use+.
27
+ # @param middleware [#call] A Rack middleware.
28
+ # @param args [Array] Arbitrary initialization arguments for the middleware.
29
+ # @yield An optional block to be passed to the constructor of the middleware.
30
+ # @return [void]
31
+ def use(middleware, *args)
32
+ @registry << MiddlewareWrapper.new(middleware, args, (proc if block_given?))
33
+ end
34
+ end
35
+ end
@@ -15,7 +15,15 @@ module Lita
15
15
  def self.build(robot)
16
16
  builder = Rack::Builder.new
17
17
  builder.run(new(robot))
18
- robot.config.http.middleware.each { |middleware| builder.use(middleware) }
18
+
19
+ robot.config.http.middleware.each do |wrapper|
20
+ if wrapper.block
21
+ builder.use(wrapper.middleware, *wrapper.args, &wrapper.block)
22
+ else
23
+ builder.use(wrapper.middleware, *wrapper.args)
24
+ end
25
+ end
26
+
19
27
  builder.to_app
20
28
  end
21
29
 
@@ -1,4 +1,4 @@
1
1
  module Lita
2
2
  # The current version of Lita.
3
- VERSION = "4.0.1"
3
+ VERSION = "4.0.2"
4
4
  end
@@ -61,6 +61,12 @@ describe Lita::Adapter, lita: true do
61
61
  end
62
62
  end
63
63
 
64
+ describe "#log" do
65
+ it "returns the Lita logger" do
66
+ expect(subject.log).to eq(Lita.logger)
67
+ end
68
+ end
69
+
64
70
  describe "#mention_format" do
65
71
  it "formats the provided name for mentioning the user" do
66
72
  expect(subject.mention_format("carl")).to eq("carl:")
@@ -142,7 +142,17 @@ describe Lita::DefaultConfiguration, lita: true do
142
142
 
143
143
  config.http.middleware.push(middleware)
144
144
 
145
- expect(config.http.middleware).to include(middleware)
145
+ expect(config.http.middleware).not_to be_empty
146
+ end
147
+
148
+ it "can add middleware with arguments" do
149
+ middleware = double("a rack middleware")
150
+
151
+ config.http.middleware.use(middleware, "argument") do
152
+ "block"
153
+ end
154
+
155
+ expect(config.http.middleware).not_to be_empty
146
156
  end
147
157
  end
148
158
 
@@ -37,6 +37,7 @@ handler = Class.new do
37
37
  end
38
38
 
39
39
  def middleware(request, response)
40
+ response["Custom-Header"] = request.env["header_value"] if request.env["use_header"]
40
41
  response.write("middleware worked") if request.env["custom_rack_middleware_working"]
41
42
  end
42
43
  end
@@ -104,3 +105,30 @@ describe handler, lita_handler: true do
104
105
  expect(response.body).to eq("middleware worked")
105
106
  end
106
107
  end
108
+
109
+ describe handler, lita_handler: true do
110
+ let(:middleware) do
111
+ Class.new do
112
+ def initialize(app, use_header = false, &block)
113
+ @app = app
114
+ @use_header = use_header
115
+ @block = block
116
+ end
117
+
118
+ def call(env)
119
+ env["use_header"] = @use_header
120
+ env["header_value"] = @block.call
121
+ @app.call(env)
122
+ end
123
+ end
124
+ end
125
+
126
+ prepend_before do
127
+ registry.config.http.middleware.use(middleware, true) { "header value" }
128
+ end
129
+
130
+ it "uses any custom middlewares registered" do
131
+ response = http.get("/middleware")
132
+ expect(response["Custom-Header"]).to eq("header value")
133
+ end
134
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lita
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.1
4
+ version: 4.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jimmy Cuadra
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-23 00:00:00.000000000 Z
11
+ date: 2014-11-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -307,6 +307,7 @@ files:
307
307
  - lib/lita/http_route.rb
308
308
  - lib/lita/logger.rb
309
309
  - lib/lita/message.rb
310
+ - lib/lita/middleware_registry.rb
310
311
  - lib/lita/namespace.rb
311
312
  - lib/lita/plugin_builder.rb
312
313
  - lib/lita/rack_app.rb