apicraft-rails 0.5.1.beta1 → 0.5.2.beta1

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
  SHA256:
3
- metadata.gz: 03c78edc81f87417dfdd7411be50609da4d39bb0827775ea16b609cce641a7bd
4
- data.tar.gz: 652eb5761da35bc28f8a779015c74ef0b971d94231250aa02edcd3c3ff1cc7b3
3
+ metadata.gz: 5411b6d7fec8f1e73a0827f8809c53212ff39f0c97d94bd5ed54825e84a81882
4
+ data.tar.gz: b4d2b5001f2aca8c32b5ac510e9b67a749e2eb5fc199ed89eaaaa42135b598df
5
5
  SHA512:
6
- metadata.gz: fe96cef636e07bc57e26ff11401e315ab457783cf254f33de98ec233a8f2c4b72be7c4b4f593f023deb615b17ce5166f8309fabc6a298139913d58e478363489
7
- data.tar.gz: f23638693ebe922a7de1b98bfef20f7cad9777704c21603489ffe6677afc33ec9020173a2cfdb76dd59c52e7246a42dbb55fcd2443f9ff7fa4946a6764c84dd6
6
+ metadata.gz: 46cf0e60bd1d0f7551c80f118af6d8c421c8d6485252dc92a2b58d75c02f052c6c3216ac976ecb46f636fee72946c8f9662ed3af7b243575a5d716bcf5c6d40a
7
+ data.tar.gz: 5e77011472cb349e45f84131e9ef15e17b22a13576b2cb3b5f40b2311d191bede0c36bcec654932527e4e7e1c7006b3a12cd7967b8a42c9cd47865efedbbfb18
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
- # APICraft Rails (Beta)
1
+ # APICraft Rails
2
2
  [![Build](https://github.com/apicraft-dev/apicraft-rails/actions/workflows/build.yml/badge.svg)](https://github.com/apicraft-dev/apicraft-rails/actions/workflows/build.yml)
3
- [![Gem Version](https://badge.fury.io/rb/apicraft-rails.svg)](https://badge.fury.io/rb/apicraft-rails)
3
+ [![Gem Version](https://badge.fury.io/rb/apicraft-rails.svg?v=0.5.2.beta1)](https://badge.fury.io/rb/apicraft-rails)
4
4
 
5
5
  🚀 Accelerates your development by 2-3x with an API Design First approach. Seamlessly integrates with your Rails application server — no fancy tooling or expenses required.
6
6
 
@@ -72,7 +72,7 @@ By adopting an API Design First approach with APICraft Rails, you can accelerate
72
72
  Add this line to your application's Gemfile:
73
73
 
74
74
  ```ruby
75
- gem 'apicraft-rails', '~> 0.5.1.beta1'
75
+ gem 'apicraft-rails', '~> 0.5.2.beta1'
76
76
  ```
77
77
 
78
78
  And then execute:
@@ -143,12 +143,14 @@ headers: {
143
143
 
144
144
  ### 🎮 API Mocking (Behaviours)
145
145
  The above is an example of a 200 response. If you have more responses documented you can force that behaviour using `Apicraft-Response-Code` header in the mock request.
146
+ You can find a list of all the supported headers in the [configuration section](#-configuration) that would allow you to manipulate the API Behaviour.
146
147
 
147
148
  `https://yoursite.com/api/orders`
148
149
  ```
149
150
  headers: {
150
151
  Apicraft-Response-Code: 400
151
152
  Apicraft-Mock: true
153
+ Apicraft-Delay: 5
152
154
  }
153
155
  ```
154
156
  ```json
@@ -223,6 +225,9 @@ module App
223
225
  end
224
226
  end
225
227
  ```
228
+ RapiDoc | SwaggerDoc
229
+ :-------------------------:|:-------------------------:
230
+ ![](assets/rapidoc.png) | ![](assets/swaggerdoc.png)
226
231
 
227
232
  ## 🔧 Configuration
228
233
 
@@ -249,6 +254,10 @@ Apicraft.configure do |config|
249
254
  # Defaults to true
250
255
  config.strict_reference_validation = true
251
256
 
257
+ # When simulating delay using the mocks, the max
258
+ # delay in seconds that can be simulated
259
+ config.max_allowed_delay = 30
260
+
252
261
  config.headers = {
253
262
  # The name of the header used to control
254
263
  # the response code of the mock
@@ -257,11 +266,14 @@ Apicraft.configure do |config|
257
266
 
258
267
  # The name of the header to introspect the API.
259
268
  # Defaults to Apicraft-Introspect
260
- introspect: "Apicraft-Introspect"
269
+ introspect: "Apicraft-Introspect",
261
270
 
262
271
  # The name of the header to mock the API.
263
272
  # Defaults to Apicraft-Mock
264
- mock: "Apicraft-Mock"
273
+ mock: "Apicraft-Mock",
274
+
275
+ # Delay simulation header name
276
+ delay: "Apicraft-Delay"
265
277
  }
266
278
  end
267
279
 
@@ -13,12 +13,14 @@ module Apicraft
13
13
  response_code: "Apicraft-Response-Code",
14
14
  introspect: "Apicraft-Introspect",
15
15
  mock: "Apicraft-Mock",
16
+ delay: "Apicraft-Delay",
16
17
  content_type: "Content-Type"
17
18
  },
18
19
  mocks: true,
19
20
  introspection: true,
20
21
  strict_reference_validation: true,
21
- request_validations: true
22
+ request_validations: true,
23
+ max_allowed_delay: 30
22
24
  }.with_indifferent_access
23
25
 
24
26
  def initialize(opts = {})
@@ -47,6 +49,10 @@ module Apicraft
47
49
  @opts[:introspection]
48
50
  end
49
51
 
52
+ def max_allowed_delay
53
+ @opts[:max_allowed_delay]
54
+ end
55
+
50
56
  def contracts_path=(contracts_path)
51
57
  @opts[:contracts_path] = contracts_path
52
58
  end
@@ -67,6 +73,10 @@ module Apicraft
67
73
  @opts[:request_validations] = enabled
68
74
  end
69
75
 
76
+ def max_allowed_delay=(enabled)
77
+ @opts[:max_allowed_delay] = enabled
78
+ end
79
+
70
80
  def headers=(headers)
71
81
  @opts[:headers] = @opts[:headers].merge(
72
82
  headers.with_indifferent_access
@@ -7,5 +7,6 @@ module Apicraft
7
7
  class InvalidContract < StandardError; end
8
8
  class InvalidOperation < StandardError; end
9
9
  class InvalidResponse < StandardError; end
10
+ class DelayTooHigh < StandardError; end
10
11
  end
11
12
  end
@@ -15,15 +15,9 @@ module Apicraft
15
15
  request = ActionDispatch::Request.new(env)
16
16
  return @app.call(env) unless mock?(request)
17
17
 
18
- contract = Apicraft::Openapi::Contract.find_by_operation(
19
- request.method, request.path_info
20
- )
21
- raise Errors::InvalidContract if contract.blank?
22
-
23
- operation = contract.operation(
24
- request.method, request.path_info
25
- )
26
- raise Errors::InvalidOperation if operation.blank?
18
+ use_delay!(request)
19
+ contract = find_contract!(request)
20
+ operation = find_operation!(contract, request)
27
21
 
28
22
  code = request.headers[config.headers[:response_code]] || "200"
29
23
  response = operation.response_for(code.to_s)
@@ -64,6 +58,35 @@ module Apicraft
64
58
  def mock?(request)
65
59
  request.headers[config.headers[:mock]].present?
66
60
  end
61
+
62
+ def find_contract!(request)
63
+ contract = Apicraft::Openapi::Contract.find_by_operation(
64
+ request.method, request.path_info
65
+ )
66
+ raise Errors::InvalidContract if contract.blank?
67
+
68
+ contract
69
+ end
70
+
71
+ def find_operation!(contract, request)
72
+ operation = contract.operation(
73
+ request.method, request.path_info
74
+ )
75
+ raise Errors::InvalidOperation if operation.blank?
76
+
77
+ operation
78
+ end
79
+
80
+ def use_delay!(request)
81
+ request_delay = delay(request)
82
+ raise Errors::DelayTooHigh if request_delay > config.max_allowed_delay
83
+
84
+ sleep(request_delay)
85
+ end
86
+
87
+ def delay(request)
88
+ request.headers[config.headers[:delay]].to_i
89
+ end
67
90
  end
68
91
  end
69
92
  end
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Current version of Apicraft.
4
4
  module Apicraft
5
- VERSION = "0.5.1.beta1"
5
+ VERSION = "0.5.2.beta1"
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: apicraft-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1.beta1
4
+ version: 0.5.2.beta1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Abhishek Sarkar
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-09-10 00:00:00.000000000 Z
11
+ date: 2024-09-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport