fakeit 0.7.0 → 0.9.1

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: 60b523cb9a2c8cea3454b35cace650d51f2bf8e2ed8e53e07b0f35ffe45c9288
4
- data.tar.gz: a3340dafc8607d5de8e9cb7f00c9b60679e7f84bdd2161f8d8607dad2e6089b3
3
+ metadata.gz: f48fecd4f45a0532ec3dd35f088d2b365c3fbbee815d7722bddc59c926088cce
4
+ data.tar.gz: 1a8e59d4f67a4e952f7c31052815786b19884b5feed18ad281f6133ae585de1a
5
5
  SHA512:
6
- metadata.gz: cfab31deab5494ae69b2adeecec85efff46d69104ad71ce0d637e4809a492bb7ac45062d8a73e5cd4adb34ad347ac798fecd05f4a1b3b00bab37a3b44adab511
7
- data.tar.gz: c4ef907db3d2df3de0fed6bf5a54b904969b080f312951665b6cfe863c4c10ecf2f3d1e7bf5b712d668878aa2b9b3b1dda7539da927ac9cabea005fec2528509
6
+ metadata.gz: 3685b6808f4b614c78e76f443e0698fbd01b2559b67bae344fc3bd9cf7a09b66ace980f36beb7837fd6a5263134c270695b568c63da6812ed613b1c667701710
7
+ data.tar.gz: dc831a3a34658853679da5a0b5cfa54bbb947bc9a7ee11e0dc2c830f22b077d7534fe287633142e95db4782e8652c6ba24903d92a21a2752e4366e8695a88e5a
data/.circleci/config.yml CHANGED
@@ -12,7 +12,7 @@ jobs:
12
12
 
13
13
  docker:
14
14
  # specify the version you desire here
15
- - image: circleci/ruby:2.7.0
15
+ - image: circleci/ruby:3.0.0
16
16
 
17
17
  # Specify service dependencies here if necessary
18
18
  # CircleCI maintains a library of pre-built images
data/.rubocop.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  AllCops:
2
- TargetRubyVersion: 2.7
2
+ TargetRubyVersion: 3.0
3
3
  NewCops: enable
4
4
 
5
5
  Style/Documentation:
data/CHANGELOG.md CHANGED
@@ -1,5 +1,46 @@
1
1
  # Changelog
2
2
 
