endpointer 0.0.7 → 0.1.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: fa780a8443a0cfe0a905bfa6ea711c0a336adc74
4
- data.tar.gz: 53c1ed73d95fd7bc73fca0617af99e670ed42eb8
3
+ metadata.gz: ed57fed80944f3216675621c478581a387cfadca
4
+ data.tar.gz: fa510a7ca1aa6a003eab39ff56dd123fad0390bb
5
5
  SHA512:
6
- metadata.gz: 0ca015089a8d79e4827f2c57ddd3defa3b354b1cafe884ea7674e6402b14d465e80a7bc7bb6d82ccf310eee7b2cb33b60f00f2a098aa6b527081b31d7448ff61
7
- data.tar.gz: bdee2529f5ba34ce2b941a0dfb72ab91f3ef6a1e04be6a922306211b0f291d5ca819cddca0ae71875be7cdf7571277f3a28731e7c42b0bc5ae8cdbdf96e3d45e
6
+ metadata.gz: a702ca63c58db74696360affe2be6daeec2d86c7952377d1d72c54d89fb0f15b671b80778b9089c53ed5d3a8c4b6b23b8425625667de5f5013e546f390c786f1
7
+ data.tar.gz: 777d002c2e8bb5d68a0bec8b12328837930320b4e5a23211878ac48a2c1d79cd5ee30ff40bcb112c4763fbd83aab35ba6fbdc18cf3fb2525b03a6d30d8ed9558
data/.travis.yml CHANGED
@@ -4,7 +4,7 @@ branches:
4
4
  - master
5
5
  - /^v[0-9]+\.[0-9]+\.[0-9]+/
6
6
  rvm:
7
- - 2.4.0
7
+ - 2.4.1
8
8
  - 2.3.3
9
9
  - 2.2.6
10
10
  - jruby-9.1.6.0
data/README.md CHANGED
@@ -37,6 +37,9 @@ In order to use Endpointer you need to create a JSON configuration file with the
37
37
  "headers": {
38
38
  "Authorization": "Bearer test",
39
39
  "Accept": "application/json"
40
+ },
41
+ "matchers": {
42
+ "matcher-id": "some regex"
40
43
  }
41
44
  },
42
45
  ...
@@ -85,6 +88,11 @@ If the request is to be executed against the real service the headers defined in
85
88
 
86
89
  $ curl -H "Accept: text/plain" http://localhost:4567/get
87
90
 
91
+
92
+ ### Custom matchers
93
+
94
+ 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
+
88
96
  ### Caching
89
97
 
90
98
  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.
@@ -115,4 +123,3 @@ As mentioned above I'm actively going to work on improving endpointer and the fo
115
123
  ## License
116
124
 
117
125
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
118
-
@@ -6,6 +6,10 @@
6
6
  "headers": {
7
7
  "Authorization": "Bearer test",
8
8
  "Content-Type": "application/json"
9
+ },
10
+ "matchers": {
11
+ "matcher1": "something at the end$",
12
+ "matcher2": "^starting with"
9
13
  }
10
14
  },
11
15
  {
@@ -0,0 +1,14 @@
1
+ module Endpointer
2
+ class CacheKeyResolver
3
+ def get_key(resource, request_body)
4
+ return "#{resource.id}.yml" if resource.matchers.nil?
5
+
6
+ matches = resource.matchers.select do |_matcher_name, regex|
7
+ request_body.match(regex)
8
+ end
9
+
10
+ return "#{resource.id}.yml" if matches.empty?
11
+ "#{resource.id}_#{matches.keys.first}.yml"
12
+ end
13
+ end
14
+ end
@@ -1,6 +1,7 @@
1
1
  require 'endpointer/errors/cached_item_not_found_error'
2
2
  require 'endpointer/errors/invalid_cache_dir_error'
3
3
  require 'endpointer/cache_container'
4
+ require 'endpointer/cache_key_resolver'
4
5
  require 'yaml'
5
6
 
6
7
  module Endpointer
@@ -10,15 +11,16 @@ module Endpointer
10
11
  initialize_path(path)
11
12
  end
12
13
 
13
- def get(resource)
14
- cache_container = retrieve_cache_container(resource)
14
+ def get(resource, request_body)
15
+ cache_key = get_cache_key(resource, request_body)
16
+ cache_container = retrieve_cache_container(cache_key)
15
17
  raise Endpointer::Errors::CachedItemNotFoundError unless cache_container.resource == resource
16
18
  cache_container.response
17
19
  end
18
20
 
19
- def set(resource, response)
21
+ def set(resource, request_body, response)
20
22
  cache_container = create_cache_container(resource, response)
21
- File.write(File.join(@path, "#{resource.id}.yml"), YAML.dump(cache_container))
23
+ File.write(File.join(@path, get_cache_key(resource, request_body)), YAML.dump(cache_container))
22
24
  end
23
25
 
24
26
  def invalidate
@@ -32,20 +34,24 @@ module Endpointer
32
34
  Endpointer::CacheContainer.new(resource, response, Time.now.utc)
33
35
  end
34
36
 
35
- def retrieve_cache_container(resource)
37
+ def retrieve_cache_container(cache_key)
36
38
  begin
37
- YAML.load(File.read(File.join(@path, "#{resource.id}.yml")))
38
- rescue
39
- raise Endpointer::Errors::CachedItemNotFoundError
39
+ YAML.load(File.read(File.join(@path, cache_key)))
40
+ rescue Errno::ENOENT => e
41
+ raise Endpointer::Errors::CachedItemNotFoundError, e.message
40
42
  end
41
43
  end
42
44
 
45
+ def get_cache_key(resource, request_body)
46
+ Endpointer::CacheKeyResolver.new.get_key(resource, request_body)
47
+ end
48
+
43
49
  def initialize_path(path)
44
50
  begin
45
51
  @path = path
46
52
  Dir.mkdir(@path) unless File.exist?(@path)
47
53
  rescue Errno::ENOENT => e
48
- raise Endpointer::Errors::InvalidCacheDirError.new(e.message)
54
+ raise Endpointer::Errors::InvalidCacheDirError, e.message
49
55
  end
50
56
  end
51
57
  end
@@ -1,4 +1,4 @@
1
1
  module Endpointer
2
- class Resource < Struct.new(:id, :method, :url, :headers)
2
+ class Resource < Struct.new(:id, :method, :url, :headers, :matchers)
3
3
  end
4
4
  end
@@ -6,10 +6,10 @@ module Endpointer
6
6
 
7
7
  def perform(request, resource, options)
8
8
  begin
9
- cache(options.cache_dir).get(resource)
9
+ cache(options.cache_dir).get(resource, get_request_body(request))
10
10
  rescue Endpointer::Errors::CachedItemNotFoundError
11
11
  response = Endpointer::PerformerFactory.create(resource.method).execute(request, resource)
12
- cache(options.cache_dir).set(resource, response)
12
+ cache(options.cache_dir).set(resource, get_request_body(request), response)
13
13
  response
14
14
  end
15
15
  end
@@ -19,5 +19,11 @@ module Endpointer
19
19
  def cache(cache_dir)
20
20
  @cache ||= Endpointer::Cacher.new(cache_dir)
21
21
  end
22
+
23
+ def get_request_body(request)
24
+ request_body = request.body.read
25
+ request.body.rewind
26
+ request_body
27
+ end
22
28
  end
23
29
  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"])
8
+ Resource.new(resource["id"], resource["method"].to_sym, resource["url"], resource["headers"], resource["matchers"])
9
9
  end
10
10
  end
11
11
 
@@ -1,3 +1,3 @@
1
1
  module Endpointer
2
- VERSION = "0.0.7"
2
+ VERSION = "0.1.0"
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.0.7
4
+ version: 0.1.0
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-22 00:00:00.000000000 Z
11
+ date: 2017-05-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -176,6 +176,7 @@ files:
176
176
  - lib/endpointer/app_creator.rb
177
177
  - lib/endpointer/argument_parser.rb
178
178
  - lib/endpointer/cache_container.rb
179
+ - lib/endpointer/cache_key_resolver.rb
179
180
  - lib/endpointer/cacher.rb
180
181
  - lib/endpointer/configuration.rb
181
182
  - lib/endpointer/errors/cached_item_not_found_error.rb