api_sim 3.2.0 → 4.0.0

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: 39744282ba52dd613a6cd9481717892a39482f3b
4
- data.tar.gz: 67e3e2d58c0e5e2110521924ddc5c99114a5ce83
3
+ metadata.gz: 3ac439cdf5117d5e307c6fd4f3bc9720af5f60fc
4
+ data.tar.gz: 6ab64d5c0d40d03a94af5590a76b1a9657379d26
5
5
  SHA512:
6
- metadata.gz: a88815795d88e7c6f6adc25dfff2b2e7f79c1eda450079d212e7f7fddab4adb20ffc29e2a5b9aa50aee4bbc81c14f67c9ebf92ca1d842740c3ff8394c9dc0348
7
- data.tar.gz: 738f62d178070ebebe67302d4762aae8c40c34eb8c74834520a3654e2b9bc3139f67edda7c9ad5a9902f56bff186f25a7674f540875eb89450f3e693bed29a40
6
+ metadata.gz: 01e1097d28d79c7c41ebd0944ed0fb53aa5e90fc4553a91d1e9838f343f403ba9d1de7c4614171382cb3db73e822c6bd4ad7511a485fd84a09feba3f83839aeb
7
+ data.tar.gz: cd9378a7e9431b6aafa3003c1f0483493aede7adad063372db27605035e57455c6f49c19f8d88cfef6c95d8c5c26a3d7bd51fcdbc936e2d2e5d82c9a7bc13241
data/api_sim.gemspec CHANGED
@@ -22,6 +22,7 @@ Gem::Specification.new do |spec|
22
22
  spec.add_dependency "sinatra", '~> 1.0'
23
23
  spec.add_dependency "nokogiri", '~> 1.6.7'
24
24
  spec.add_dependency "json-schema", '>= 2.5'
25
+ spec.add_dependency "mustermann", '~> 1.0.0.beta2'
25
26
  spec.add_development_dependency "bundler", "~> 1.11"
26
27
  spec.add_development_dependency "rake", "~> 10.0"
27
28
  spec.add_development_dependency "rspec", "~> 3.0"
@@ -4,9 +4,11 @@ require 'json'
4
4
  require 'tilt/erb'
5
5
  require 'api_sim/view_helpers'
6
6
  require 'json-schema'
7
+ require 'mustermann'
7
8
 
8
9
  module ApiSim
9
10
  class BuiltApp < Sinatra::Base
11
+ API_REQUEST_MATCHER = Mustermann.new('/requests/:method{+path}')
10
12
  use Rack::MethodOverride
11
13
 
12
14
  helpers do
@@ -69,9 +71,8 @@ module ApiSim
69
71
  end
70
72
 
71
73
  get '/requests/*' do
72
- endpoint_name = parse_endpoint_from_request(request) || halt(404)
73
- http_method = params.fetch('method', 'GET')
74
- endpoint = find_matching_endpoint(endpoint_name, http_method)
74
+ params = API_REQUEST_MATCHER.match(request.path) || halt(404)
75
+ endpoint = matcher(faux_request(params['method'], params['path']))
75
76
 
76
77
  halt(404) unless endpoint
77
78
  endpoint.requests.to_json
@@ -88,21 +89,6 @@ module ApiSim
88
89
 
89
90
  private
90
91
 
