endpointer 0.1.0 → 0.1.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
  SHA1:
3
- metadata.gz: ed57fed80944f3216675621c478581a387cfadca
4
- data.tar.gz: fa510a7ca1aa6a003eab39ff56dd123fad0390bb
3
+ metadata.gz: 54a06e9c23fe3e2534949eb3592bdb353b94825f
4
+ data.tar.gz: e63af05bc1595772ab7f240164519d541a417336
5
5
  SHA512:
6
- metadata.gz: a702ca63c58db74696360affe2be6daeec2d86c7952377d1d72c54d89fb0f15b671b80778b9089c53ed5d3a8c4b6b23b8425625667de5f5013e546f390c786f1
7
- data.tar.gz: 777d002c2e8bb5d68a0bec8b12328837930320b4e5a23211878ac48a2c1d79cd5ee30ff40bcb112c4763fbd83aab35ba6fbdc18cf3fb2525b03a6d30d8ed9558
6
+ metadata.gz: 98b40910be28f25ea528f485160f58784b041ea0635da0b065a120e3ec36d1a9114d238e69a359205b5822e84f5d8bf74437dfbc1e1ffdb91621f7d6bd233703
7
+ data.tar.gz: af82f344e594cadbd8eedc24f257f55b3cf07d23919e2f7d992b25e5aa3049745c6583e6dd1975ec4303a0eeeea98269c44f8bb22c866885c4e15f4bb7afde73
data/README.md CHANGED
@@ -40,7 +40,15 @@ In order to use Endpointer you need to create a JSON configuration file with the
40
40
  },
41
41
  "matchers": {
42
42
  "matcher-id": "some regex"
43
- }
43
+ },
44
+ "substitutions": [
45
+ {
46
+ "from_request": "Some regex with a named matcher called match (?<match\\w+)",
47
+ "to_response": "Some regex with a named matcher called match (?<match\\w+)",
48
+ },
49
+ ...
50
+ ],
51
+
44
52
  },
45
53
  ...
46
54
  ]
@@ -93,6 +101,10 @@ If the request is to be executed against the real service the headers defined in
93
101
 
94
102
  Endpointer now supports custom matchers with the goal to give you more fine grained control of what gets cached. This allows you to cache different requests using a regex separately. You just define your resource with its custom matchers as shown above. Endpointer will then attempt to match the regex against the request body and store the response separately if it matches. If no matcher matches it will default to the default cache key for the resource(resource id).
95
103
 
104
+ ### Substitutions
105
+
106
+ Endpointer can take a list of regexes to copy values from the request and replace into the response. This is particularly useful if your use case needs a cached response that needs different values based on the request.
107
+
96
108
  ### Caching
97
109
 
98
110
  By default endpointer will use your operating system's temp directory to store its cache files `(TMP_DIR/endpointer_cache)`. In order to configure the cache path you need to pass the `--cache-dir=<path>` argument.
@@ -119,6 +131,7 @@ As mentioned above I'm actively going to work on improving endpointer and the fo
119
131
  * A `--debug` flag to the command line that will give a pry window on every request allowing you to play with the Request and Response objects.
120
132
  * Configurable port(this can already be done if using config.ru)
121
133
  * Support multiple key/value stores for caching. Currently only uses local YAML files. One Suggestion is Redis support.
134
+ * Support response manipulator plugins that can pre-process responses
122
135
 
123
136
  ## License
124
137
 
@@ -2,6 +2,7 @@ require 'endpointer/errors/cached_item_not_found_error'
2
2
  require 'endpointer/errors/invalid_cache_dir_error'
3
3
  require 'endpointer/cache_container'
4
4
  require 'endpointer/cache_key_resolver'
5
+ require 'endpointer/response_presenter'
5
6
  require 'yaml'
6
7
 
7
8
  module Endpointer
@@ -15,7 +16,7 @@ module Endpointer
15
16
  cache_key = get_cache_key(resource, request_body)
16
17
  cache_container = retrieve_cache_container(cache_key)
17
18
  raise Endpointer::Errors::CachedItemNotFoundError unless cache_container.resource == resource
18
- cache_container.response
19
+ present_response(resource, request_body, cache_container)
19
20
  end
20
21
 
21
22
  def set(resource, request_body, response)
@@ -54,5 +55,15 @@ module Endpointer
54
55
  raise Endpointer::Errors::InvalidCacheDirError, e.message
55
56
  end
56
57
  end
58
+
59
+ def present_response(resource, request_body, cache_container)
60
+ Endpointer::ResponsePresenter.new.present(
61
+ status: cache_container.response.code,
62
+ body: cache_container.response.body,
63
+ headers: cache_container.response.headers,
64
+ request_body: request_body,
65
+ resource: resource
66
+ )
67
+ end
57
68
  end
58
69
  end
@@ -19,7 +19,10 @@ module Endpointer
19
19
  rescue RestClient::ExceptionWithResponse => e
20
20
  response = e.response
21
21
  end
22
- Endpointer::ResponsePresenter.new.present(status: response.code, body: response.body, headers: response.headers)
22
+
23
+ request_body = request.body.read
24
+ request.body.rewind
25
+ Endpointer::ResponsePresenter.new.present(status: response.code, body: response.body, headers: response.headers, request_body: request_body, resource: resource)
23
26
  end
24
27
  end
25
28
  end
@@ -14,10 +14,12 @@ module Endpointer
14
14
  private
15
15
 
16
16
  def execute_method(method, request, resource)
17
+ request_body = request.body.read
18
+
17
19
  begin
18
20
  response = RestClient.send(method,
19
21
  construct_uri(request, resource),
20
- request.body.read,
22
+ request_body,
21
23
  create_headers(request, resource)
22
24
  )
23
25
  request.body.rewind
@@ -25,7 +27,7 @@ module Endpointer
25
27
  response = e.response
26
28
  end
27
29
 
28
- Endpointer::ResponsePresenter.new.present(status: response.code, body: response.body, headers: response.headers)
30
+ Endpointer::ResponsePresenter.new.present(status: response.code, body: response.body, headers: response.headers, request_body: request_body, resource: resource)
29
31
  end
30
32
  end
31
33
  end
@@ -1,4 +1,4 @@
1
1
  module Endpointer
2
- class Resource < Struct.new(:id, :method, :url, :headers, :matchers)
2
+ class Resource < Struct.new(:id, :method, :url, :headers, :matchers, :substitutions)
3
3
  end
4
4
  end
@@ -5,7 +5,7 @@ module Endpointer
5
5
  class ResourceParser
6
6
  def parse(resource_config)
7
7
  parse_config(resource_config).map do |resource|
8
- Resource.new(resource["id"], resource["method"].to_sym, resource["url"], resource["headers"], resource["matchers"])
8
+ Resource.new(resource["id"], resource["method"].to_sym, resource["url"], resource["headers"], resource["matchers"], resource["substitutions"])
9
9
  end
10
10
  end
11
11
 
@@ -1,10 +1,11 @@
1
+ require 'endpointer/response_substitutioner'
1
2
  module Endpointer
2
3
  class ResponsePresenter
3
4
 
4
- def present(status: , body: , headers: )
5
+ def present(status: , body: , headers: , request_body: , resource: )
5
6
  Response.new(
6
7
  status,
7
- body,
8
+ substituted_body(request_body, body, resource),
8
9
  sanitise_headers(uglify_headers(headers))
9
10
  )
10
11
  end
@@ -24,5 +25,9 @@ module Endpointer
24
25
  }
25
26
  end
26
27
 
28
+ def substituted_body(request_body, response_body, resource)
29
+ Endpointer::ResponseSubstitutioner.new.substitute(request_body, response_body, resource.substitutions)
30
+ end
31
+
27
32
  end
28
33
  end
@@ -0,0 +1,26 @@
1
+ module Endpointer
2
+ class ResponseSubstitutioner
3
+
4
+ def substitute(request_body, response_body, substitutions)
5
+ return response_body if substitutions.nil?
6
+
7
+ substitutions.inject(response_body) do |output, substitution|
8
+ value_to_use = get_value_for(request_body, substitution.fetch('from_request'))
9
+ value_to_replace = get_value_for(response_body, substitution.fetch('to_response'))
10
+ output.gsub(value_to_replace, value_to_use)
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ def get_value_for(string, regex)
17
+ match_data = string.match(Regexp.new(regex))
18
+
19
+ if match_data
20
+ match_data[:match]
21
+ else
22
+ ''
23
+ end
24
+ end
25
+ end
26
+ end
@@ -1,3 +1,3 @@
1
1
  module Endpointer
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: endpointer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zen Kyprianou
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-23 00:00:00.000000000 Z
11
+ date: 2017-05-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -193,6 +193,7 @@ files:
193
193
  - lib/endpointer/resource_parser.rb
194
194
  - lib/endpointer/response.rb
195
195
  - lib/endpointer/response_presenter.rb
196
+ - lib/endpointer/response_substitutioner.rb
196
197
  - lib/endpointer/version.rb
197
198
  homepage: https://github.com/zenonas/endpointer
198
199
  licenses: