endpointer 0.0.4 → 0.0.5

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: e705dec6f55d6ae9e87f60e415451ec35f1826d7
4
- data.tar.gz: f99559081ebf4ff13bc782e5c4d3dbe13a0f7fc1
3
+ metadata.gz: a628ec40e0ebf2d37e81363bc14414499a71e2ca
4
+ data.tar.gz: 44ccbdca0c8c66cdbfc029c1a54ad7bbd05069e4
5
5
  SHA512:
6
- metadata.gz: 231c82787a365665c0c322f0c4dd6c70a03c3a461db9771d98de63c37fa4b6c6f67ce6a90b10d580a59f88d19e8df4370b0a3c02336b27f54680f7c4ab85dffd
7
- data.tar.gz: 2292181e7d9aaf294428cca8189d7ce6fae38a14ca37424b13d0a9842c929524530038d49c2dd230d92cb089cb9c58bd5cd035b2290210ae92cc95037a1d9b25
6
+ metadata.gz: a1faa71cf5ea0e0dcf5bd8684bb7af2ae940adee40c3a073579c3e3fcbd74915fb896b711d54a6c834b8a84b74c7ab77ff037e7acc3171e7bb8e13950307edad
7
+ data.tar.gz: 881bed74421da18ace219797cfafb0e359a88e010c3d4d7cbbcecb48180c2f30007f96baf34cdb058ba2a2338f6d6851bf08245224218ac1d19e06876351551a
data/.travis.yml CHANGED
@@ -6,6 +6,7 @@ branches:
6
6
  rvm:
7
7
  - 2.4.0
8
8
  - 2.3.3
9
+ - 2.2.6
9
10
  - jruby-9.1.6.0
10
11
  script:
11
12
  - bundle exec rake
data/README.md CHANGED
@@ -6,7 +6,7 @@ Endpointer is a small gem that tries to act as a caching proxy between your dev
6
6
 
7
7
  ## Requirements
8
8
 
9
- * Ruby 2.2+ (Tests running against MRI 2.4.0, 2.3.3, and JRuby 9.1.6.0)
9
+ * Ruby 2.2+ (Tests running against MRI 2.4.0, 2.3.3, 2.2.6, and JRuby 9.1.6.0)
10
10
 
11
11
  ## Installation
12
12
 
@@ -45,7 +45,19 @@ In order to use Endpointer you need to create a JSON configuration file with the
45
45
 
46
46
  You can then invoke endpointer by executing
47
47
 
48
- $ endpointer [--invalidate] <path_to_json_config_file>
48
+ $ endpointer
49
+
50
+ (Note: Endpointer by default looks for a file called `endpointer.json` in the current directory)
51
+
52
+ A full list of endpointer commands can be seen by invoking `endpointer --help`
53
+ ```
54
+ ❯ endpointer --help
55
+ Usage: endpointer [options]
56
+ -d, --cache-dir CACHE_DIR Modifies the default cache directory [Default: TMP_DIR/endpointer_cache]
57
+ -i, --invalidate Invalidates the cache at startup
58
+ -c, --config CONFIG Override the default resource config file path. [Default: ./endpointer.json]
59
+
60
+ ```
49
61
 
50
62
  Or, include it in a config.ru
51
63
 
@@ -54,7 +66,13 @@ Or, include it in a config.ru
54
66
  #config.ru
55
67
  require 'endpointer'
56
68
 
57
- run Endpointer.app(['--invalidate', '<path_to_json_file>'])
69
+ Endpointer.configure do |config|
70
+ config.invalidate = false # Default
71
+ config.resource_config = File.read('./endpointer.json') # Default
72
+ config.cache_dir = File.join(Dir.tmpdir, 'endpointer_cache') # Default
73
+ end
74
+
75
+ run Endpointer.app
58
76
  ```
59
77
 
60
78
  Endpointer will attempt to return a cached resource, if one is found to match the request. Otherwise, a call to the real service will be performed and the response, if successful, persisted.
@@ -71,7 +89,7 @@ If the request is to be executed against the real service the headers defined in
71
89
 
72
90
  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.
73
91
 
74
- $ endpointer [--invalidate] [--cache-dir=/path/to/cache] <path_to_json_config_file>
92
+ $ endpointer -cache-dir=/path/to/cache
75
93
 
76
94
  You can provide the `--invalidate` flag to the command line to invalidate the cache. This empties the endpointer_cache directory.
77
95
 
data/bin/endpointer CHANGED
@@ -2,10 +2,7 @@
2
2
 
3
3
  require "bundler/setup"
4
4
  require "endpointer"
5
+ require "endpointer/argument_parser"
5
6
 
6
- begin
7
- Endpointer.run ARGV
8
- rescue Endpointer::Errors::InvalidArgumentsError
9
- STDERR.puts "Usage: endpointer [--invalidate] [--cache-dir=/path/to/cache] <path_to_json_config>.json"
10
- exit 1
11
- end
7
+ config = Endpointer::ArgumentParser.new.parse(ARGV)
8
+ Endpointer.run config
data/endpointer.gemspec CHANGED
@@ -13,6 +13,7 @@ Gem::Specification.new do |spec|
13
13
  spec.description = %q{Allows you to define endpoints to serve via a small caching proxy}
14
14
  spec.homepage = "https://github.com/zenonas/endpointer"
15
15
  spec.license = "MIT"
16
+ spec.required_ruby_version = '>= 2.2.0'
16
17
 
17
18
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
19
  spec.executables = ["endpointer"]
@@ -0,0 +1,19 @@
1
+ [
2
+ {
3
+ "id": "httpbinpost",
4
+ "method": "post",
5
+ "url": "http://httpbin.org/post",
6
+ "headers": {
7
+ "Authorization": "Bearer test",
8
+ "Content-Type": "application/json"
9
+ }
10
+ },
11
+ {
12
+ "id": "httpbinget",
13
+ "method": "get",
14
+ "url": "https://httpbin.org/get",
15
+ "headers": {
16
+ "Authorization": "Bearer test"
17
+ }
18
+ }
19
+ ]
data/lib/endpointer.rb CHANGED
@@ -1,19 +1,30 @@
1
1
  require "endpointer/version"
2
- require "endpointer/argument_parser"
2
+ require "endpointer/resource_parser"
3
3
  require "endpointer/app_creator"
4
4
  require "endpointer/errors/invalid_arguments_error"
5
+ require 'endpointer/configuration'
5
6
 
6
7
  module Endpointer
8
+ class << self
9
+ def run(config)
10
+ @configuration = config
11
+ app.run!
12
+ end
7
13
 
8
- def self.run(arguments)
9
- app(arguments).run!
10
- end
14
+ def app
15
+ Cacher.new(configuration.cache_dir).invalidate if configuration.invalidate
16
+ AppCreator.new.create(configuration)
17
+ end
18
+
19
+ def configure
20
+ yield(configuration) if block_given?
21
+ self
22
+ end
23
+
24
+ private
11
25
 
12
- def self.app(arguments)
13
- argument_parser = ArgumentParser.new(arguments)
14
- raise Errors::InvalidArgumentsError unless argument_parser.valid?
15
- options = argument_parser.parse_options
16
- Cacher.new(options.cache_dir).invalidate if options.invalidate
17
- AppCreator.new.create(argument_parser.parse_resources, options)
26
+ def configuration
27
+ @configuration ||= Configuration.new
28
+ end
18
29
  end
19
30
  end
@@ -5,10 +5,10 @@ require 'uri'
5
5
  module Endpointer
6
6
  class AppCreator
7
7
 
8
- def create(resources, options)
9
- resources.each do |resource|
8
+ def create(config)
9
+ resources(config).each do |resource|
10
10
  app.send(resource.method, path(resource.url)) do
11
- executor_response = Endpointer::ResourceExecutor.new.perform(request, resource, options)
11
+ executor_response = Endpointer::ResourceExecutor.new.perform(request, resource, config)
12
12
  headers executor_response.headers
13
13
  executor_response.body
14
14
  end
@@ -25,5 +25,9 @@ module Endpointer
25
25
  def path(url)
26
26
  URI.parse(url).path
27
27
  end
28
+
29
+ def resources(config)
30
+ Endpointer::ResourceParser.new.parse(config.resource_config)
31
+ end
28
32
  end
29
33
  end
@@ -1,64 +1,47 @@
1
1
  require 'endpointer/resource'
2
- require 'endpointer/options'
2
+ require 'endpointer/configuration'
3
3
  require 'json'
4
4
 
5
5
  module Endpointer
6
6
  class ArgumentParser
7
7
 
8
- VALID_OPTIONS = [
9
- '--invalidate',
10
- '--cache-dir='
11
- ]
12
-
13
- def initialize(arguments)
14
- @arguments = arguments
15
- end
16
-
17
- def parse_resources
18
- parse_config(config_file).map do |resource|
19
- Resource.new(resource["id"], resource["method"].to_sym, resource["url"], resource["headers"])
8
+ def parse(arguments)
9
+ begin
10
+ opt_parser.parse(arguments)
11
+ rescue OptionParser::InvalidOption => e
12
+ abort "Error: #{e}"
20
13
  end
21
- end
22
14
 
23
- def parse_options
24
- options = @arguments.select { |argument| option_argument?(argument)}
25
- build_options_from(options)
26
- end
27
-
28
- def valid?
29
- return false unless config_file
30
- valid_arguments = VALID_OPTIONS + [config_file]
31
- return false if @arguments.any? { |arg|
32
- valid_arguments.none? { |valid_opt| arg.include?(valid_opt) }
33
- }
34
- true
15
+ configuration
35
16
  end
36
17
 
37
18
  private
38
19
 
39
- def build_options_from(parsed_options)
40
- invalidate = parsed_options.include?("--invalidate")
41
- cache_dir_arg = parsed_options.find { |opt| opt.match(/^--cache-dir/) }
42
- cache_dir = cache_dir_arg.split('=').last unless cache_dir_arg.nil?
43
-
44
- Options.new(invalidate, cache_dir)
45
- end
46
-
47
- def config_file
48
- @arguments.find { |argument| config_file?(argument) }
49
- end
50
-
51
- def parse_config(config_file)
52
- JSON.parse(File.read(config_file))
53
- end
54
-
55
- def option_argument?(argument)
56
- argument.match(/^--.*/)
20
+ def opt_parser
21
+ OptionParser.new do |parser|
22
+ parser.banner = "Usage: endpointer [options]"
23
+
24
+ parser.on("-d CACHE_DIR", "--cache-dir CACHE_DIR", "Modifies the default cache directory [Default: TMP_DIR/endpointer_cache]") do |cache_dir|
25
+ configuration.cache_dir = cache_dir if cache_dir
26
+ end
27
+
28
+ parser.on("-i", "--invalidate", "Invalidates the cache at startup") do
29
+ configuration.invalidate = true
30
+ end
31
+
32
+ parser.on("-c CONFIG", "--config CONFIG", "Override the default resource config file path. [Default: ./endpointer.json]") do |config|
33
+ begin
34
+ resource_config = File.read(config)
35
+ configuration.resource_config = resource_config
36
+ rescue Errno::ENOENT
37
+ abort "Error: Config file supplied does not exist"
38
+ end
39
+ end
40
+ end
57
41
  end
58
42
 
59
- def config_file?(argument)
60
- argument.match(/.json$/) || File.exist?(argument)
43
+ def configuration
44
+ @configuration ||= Endpointer::Configuration.new
61
45
  end
62
-
63
46
  end
64
47
  end
@@ -0,0 +1,18 @@
1
+ module Endpointer
2
+ class Configuration
3
+
4
+ attr_accessor :invalidate, :cache_dir, :resource_config
5
+
6
+ DEFAULT_CONFIG_PATH = './endpointer.json'
7
+
8
+ def initialize(invalidate = nil, cache_dir = nil)
9
+ @invalidate = invalidate || false
10
+ @cache_dir = cache_dir || File.join(Dir.tmpdir, "endpointer_cache")
11
+ begin
12
+ @resource_config = File.read(DEFAULT_CONFIG_PATH)
13
+ rescue Errno::ENOENT
14
+ @resource_config = nil
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,23 @@
1
+ require 'json'
2
+ require 'endpointer/resource'
3
+
4
+ module Endpointer
5
+ class ResourceParser
6
+ def parse(resource_config)
7
+ parse_config(resource_config).map do |resource|
8
+ Resource.new(resource["id"], resource["method"].to_sym, resource["url"], resource["headers"])
9
+ end
10
+ end
11
+
12
+ private
13
+
14
+ def parse_config(config)
15
+ abort("Error: No config file present") if config.nil?
16
+ begin
17
+ JSON.parse(config)
18
+ rescue JSON::ParserError
19
+ abort("Error: The resource config is invalid")
20
+ end
21
+ end
22
+ end
23
+ end
@@ -1,3 +1,3 @@
1
1
  module Endpointer
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
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.4
4
+ version: 0.0.5
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-01-12 00:00:00.000000000 Z
11
+ date: 2017-01-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -170,17 +170,18 @@ files:
170
170
  - bin/console
171
171
  - bin/endpointer
172
172
  - endpointer.gemspec
173
+ - examples/endpointer.json
173
174
  - lib/endpointer.rb
174
175
  - lib/endpointer/app.rb
175
176
  - lib/endpointer/app_creator.rb
176
177
  - lib/endpointer/argument_parser.rb
177
178
  - lib/endpointer/cache_container.rb
178
179
  - lib/endpointer/cacher.rb
180
+ - lib/endpointer/configuration.rb
179
181
  - lib/endpointer/errors/cached_item_not_found_error.rb
180
182
  - lib/endpointer/errors/invalid_arguments_error.rb
181
183
  - lib/endpointer/errors/invalid_cache_dir_error.rb
182
184
  - lib/endpointer/errors/performer_not_found_error.rb
183
- - lib/endpointer/options.rb
184
185
  - lib/endpointer/performer_factory.rb
185
186
  - lib/endpointer/performers.rb
186
187
  - lib/endpointer/performers/get.rb
@@ -188,6 +189,7 @@ files:
188
189
  - lib/endpointer/performers/post.rb
189
190
  - lib/endpointer/resource.rb
190
191
  - lib/endpointer/resource_executor.rb
192
+ - lib/endpointer/resource_parser.rb
191
193
  - lib/endpointer/response.rb
192
194
  - lib/endpointer/response_presenter.rb
193
195
  - lib/endpointer/version.rb
@@ -203,7 +205,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
203
205
  requirements:
204
206
  - - ">="
205
207
  - !ruby/object:Gem::Version
206
- version: '0'
208
+ version: 2.2.0
207
209
  required_rubygems_version: !ruby/object:Gem::Requirement
208
210
  requirements:
209
211
  - - ">="
@@ -1,11 +0,0 @@
1
- module Endpointer
2
- class Options
3
-
4
- attr_accessor :invalidate, :cache_dir
5
-
6
- def initialize(invalidate = nil, cache_dir = nil)
7
- @invalidate = invalidate || false
8
- @cache_dir = cache_dir || File.join(Dir.tmpdir, "endpointer_cache")
9
- end
10
- end
11
- end