request_interceptor 0.1.1 → 0.2.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
  SHA1:
3
- metadata.gz: c71d28cdbf09e7e8cef303d41b8bcfe1f52f55a7
4
- data.tar.gz: 2fa0efa5e19d3975ad6e502f309141e0dc87abaf
3
+ metadata.gz: 78ccd56344e9eeee0f940e9042db9d19f26490fa
4
+ data.tar.gz: 356ededd222ceac8fea4b64ae978994cef14bd6b
5
5
  SHA512:
6
- metadata.gz: 75d941129c251e6c0595d53020b1f6ebce625424e5f9a0605881d07ff3059ba05692bec55011440fb973cdb3378c4253b445f9f05519f967dd75f958d52d4f78
7
- data.tar.gz: 9e254ab6980c329f7d2780f13f7ee6cb341a103f5602a568b1e07a39122b8c8f43bfb81df2687425a9f63cb64b2a0e0440bd5282d340e92c52bd4a8295f86f27
6
+ metadata.gz: b12b0f6b6eb87bfbd3203c1e9087c7681be611932df6dcf1d108975a96b2277e9666b9e2e4addffb96ea82603d5cf103bd024e3d772da4f6f483da69c1c93ecd
7
+ data.tar.gz: 9781bed571b57a806e658d08dd64ce4f93003130c86e4cd9d1b63085787d26b72c70017e184740492dfd7f6faae37db395f65c91074d03d08c0ea63864907542
data/README.md CHANGED
@@ -67,6 +67,60 @@ log.first.request # => Rack::MockRequest
67
67
  log.first.response # => Rack::MockResponse
68
68
  ```
69
69
 
70
+ ### Pre-configured hostnames and interceptor customization
71
+
72
+ Interceptors further support pre-configured hostnames and customization of existing interceptors:
73
+
74
+ ```ruby
75
+ customized_app = app.customize do
76
+ host "example.de"
77
+
78
+ get "/" do
79
+ content_type "text/plain"
80
+ "Hallo Welt"
81
+ end
82
+ end
83
+
84
+ customized_app.intercept do
85
+ response = Net::HTTP.get(URI("http://example.de/")) # => "Hello World"
86
+ response == "Hallo Welt" # => true
87
+ end
88
+ ```
89
+
90
+ These two features are only available for Sinatra based interceptors that inherit from `RequestInterceptor::Application`, which is the default for all interceptors that have been defined using `RequestInterceptor.define` if no other template class through `RequestInterceptor.template=` has been configured.
91
+
92
+ ### Constructor argument forwarding
93
+
94
+ Any arugments provided to the `.intercept` method are forwarded to the interceptor's constructor:
95
+
96
+ ```ruby
97
+ multilingual_app = RequestInterceptor.define do
98
+ host "example.com"
99
+
100
+ attr_reader :language
101
+
102
+ def initialize(language = nil)
103
+ @language = language
104
+ super()
105
+ end
106
+
107
+ get "/" do
108
+ content_type "text/plain"
109
+ language == :de ? "Hallo Welt" : "Hello World"
110
+ end
111
+ end
112
+
113
+ multilingual_app.intercept(:de) do
114
+ response = Net::HTTP.get(URI("http://example.com/"))
115
+ response == "Hallo Welt" # => true
116
+ end
117
+
118
+ multilingual_app.intercept do
119
+ response = Net::HTTP.get(URI("http://example.com/"))
120
+ response = "Hello World" # => true
121
+ end
122
+ ```
123
+
70
124
  ## Contributing
71
125
 
72
126
  Bug reports and pull requests are welcome on GitHub at (t6d/request_interceptor)[https://github.com/t6d/request_interceptor].
@@ -1,6 +1,20 @@
1
1
  require "sinatra/base"
2
2
 
3
3
  class RequestInterceptor::Application < Sinatra::Base
4
+ def self.customize(&customizations)
5
+ RequestInterceptor.define(self, &customizations)
6
+ end
7
+
8
+ def self.intercept(host, *args, &test)
9
+ RequestInterceptor.run(host => self.new(*args), &test)
10
+ end
11
+
12
+ def self.host(host)
13
+ define_singleton_method(:intercept) do |*args, &test|
14
+ super(host, *args, &test)
15
+ end
16
+ end
17
+
4
18
  configure do
5
19
  disable :show_exceptions
6
20
  enable :raise_errors
@@ -1,3 +1,3 @@
1
1
  class RequestInterceptor
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: request_interceptor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Konstantin Tennhard
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2015-12-08 00:00:00.000000000 Z
12
+ date: 2016-05-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sinatra
@@ -138,7 +138,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
138
138
  version: '0'
139
139
  requirements: []
140
140
  rubyforge_project:
141
- rubygems_version: 2.4.5
141
+ rubygems_version: 2.5.1
142
142
  signing_key:
143
143
  specification_version: 4
144
144
  summary: Sinatra based foreign API simulation