3
+ ## [v0.9.0](https://github.com/JustinFeng/fakeit/tree/v0.9.0) (2021-03-18)
4
+
5
+ [Full Changelog](https://github.com/JustinFeng/fakeit/compare/v0.8.1...v0.9.0)
6
+
7
+ **Implemented enhancements:**
8
+
9
+ - Experimental feature to support plain text and binary response, e.g. application/pdf
10
+
11
+ ## [v0.8.1](https://github.com/JustinFeng/fakeit/tree/v0.8.1) (2021-03-01)
12
+
13
+ [Full Changelog](https://github.com/JustinFeng/fakeit/compare/v0.8.0...v0.8.1)
14
+
15
+ **Fixed bugs:**
16
+
17
+ - Cannot load spec file from url [\#21](https://github.com/JustinFeng/fakeit/issues/21)
18
+
19
+ ## [v0.8.0](https://github.com/JustinFeng/fakeit/tree/v0.8.0) (2021-02-27)
20
+
21
+ [Full Changelog](https://github.com/JustinFeng/fakeit/compare/v0.7.1...v0.8.0)
22
+
23
+ **Implemented enhancements:**
24
+
25
+ - Upgraded to ruby 3.0.0
26
+
27
+ ## [v0.7.1](https://github.com/JustinFeng/fakeit/tree/v0.7.1) (2021-02-07)
28
+
29
+ [Full Changelog](https://github.com/JustinFeng/fakeit/compare/v0.7.0...v0.7.1)
30
+
31
+ **Fixed bugs:**
32
+
33
+ - ERROR NoMethodError: undefined method `to\_example' for \# [\#20](https://github.com/JustinFeng/fakeit/issues/20)
34
+
35
+ ## [v0.7.0](https://github.com/JustinFeng/fakeit/tree/v0.7.0) (2020-10-22)
36
+
37
+ [Full Changelog](https://github.com/JustinFeng/fakeit/compare/v0.6.3...v0.7.0)
38
+
39
+ **Implemented enhancements:**
40
+
41
+ - Supported change mock server behaviour on the fly
42
+ - Allowed to redirect console log to a file
43
+
3
44
  ## [v0.6.3](https://github.com/JustinFeng/fakeit/tree/v0.6.3) (2020-09-11)
4
45
 
5
46
  [Full Changelog](https://github.com/JustinFeng/fakeit/compare/v0.6.2...v0.6.3)
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- fakeit (0.7.0)
4
+ fakeit (0.9.1)
5
5
  faker (= 2.13.0)
6
6
  openapi_parser (= 0.12.1)
7
7
  rack (~> 2.0)
@@ -9,22 +9,23 @@ PATH
9
9
  rainbow (~> 3.0)
10
10
  regexp-examples (= 1.5.1)
11
11
  slop (~> 4.8)
12
+ webrick (~> 1.7)
12
13
 
13
14
  GEM
14
15
  remote: https://rubygems.org/
15
16
  specs:
16
- ast (2.4.1)
17
+ ast (2.4.2)
17
18
  byebug (11.1.3)
18
- concurrent-ruby (1.1.7)
19
+ concurrent-ruby (1.1.8)
19
20
  diff-lcs (1.4.4)
20
- docile (1.3.2)
21
+ docile (1.3.5)
21
22
  faker (2.13.0)
22
23
  i18n (>= 1.6, < 2)
23
- i18n (1.8.5)
24
+ i18n (1.8.9)
24
25
  concurrent-ruby (~> 1.0)
25
26
  openapi_parser (0.12.1)
26
- parallel (1.19.2)
27
- parser (2.7.1.4)
27
+ parallel (1.20.1)
28
+ parser (3.0.0.0)
28
29
  ast (~> 2.4.1)
29
30
  rack (2.2.3)
30
31
  rack-cors (1.1.1)
@@ -32,41 +33,46 @@ GEM
32
33
  rack-test (1.1.0)
33
34
  rack (>= 1.0, < 3)
34
35
  rainbow (3.0.0)
35
- rake (12.3.3)
36
+ rake (13.0.3)
36
37
  regexp-examples (1.5.1)
37
- regexp_parser (1.7.1)
38
+ regexp_parser (2.1.1)
38
39
  rexml (3.2.4)
39
- rspec (3.9.0)
40
- rspec-core (~> 3.9.0)
41
- rspec-expectations (~> 3.9.0)
42
- rspec-mocks (~> 3.9.0)
43
- rspec-core (3.9.2)
44
- rspec-support (~> 3.9.3)
45
- rspec-expectations (3.9.2)
40
+ rspec (3.10.0)
41
+ rspec-core (~> 3.10.0)
42
+ rspec-expectations (~> 3.10.0)
43
+ rspec-mocks (~> 3.10.0)
44
+ rspec-core (3.10.1)
45
+ rspec-support (~> 3.10.0)
46
+ rspec-expectations (3.10.1)
46
47
  diff-lcs (>= 1.2.0, < 2.0)
47
- rspec-support (~> 3.9.0)
48
- rspec-mocks (3.9.1)
48
+ rspec-support (~> 3.10.0)
49
+ rspec-mocks (3.10.2)
49
50
  diff-lcs (>= 1.2.0, < 2.0)
50
- rspec-support (~> 3.9.0)
51
- rspec-support (3.9.3)
52
- rubocop (0.90.0)
51
+ rspec-support (~> 3.10.0)
52
+ rspec-support (3.10.2)
53
+ rubocop (1.10.0)
53
54
  parallel (~> 1.10)
54
- parser (>= 2.7.1.1)
55
+ parser (>= 3.0.0.0)
55
56
  rainbow (>= 2.2.2, < 4.0)
56
- regexp_parser (>= 1.7)
57
+ regexp_parser (>= 1.8, < 3.0)
57
58
  rexml
58
- rubocop-ast (>= 0.3.0, < 1.0)
59
+ rubocop-ast (>= 1.2.0, < 2.0)
59
60
  ruby-progressbar (~> 1.7)
60
- unicode-display_width (>= 1.4.0, < 2.0)
61
- rubocop-ast (0.3.0)
62
- parser (>= 2.7.1.4)
63
- ruby-progressbar (1.10.1)
64
- simplecov (0.19.0)
61
+ unicode-display_width (>= 1.4.0, < 3.0)
62
+ rubocop-ast (1.4.1)
63
+ parser (>= 2.7.1.5)
64
+ rubocop-rake (0.5.1)
65
+ rubocop
66
+ ruby-progressbar (1.11.0)
67
+ simplecov (0.21.2)
65
68
  docile (~> 1.1)
66
69
  simplecov-html (~> 0.11)
67
- simplecov-html (0.12.2)
70
+ simplecov_json_formatter (~> 0.1)
71
+ simplecov-html (0.12.3)
72
+ simplecov_json_formatter (0.1.2)
68
73
  slop (4.8.2)
69
- unicode-display_width (1.7.0)
74
+ unicode-display_width (2.0.0)
75
+ webrick (1.7.0)
70
76
 
71
77
  PLATFORMS
72
78
  ruby
@@ -76,10 +82,11 @@ DEPENDENCIES
76
82
  byebug (~> 11.0)
77
83
  fakeit!
78
84
  rack-test (~> 1.1)
79
- rake (~> 12.0)
85
+ rake (~> 13.0)
80
86
  rspec (~> 3.0)
81
- rubocop (~> 0.80)
87
+ rubocop (~> 1.10)
88
+ rubocop-rake (~> 0.5)
82
89
  simplecov (~> 0.18)
83
90
 
84
91
  BUNDLED WITH
85
- 2.1.4
92
+ 2.2.15
data/README.md CHANGED
@@ -35,6 +35,7 @@ After tried several existing options, we cannot find a best solution to meet all
35
35
  * Load specification from local or remote
36
36
  * Support hot reload local specification
37
37
  * Support specification in yaml or json format
38
+ * [Experimental] Support plain text and binary response, e.g. `application/pdf`
38
39
 
39
40
  ## Installation
40
41
 
@@ -66,6 +67,12 @@ Or use the [docker image](https://hub.docker.com/r/realfengjia/fakeit)
66
67
  -v, --version
67
68
  -h, --help
68
69
 
70
+ **Notes:**
71
+ * See [here](docs/random.md) for Openapi properties supported in random response generation
72
+ * See [here](docs/static.md) for default value in static response generation
73
+ * Regarding `--use-example` mode, property without example specified will still be randomly or statically generated
74
+ * Random response generation can not handle recursive schema reference. If you do need it in your spec file, please provide `example` property for the recursive part of schema and specify `--use-example` option.
75
+
69
76
  ### Configuration endpoint
70
77
 
71
78
  Mock server behaviour can be changed on the fly
@@ -81,20 +88,18 @@ Update config:
81
88
  Request and response:
82
89
  ```json
83
90
  {
84
- 'permissive' => false,
85
- 'use_example' => true,
86
- 'static' => false,
87
- 'static_types' => ['integer'],
88
- 'static_properties' => ['id']
91
+ "permissive": false,
92
+ "use_example": true,
93
+ "static": false,
94
+ "static_types": [
95
+ "integer"
96
+ ],
97
+ "static_properties": [
98
+ "id"
99
+ ]
89
100
  }
90
101
  ```
91
102
 
92
- **Notes:**
93
- * See [here](docs/random.md) for Openapi properties supported in random response generation
94
- * See [here](docs/static.md) for default value in static response generation
95
- * Regarding `--use-example` mode, property without example specified will still be randomly or statically generated
96
- * Random response generation can not handle recursive schema reference. If you do need it in your spec file, please provide `example` property for the recursive part of schema and specify `--use-example` option.
97
-
98
103
  ## Development
99
104
 
100
105
  After checking out the repo, run `bundle install` to install dependencies. Then, run `rake` to run the tests.
data/fakeit.gemspec CHANGED
@@ -22,14 +22,15 @@ Gem::Specification.new do |spec|
22
22
  spec.executables = 'fakeit'
23
23
  spec.require_paths = ['lib']
24
24
 
25
- spec.required_ruby_version = '>= 2.7.0'
25
+ spec.required_ruby_version = '>= 3.0.0'
26
26
 
27
27
  spec.add_development_dependency 'bundler', '~> 2.0'
28
28
  spec.add_development_dependency 'byebug', '~> 11.0'
29
29
  spec.add_development_dependency 'rack-test', '~> 1.1'
30
- spec.add_development_dependency 'rake', '~> 12.0'
30
+ spec.add_development_dependency 'rake', '~> 13.0'
31
31
  spec.add_development_dependency 'rspec', '~> 3.0'
32
- spec.add_development_dependency 'rubocop', '~> 0.80'
32
+ spec.add_development_dependency 'rubocop', '~> 1.10'
33
+ spec.add_development_dependency 'rubocop-rake', '~> 0.5'
33
34
  spec.add_development_dependency 'simplecov', '~> 0.18'
34
35
 
35
36
  spec.add_dependency 'faker', '2.13.0'
@@ -39,4 +40,5 @@ Gem::Specification.new do |spec|
39
40
  spec.add_dependency 'rainbow', '~> 3.0'
40
41
  spec.add_dependency 'regexp-examples', '1.5.1'
41
42
  spec.add_dependency 'slop', '~> 4.8'
43
+ spec.add_dependency 'webrick', '~> 1.7'
42
44
  end
data/lib/fakeit.rb CHANGED
@@ -9,7 +9,7 @@ require 'rack'
9
9
  require 'logger'
10
10
  require 'rainbow'
11
11
 
12
- Dir.glob(File.join(File.dirname(__FILE__), 'fakeit', '**/*.rb')).sort.each { require _1 }
12
+ Dir.glob(File.join(File.dirname(__FILE__), 'fakeit', '**/*.rb')).each { require _1 }
13
13
 
14
14
  module Fakeit
15
15
  class << self
@@ -23,7 +23,7 @@ module Fakeit
23
23
  end
24
24
 
25
25
  def parse_form_data(params)
26
- params.transform_values { |v| v.class == Hash && v[:tempfile] ? v[:tempfile].read : v }
26
+ params.transform_values { |v| v.instance_of?(Hash) && v[:tempfile] ? v[:tempfile].read : v }
27
27
  end
28
28
  end
29
29
  end
@@ -3,25 +3,15 @@ module Fakeit
3
3
  module Helpers
4
4
  class ResponseBuilder
5
5
  class << self
6
- def error(code, err)
7
- [code, { 'Content-Type' => 'application/json' }, [{ message: err.message }.to_json]]
8
- end
6
+ def error(code, err) = [code, { 'Content-Type' => 'application/json' }, [{ message: err.message }.to_json]]
9
7
 
10
- def not_found
11
- [404, {}, ['Not Found']]
12
- end
8
+ def not_found = [404, {}, ['Not Found']]
13
9
 
14
- def method_not_allowed
15
- [405, {}, ['Method Not Allowed']]
16
- end
10
+ def method_not_allowed = [405, {}, ['Method Not Allowed']]
17
11
 
18
- def unsupported_media_type
19
- [415, {}, ['Unsupported Media Type']]
20
- end
12
+ def unsupported_media_type = [415, {}, ['Unsupported Media Type']]
21
13
 
22
- def ok(body)
23
- [200, { 'Content-Type' => 'application/json' }, [body.to_json]]
24
- end
14
+ def ok(body) = [200, { 'Content-Type' => 'application/json' }, [body.to_json]]
25
15
  end
26
16
  end
27
17
  end
@@ -4,9 +4,7 @@ module Fakeit
4
4
  class ConfigRoute
5
5
  attr_reader :options
6
6
 
7
- def initialize(options)
8
- @options = options
9
- end
7
+ def initialize(options) = @options = options
10
8
 
11
9
  def call(request)
12
10
  case [request.request_method, request.media_type]
@@ -2,9 +2,7 @@ module Fakeit
2
2
  module App
3
3
  module Routes
4
4
  class OpenapiRoute
5
- def initialize(spec_file)
6
- @specification = Fakeit::Openapi::Specification.new(spec_file)
7
- end
5
+ def initialize(spec_file) = @specification = Fakeit::Openapi::Specification.new(spec_file)
8
6
 
9
7
  def call(request, options)
10
8
  @specification
@@ -22,9 +20,7 @@ module Fakeit
22
20
  options.permissive ? response(operation) : Fakeit::App::Helpers::ResponseBuilder.error(418, e)
23
21
  end
24
22
 
25
- def response(operation)
26
- [operation.status, operation.headers, [operation.body]]
27
- end
23
+ def response(operation) = [operation.status, operation.headers, [operation.body]]
28
24
 
29
25
  def validate(operation, request)
30
26
  operation.validate(
@@ -0,0 +1,12 @@
1
+ module OpenAPIParser
2
+ class SchemaValidator
3
+ class ArrayValidator
4
+ alias old_impl coerce_and_validate
5
+
6
+ def coerce_and_validate(value, schema, **keyword_args)
7
+ coerced_value = schema.parent.is_a?(OpenAPIParser::Schemas::Parameter) ? [*value] : value
8
+ old_impl(coerced_value, schema, **keyword_args)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,9 @@
1
+ require 'fakeit/openapi/schema'
2
+
3
+ module OpenAPIParser
4
+ module Schemas
5
+ class Reference
6
+ def to_example(_) = raise(Fakeit::Openapi::ReferenceError, "Invalid $ref at \"#{ref}\"")
7
+ end
8
+ end
9
+ end
@@ -7,9 +7,7 @@ module OpenAPIParser
7
7
 
8
8
  alias old_type type
9
9
 
10
- def type
11
- old_type || inferred_type
12
- end
10
+ def type = old_type || inferred_type
13
11
 
14
12
  private
15
13
 
@@ -1,9 +1,7 @@
1
1
  module Fakeit
2
2
  module Middleware
3
3
  class Recorder
4
- def initialize(app)
5
- @app = app
6
- end
4
+ def initialize(app) = @app = app
7
5
 
8
6
  def call(env)
9
7
  env
@@ -20,9 +18,7 @@ module Fakeit
20
18
  &.tap { |body| body.rewind }
21
19
  end
22
20
 
23
- def log_response(response)
24
- Logger.info("Response body: #{response[2].first}")
25
- end
21
+ def log_response(response) = Logger.info("Response body: #{response[2].first}")
26
22
  end
27
23
  end
28
24
  end
@@ -33,25 +33,15 @@ module Fakeit
33
33
  end
34
34
  end
35
35
 
36
- def add_depth(example_options)
37
- { **example_options, depth: example_options[:depth] + 1 }
38
- end
36
+ def add_depth(example_options) = { **example_options, depth: example_options[:depth] + 1 }
39
37
 
40
- def need_retry?(item, result, retries)
41
- uniqueItems && result.include?(item) && retries.positive?
42
- end
38
+ def need_retry?(item, result, retries) = uniqueItems && result.include?(item) && retries.positive?
43
39
 
44
- def non_empty_size
45
- [min_array, 1].max
46
- end
40
+ def non_empty_size = [min_array, 1].max
47
41
 
48
- def min_array
49
- minItems || 1
50
- end
42
+ def min_array = minItems || 1
51
43
 
52
- def max_array(depth)
53
- maxItems || min_array + (depth > 1 ? 2 : 9)
54
- end
44
+ def max_array(depth) = maxItems || min_array + (depth > 1 ? 2 : 9)
55
45
  end
56
46
  end
57
47
  end
@@ -29,21 +29,13 @@ module Fakeit
29
29
  end
30
30
  end
31
31
 
32
- def int_rand_begin
33
- min_int / int_multiple + int_rand_begin_adjust
34
- end
32
+ def int_rand_begin = min_int / int_multiple + int_rand_begin_adjust
35
33
 
36
- def int_rand_end
37
- max_int / int_multiple
38
- end
34
+ def int_rand_end = max_int / int_multiple
39
35
 
40
- def int_rand_begin_adjust
41
- (min_int % int_multiple).zero? ? 0 : 1
42
- end
36
+ def int_rand_begin_adjust = (min_int % int_multiple).zero? ? 0 : 1
43
37
 
44
- def int_multiple
45
- multipleOf || 1
46
- end
38
+ def int_multiple = multipleOf || 1
47
39
 
48
40
  def min_int
49
41
  if minimum
@@ -14,35 +14,22 @@ module Fakeit
14
14
 
15
15
  private
16
16
 
17
- def static_number_example
18
- (num_rand_end * num_multiple)
19
- .then { multipleOf ? _1 : _1.round(2) }
20
- end
17
+ def static_number_example = (num_rand_end * num_multiple).then { multipleOf ? _1 : _1.round(2) }
21
18
 
22
19
  def random_number_example
23
20
  (Faker::Number.between(from: num_rand_begin, to: num_rand_end) * num_multiple)
24
21
  .then { multipleOf ? _1 : _1.round(2) }
25
22
  end
26
23
 
27
- def num_rand_begin
28
- multipleOf ? (min_num / multipleOf).ceil : min_num
29
- end
24
+ def num_rand_begin = multipleOf ? (min_num / multipleOf).ceil : min_num
30
25
 
31
- def num_rand_end
32
- multipleOf ? (max_num / multipleOf).floor : max_num
33
- end
26
+ def num_rand_end = multipleOf ? (max_num / multipleOf).floor : max_num
34
27
 
35
- def num_multiple
36
- multipleOf || 1
37
- end
28
+ def num_multiple = multipleOf || 1
38
29
 
39
- def min_num
40
- (minimum || MIN_NUM).to_f.ceil(2)
41
- end
30
+ def min_num = (minimum || MIN_NUM).to_f.ceil(2)
42
31
 
43
- def max_num
44
- (maximum || MAX_NUM).to_f.floor(2)
45
- end
32
+ def max_num = (maximum || MAX_NUM).to_f.floor(2)
46
33
  end
47
34
  end
48
35
  end
@@ -65,13 +65,9 @@ module Fakeit
65
65
  end
66
66
  end
67
67
 
68
- def static_string_with_length
69
- '1' * max_string_length
70
- end
68
+ def static_string_with_length = '1' * max_string_length
71
69
 
72
- def static_string_format
73
- (STATIC_FORMAT_HANDLERS[format] || method(:unknown_format))[]
74
- end
70
+ def static_string_format = (STATIC_FORMAT_HANDLERS[format] || method(:unknown_format))[]
75
71
 
76
72
  def static_string_pattern
77
73
  @static_string_pattern ||= @string_pattern.examples(
@@ -79,29 +75,17 @@ module Fakeit
79
75
  ).first
80
76
  end
81
77
 
82
- def length_constraint
83
- minLength || maxLength
84
- end
78
+ def length_constraint = minLength || maxLength
85
79
 
86
- def string_with_length
87
- Faker::Internet.username(specifier: min_string_length..max_string_length)
88
- end
80
+ def string_with_length = Faker::Internet.username(specifier: min_string_length..max_string_length)
89
81
 
90
- def min_string_length
91
- minLength || 0
92
- end
82
+ def min_string_length = minLength || 0
93
83
 
94
- def max_string_length
95
- maxLength || min_string_length + 10
96
- end
84
+ def max_string_length = maxLength || min_string_length + 10
97
85
 
98
- def random_string_format
99
- (RANDOM_FORMAT_HANDLERS[format] || method(:unknown_format))[]
100
- end
86
+ def random_string_format = (RANDOM_FORMAT_HANDLERS[format] || method(:unknown_format))[]
101
87
 
102
- def random_string_pattern
103
- @random_string_pattern ||= @string_pattern.random_example(max_repeater_variance: 1)
104
- end
88
+ def random_string_pattern = @random_string_pattern ||= @string_pattern.random_example(max_repeater_variance: 1)
105
89
 
106
90
  def unknown_format
107
91
  Logger.info("Unknown string format: #{format}")
@@ -4,7 +4,8 @@ module Fakeit
4
4
  def load(src)
5
5
  parse_method = parse_method(src)
6
6
 
7
- open(src, &:read)
7
+ URI
8
+ .open(src, &:read)
8
9
  .then(&parse_method)
9
10
  .then(&OpenAPIParser.method(:parse))
10
11
  end
@@ -7,9 +7,7 @@ module Fakeit
7
7
  @app_options = app_options
8
8
  end
9
9
 
10
- def status
11
- response.first.to_i
12
- end
10
+ def status = response.first.to_i
13
11
 
14
12
  def headers
15
13
  response_headers
@@ -22,39 +20,31 @@ module Fakeit
22
20
  response_schema
23
21
  &.schema
24
22
  &.to_example(example_options)
25
- &.then(&JSON.method(:generate))
23
+ &.then(&method(:serialise))
26
24
  .to_s
27
25
  end
28
26
 
29
- def validate(**request_parts)
30
- @validator.validate(**request_parts)
31
- end
27
+ def validate(...) = @validator.validate(...)
32
28
 
33
29
  private
34
30
 
31
+ def serialise(body) = body.is_a?(String) ? body : JSON.generate(body)
32
+
35
33
  def example_options
36
34
  { use_example: @app_options.use_example, use_static: @app_options.method(:use_static?), depth: 0 }
37
35
  end
38
36
 
39
37
  def response_content
40
- response.last.content&.find { |k, _| k =~ %r{^application/.*json} }
38
+ response.last.content&.find { |k, _| k =~ %r{^application/.*json} || k == 'application/pdf' }
41
39
  end
42
40
 
43
- def response_schema
44
- response_content&.last
45
- end
41
+ def response_schema = response_content&.last
46
42
 
47
- def response_content_type
48
- response_content&.first
49
- end
43
+ def response_content_type = response_content&.first
50
44
 
51
- def response_headers
52
- response.last.headers
53
- end
45
+ def response_headers = response.last.headers
54
46
 
55
- def response
56
- @request_operation.operation_object.responses.response.min
57
- end
47
+ def response = @request_operation.operation_object.responses.response.min
58
48
  end
59
49
  end
60
50
  end
@@ -0,0 +1,6 @@
1
+ module Fakeit
2
+ module Openapi
3
+ class ReferenceError < StandardError
4
+ end
5
+ end
6
+ end
@@ -1,9 +1,7 @@
1
1
  module Fakeit
2
2
  module Validation
3
3
  class Validator
4
- def initialize(operation)
5
- @operation = operation
6
- end
4
+ def initialize(operation) = @operation = operation
7
5
 
8
6
  def validate(body: {}, params: {}, headers: {})
9
7
  options = OpenAPIParser::SchemaValidator::Options.new(coerce_value: true)
@@ -26,17 +24,11 @@ module Fakeit
26
24
  end
27
25
  end
28
26
 
29
- def can_validate?(media_type)
30
- media_type =~ %r{^application/.*json} || media_type == 'multipart/form-data'
31
- end
27
+ def can_validate?(media_type) = media_type =~ %r{^application/.*json} || media_type == 'multipart/form-data'
32
28
 
33
- def request_content_types
34
- request_body&.content&.keys.to_a
35
- end
29
+ def request_content_types = request_body&.content&.keys.to_a
36
30
 
37
- def request_body
38
- @operation.operation_object.request_body
39
- end
31
+ def request_body = @operation.operation_object.request_body
40
32
  end
41
33
  end
42
34
  end
@@ -1,3 +1,3 @@
1
1
  module Fakeit
2
- VERSION = '0.7.0'.freeze
2
+ VERSION = '0.9.1'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fakeit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.9.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Feng
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-10-22 00:00:00.000000000 Z
11
+ date: 2021-04-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -58,14 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '12.0'
61
+ version: '13.0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '12.0'
68
+ version: '13.0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rspec
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -86,14 +86,28 @@ dependencies:
86
86
  requirements:
87
87
  - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: '0.80'
89
+ version: '1.10'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
- version: '0.80'
96
+ version: '1.10'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop-rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.5'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.5'
97
111
  - !ruby/object:Gem::Dependency
98
112
  name: simplecov
99
113
  requirement: !ruby/object:Gem::Requirement
@@ -206,6 +220,20 @@ dependencies:
206
220
  - - "~>"
207
221
  - !ruby/object:Gem::Version
208
222
  version: '4.8'
223
+ - !ruby/object:Gem::Dependency
224
+ name: webrick
225
+ requirement: !ruby/object:Gem::Requirement
226
+ requirements:
227
+ - - "~>"
228
+ - !ruby/object:Gem::Version
229
+ version: '1.7'
230
+ type: :runtime
231
+ prerelease: false
232
+ version_requirements: !ruby/object:Gem::Requirement
233
+ requirements:
234
+ - - "~>"
235
+ - !ruby/object:Gem::Version
236
+ version: '1.7'
209
237
  description: Create mock server from Openapi specification
210
238
  email:
211
239
  - realfengjia@foxmail.com
@@ -235,6 +263,8 @@ files:
235
263
  - lib/fakeit/app/options.rb
236
264
  - lib/fakeit/app/routes/config_route.rb
237
265
  - lib/fakeit/app/routes/openapi_route.rb
266
+ - lib/fakeit/core_extensions/array_validator.rb
267
+ - lib/fakeit/core_extensions/reference.rb
238
268
  - lib/fakeit/core_extensions/schema.rb
239
269
  - lib/fakeit/logger.rb
240
270
  - lib/fakeit/middleware/recorder.rb
@@ -246,6 +276,7 @@ files:
246
276
  - lib/fakeit/openapi/example/string_example.rb
247
277
  - lib/fakeit/openapi/loader.rb
248
278
  - lib/fakeit/openapi/operation.rb
279
+ - lib/fakeit/openapi/reference_error.rb
249
280
  - lib/fakeit/openapi/schema.rb
250
281
  - lib/fakeit/openapi/specification.rb
251
282
  - lib/fakeit/validation/validation_error.rb
@@ -255,7 +286,7 @@ homepage: https://github.com/JustinFeng/fakeit
255
286
  licenses:
256
287
  - MIT
257
288
  metadata: {}
258
- post_install_message:
289
+ post_install_message:
259
290
  rdoc_options: []
260
291
  require_paths:
261
292
  - lib
@@ -263,15 +294,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
263
294
  requirements:
264
295
  - - ">="
265
296
  - !ruby/object:Gem::Version
266
- version: 2.7.0
297
+ version: 3.0.0
267
298
  required_rubygems_version: !ruby/object:Gem::Requirement
268
299
  requirements:
269
300
  - - ">="
270
301
  - !ruby/object:Gem::Version
271
302
  version: '0'
272
303
  requirements: []
273
- rubygems_version: 3.1.4
274
- signing_key:
304
+ rubygems_version: 3.2.3
305
+ signing_key:
275
306
  specification_version: 4
276
307
  summary: Create mock server from Openapi specification
277
308
  test_files: []