91
- def find_matching_endpoint(endpoint_name, http_method)
92
- matching_endpoints = active_endpoints.select do |endpoint|
93
- endpoint.route == "/#{endpoint_name}"
94
- end
95
-
96
- case matching_endpoints.size
97
- when 0
98
- nil
99
- when 1
100
- matching_endpoints.first
101
- else
102
- matching_endpoints.find { |endpoint| endpoint.http_method =~ /#{http_method}/i }
103
- end
104
- end
105
-
106
92
  def active_endpoints
107
93
  self.class.endpoints.reject(&:overridden?)
108
94
  end
@@ -171,11 +157,6 @@ module ApiSim
171
157
  @response_body.empty? ? {} : @response_body
172
158
  end
173
159
 
174
- def parse_endpoint_from_request(request)
175
- path_matcher = request.path.match(/\/requests\/([\w\/_-]*)/)
176
- path_matcher.size == 2 ? path_matcher[1] : nil
177
- end
178
-
179
160
  def schema_validates?(endpoint)
180
161
  if blank?(endpoint.request_schema) || request.env['CONTENT_TYPE'] != 'application/json'
181
162
  true
@@ -54,14 +54,7 @@ module ApiSim
54
54
  protected
55
55
 
56
56
  def matches_route_pattern?(request)
57
- route_tokens = route.split('/')
58
- request_tokens = request.path.split('/')
59
-
60
- return false if route_tokens.count != request_tokens.count && !route_tokens.include?('*')
61
- route_tokens.zip(request_tokens).all? do |matcher_part, request_part|
62
- break true if matcher_part == '*'
63
- matcher_part == request_part || matcher_part.start_with?(':')
64
- end
57
+ route.match(request.path)
65
58
  end
66
59
  end
67
60
  end
@@ -1,4 +1,5 @@
1
1
  require 'forwardable'
2
+ require 'mustermann'
2
3
  require 'api_sim/recorded_request'
3
4
  require 'api_sim/matchers/base_matcher'
4
5
 
@@ -9,7 +10,7 @@ module ApiSim
9
10
 
10
11
  def initialize(http_method:, route:, response_generator:, default: false, matcher: ALWAYS_TRUE_MATCHER)
11
12
  @matcher = matcher
12
- @route = route
13
+ @route = Mustermann.new(route)
13
14
  @http_method = http_method
14
15
  @default = default
15
16
  @response_generator = response_generator
@@ -42,9 +43,7 @@ module ApiSim
42
43
  end
43
44
 
44
45
  def [](requested_part)
45
- @matcher.route.split('/').zip(path.split('/')).find { |matcher_part, path_part|
46
- ":#{requested_part}" == matcher_part and break path_part
47
- } || super
46
+ @matcher.route.match(path)[requested_part]
48
47
  end
49
48
  end
50
49
  end
@@ -1,3 +1,4 @@
1
+ require 'mustermann'
1
2
  require 'api_sim/recorded_request'
2
3
  require 'api_sim/matchers/base_matcher'
3
4
 
@@ -12,7 +13,7 @@ module ApiSim
12
13
  @headers = headers
13
14
  @response_body = response_body
14
15
  @response_code = response_code
15
- @route = route
16
+ @route = Mustermann.new(route)
16
17
  @http_method = http_method
17
18
  @schema = schema
18
19
  end
@@ -21,7 +22,7 @@ module ApiSim
21
22
  request.body.rewind
22
23
  body = request.body.read
23
24
  request.body.rewind
24
- request.path == route && request.request_method == http_method && matcher.match(body)
25
+ route.match(request.path) && request.request_method == http_method && matcher.match(body)
25
26
  end
26
27
 
27
28
  def match_on_body?
@@ -1,3 +1,4 @@
1
+ require 'mustermann'
1
2
  require 'api_sim/recorded_request'
2
3
  require 'api_sim/matchers/base_matcher'
3
4
 
@@ -12,7 +13,7 @@ module ApiSim
12
13
  @headers = args.fetch(:headers, {})
13
14
  @response_body = args.fetch(:response_body, '')
14
15
  @response_code = args.fetch(:response_code, 200)
15
- @route = args.fetch(:route)
16
+ @route = Mustermann.new(args.fetch(:route))
16
17
  @http_method = args.fetch(:http_method)
17
18
  @schema = args.fetch(:schema, nil)
18
19
  @request_schema = args.fetch(:request_schema, nil)
@@ -1,3 +1,3 @@
1
1
  module ApiSim
2
- VERSION = "3.2.0"
2
+ VERSION = "4.0.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api_sim
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.0
4
+ version: 4.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - TJ Taylor
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-10-06 00:00:00.000000000 Z
11
+ date: 2016-10-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sinatra
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '2.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mustermann
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 1.0.0.beta2
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 1.0.0.beta2
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: bundler
57
71
  requirement: !ruby/object:Gem::Requirement