endpointer 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.ruby-version +1 -0
- data/.travis.yml +12 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +21 -0
- data/README.md +89 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/endpointer +6 -0
- data/bin/setup +8 -0
- data/endpointer.gemspec +32 -0
- data/lib/endpointer/app.rb +14 -0
- data/lib/endpointer/app_creator.rb +29 -0
- data/lib/endpointer/argument_parser.rb +50 -0
- data/lib/endpointer/cache_container.rb +6 -0
- data/lib/endpointer/cacher.rb +47 -0
- data/lib/endpointer/errors/cached_item_not_found_error.rb +7 -0
- data/lib/endpointer/errors/performer_not_found_error.rb +7 -0
- data/lib/endpointer/options.rb +4 -0
- data/lib/endpointer/performer_factory.rb +18 -0
- data/lib/endpointer/performers/get.rb +20 -0
- data/lib/endpointer/performers/method.rb +41 -0
- data/lib/endpointer/performers/post.rb +24 -0
- data/lib/endpointer/performers.rb +3 -0
- data/lib/endpointer/resource.rb +4 -0
- data/lib/endpointer/resource_executor.rb +24 -0
- data/lib/endpointer/response.rb +5 -0
- data/lib/endpointer/response_presenter.rb +28 -0
- data/lib/endpointer/version.rb +3 -0
- data/lib/endpointer.rb +16 -0
- metadata +217 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 309e7c6adba9e6eec3ebcbfcbf40056036e044eb
|
4
|
+
data.tar.gz: bc55a47865df1fcf8b610d86478bb06f07f3a913
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bacffc299387e41259df9c3ed29d6c4636ff98b91ae2c31d7c401cd215ac72d95b8b451956d37bd3b531ec44755e92f393536a3b2bc33a64a011e2420409b7a6
|
7
|
+
data.tar.gz: bdfcc59cdee9f13309c4992feff17fdc7e92a63d6cf8c8c28e5d4c4373b778b5e7664a1682ea8d133e7865c0a6618f2fa851850282e00594d51f963c2efca902
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.4.0
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at zen@kyprianou.eu. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Zen Kyprianou
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
# Endpointer
|
2
|
+
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/endpointer.svg)](http://badge.fury.io/rb/endpointer) [![Build Status](https://travis-ci.org/zenonas/endpointer.svg?branch=master)](https://travis-ci.org/zenonas/endpointer) [![Code Climate](https://codeclimate.com/github/zenonas/endpointer/badges/gpa.svg)](https://codeclimate.com/github/zenonas/endpointer) [![Test Coverage](https://codeclimate.com/github/zenonas/endpointer/badges/coverage.svg)](https://codeclimate.com/github/zenonas/endpointer/coverage) [![Issue Count](https://codeclimate.com/github/zenonas/endpointer/badges/issue_count.svg)](https://codeclimate.com/github/zenonas/endpointer)
|
4
|
+
|
5
|
+
Endpointer is a small gem that tries to act as a caching proxy between your dev machine and any web service. The motivation was to provide an easy to configure and use fake web service that is able to host host any amount of resources returning canned responses that are easy to modify. Please note the gem is still in early and active development and as such some features may be missing. For a list of features that are still missing consult the list at the end of this readme. If you would like a feature please feel free to raise an issue or a pull request. All contributions are welcome.
|
6
|
+
|
7
|
+
## Requirements
|
8
|
+
|
9
|
+
* Ruby 2.0+ (Tests running against MRI 2.4.0, 2.3.3, and JRuby 9.1.6.0)
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'endpointer'
|
17
|
+
```
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install endpointer
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
In order to use Endpointer you need to create a JSON configuration file with the following format
|
30
|
+
|
31
|
+
```json
|
32
|
+
[
|
33
|
+
{
|
34
|
+
"id": "some_unique_identifier_for_the_resource",
|
35
|
+
"method": "get",
|
36
|
+
"url": "http://httpbin.org/get",
|
37
|
+
"headers": {
|
38
|
+
"Authorization": "Bearer test",
|
39
|
+
"Accept": "application/json"
|
40
|
+
}
|
41
|
+
},
|
42
|
+
...
|
43
|
+
]
|
44
|
+
```
|
45
|
+
|
46
|
+
You can then invoke endpointer by executing
|
47
|
+
|
48
|
+
$ endpointer [--invalidate] <path_to_json_config_file>
|
49
|
+
|
50
|
+
Endpointer will attempt to return a cached resource by matching the resource requested with a cached version and if not available will call the real service.
|
51
|
+
In order to request for example the above resource from endpointer you would type:
|
52
|
+
|
53
|
+
$ curl http://localhost:4567/get
|
54
|
+
|
55
|
+
If the request is to be executed against the real service the headers defined in the config will be used with their default values unless overridden as in the following example:
|
56
|
+
|
57
|
+
$ curl -H "Accept: text/plain" http://localhost:4567/get
|
58
|
+
|
59
|
+
### Caching
|
60
|
+
|
61
|
+
By default endpointer will use your operating systems temp directory to store its cache files `(TMP_DIR/endpointer_cache)`. I plan on making this configurable in the future. Possibly as a parameter.
|
62
|
+
|
63
|
+
You can provide the `--invalidate` flag to the command line to invalidate the cache. This empties the endpointer_cache directory
|
64
|
+
|
65
|
+
## Development
|
66
|
+
|
67
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
68
|
+
|
69
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
70
|
+
|
71
|
+
## Contributing
|
72
|
+
|
73
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/zenonas/endpointer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
74
|
+
|
75
|
+
### Upcoming features
|
76
|
+
|
77
|
+
As mentioned above I'm actively going to work on improving endpointer and the following are a few features that I would like to see implemented. Feel free to suggest new ones or work on one your self.
|
78
|
+
|
79
|
+
* Custom cache path
|
80
|
+
* The ability to easily edit cached files. Maybe a second executable that allows you to edit the canned responses in a pretty JSON format
|
81
|
+
* Support all HTTP verbs(currently only GET, POST are supported methods)
|
82
|
+
* 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.
|
83
|
+
* The ability to run headless on a service to make this a deployable fake web service
|
84
|
+
* Make the port configurable
|
85
|
+
|
86
|
+
## License
|
87
|
+
|
88
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
89
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "endpointer"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/endpointer
ADDED
data/bin/setup
ADDED
data/endpointer.gemspec
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'endpointer/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "endpointer"
|
8
|
+
spec.version = Endpointer::VERSION
|
9
|
+
spec.authors = ["Zen Kyprianou"]
|
10
|
+
spec.email = ["endpointer@zenonas.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Faster dev environment with this simple caching proxy DSL}
|
13
|
+
spec.description = %q{Allows you to define endpoints to serve via a simple caching proxy dsl}
|
14
|
+
spec.homepage = "https://github.com/zenonas/endpointer"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.executables = ["endpointer"]
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
24
|
+
spec.add_development_dependency "pry"
|
25
|
+
spec.add_development_dependency 'codeclimate-test-reporter'
|
26
|
+
spec.add_development_dependency 'simplecov'
|
27
|
+
spec.add_development_dependency "rack-test"
|
28
|
+
spec.add_development_dependency "webmock"
|
29
|
+
|
30
|
+
spec.add_dependency "rest-client"
|
31
|
+
spec.add_dependency "sinatra"
|
32
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
|
3
|
+
module Endpointer
|
4
|
+
class App < Sinatra::Base
|
5
|
+
|
6
|
+
not_found do
|
7
|
+
content_type 'application/json'
|
8
|
+
JSON.generate({
|
9
|
+
'error' => 'URL not configured. You need to configure all urls that Endpointer will listen to in your json file'
|
10
|
+
})
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'endpointer/app'
|
2
|
+
require 'endpointer/resource_executor'
|
3
|
+
require 'uri'
|
4
|
+
|
5
|
+
module Endpointer
|
6
|
+
class AppCreator
|
7
|
+
|
8
|
+
def create(resources, options)
|
9
|
+
resources.each do |resource|
|
10
|
+
app.send(resource.method, path(resource.url)) do
|
11
|
+
executor_response = Endpointer::ResourceExecutor.new.perform(request, resource, options)
|
12
|
+
headers executor_response.headers
|
13
|
+
executor_response.body
|
14
|
+
end
|
15
|
+
end
|
16
|
+
app
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def app
|
22
|
+
@app ||= Sinatra.new(Endpointer::App)
|
23
|
+
end
|
24
|
+
|
25
|
+
def path(url)
|
26
|
+
URI.parse(url).path
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'endpointer/resource'
|
2
|
+
require 'endpointer/options'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module Endpointer
|
6
|
+
class ArgumentParser
|
7
|
+
|
8
|
+
def initialize(arguments)
|
9
|
+
@arguments = arguments
|
10
|
+
end
|
11
|
+
|
12
|
+
def parse_resources
|
13
|
+
parse_config(config_file).map do |resource|
|
14
|
+
Resource.new(resource["id"], resource["method"].to_sym, resource["url"], resource["headers"])
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def parse_options
|
19
|
+
options = @arguments.select { |argument| option_argument?(argument)}
|
20
|
+
build_options_from(options)
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def build_options_from(parsed_options)
|
26
|
+
invalidate = parsed_options.include?("--invalidate")
|
27
|
+
Options.new(invalidate)
|
28
|
+
end
|
29
|
+
|
30
|
+
def config_file
|
31
|
+
@arguments.find { |argument| config_file?(argument) }
|
32
|
+
end
|
33
|
+
|
34
|
+
def parse_config(config_file)
|
35
|
+
JSON.parse(File.read(config_file))
|
36
|
+
end
|
37
|
+
|
38
|
+
def resources_from_config_file
|
39
|
+
end
|
40
|
+
|
41
|
+
def option_argument?(argument)
|
42
|
+
argument.match(/^--.*/)
|
43
|
+
end
|
44
|
+
|
45
|
+
def config_file?(argument)
|
46
|
+
argument.match(/.json$/) || File.exists?(argument)
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'endpointer/errors/cached_item_not_found_error'
|
2
|
+
require 'endpointer/cache_container'
|
3
|
+
require 'yaml'
|
4
|
+
|
5
|
+
module Endpointer
|
6
|
+
class Cacher
|
7
|
+
|
8
|
+
def initialize(path)
|
9
|
+
initialize_path(path)
|
10
|
+
end
|
11
|
+
|
12
|
+
def get(resource)
|
13
|
+
cache_container = retrieve_cache_container(resource)
|
14
|
+
raise Endpointer::Errors::CachedItemNotFoundError unless cache_container.resource == resource
|
15
|
+
cache_container.response
|
16
|
+
end
|
17
|
+
|
18
|
+
def set(resource, response)
|
19
|
+
cache_container = create_cache_container(resource, response)
|
20
|
+
File.write(File.join(@path, "#{resource.id}.yml"), YAML.dump(cache_container))
|
21
|
+
end
|
22
|
+
|
23
|
+
def invalidate
|
24
|
+
FileUtils.remove_entry(@path)
|
25
|
+
initialize_path(@path)
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def create_cache_container(resource, response)
|
31
|
+
Endpointer::CacheContainer.new(resource, response, Time.now.utc)
|
32
|
+
end
|
33
|
+
|
34
|
+
def retrieve_cache_container(resource)
|
35
|
+
begin
|
36
|
+
YAML.load(File.read(File.join(@path, "#{resource.id}.yml")))
|
37
|
+
rescue
|
38
|
+
raise Endpointer::Errors::CachedItemNotFoundError
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def initialize_path(path)
|
43
|
+
@path = path
|
44
|
+
Dir.mkdir(@path) unless File.exists?(@path)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'endpointer/performers'
|
2
|
+
require 'endpointer/errors/performer_not_found_error'
|
3
|
+
|
4
|
+
module Endpointer
|
5
|
+
class PerformerFactory
|
6
|
+
|
7
|
+
PERFORMERS = {
|
8
|
+
get: Endpointer::Performers::Get,
|
9
|
+
post: Endpointer::Performers::Post
|
10
|
+
}
|
11
|
+
|
12
|
+
def self.create(method)
|
13
|
+
PERFORMERS.fetch(method) {
|
14
|
+
raise Endpointer::Errors::PerformerNotFoundError.new 'The method you selected has no performer to execute it. Make sure you only use supported methods'
|
15
|
+
}.new
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'endpointer/performers/method'
|
2
|
+
require 'endpointer/response_presenter'
|
3
|
+
require 'rest-client'
|
4
|
+
|
5
|
+
module Endpointer
|
6
|
+
module Performers
|
7
|
+
class Get < Method
|
8
|
+
|
9
|
+
def execute(request, resource)
|
10
|
+
begin
|
11
|
+
url = File.join(create_hostname(resource), create_path(request))
|
12
|
+
response = RestClient::Request.execute(method: :get, url: url, headers: create_headers(request, resource))
|
13
|
+
rescue RestClient::ExceptionWithResponse => e
|
14
|
+
response = e.response
|
15
|
+
end
|
16
|
+
Endpointer::ResponsePresenter.new.present(status: response.code, body: response.body, headers: response.headers)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'endpointer/response'
|
2
|
+
require 'uri'
|
3
|
+
|
4
|
+
module Endpointer
|
5
|
+
module Performers
|
6
|
+
class Method
|
7
|
+
|
8
|
+
def execute(request, resource)
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def create_headers(request, resource)
|
14
|
+
resource.headers.keys.each_with_object({}) do |key, header|
|
15
|
+
header[key] = request_header_or_default(key, request, resource)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def request_header_or_default(key, request, resource)
|
20
|
+
request.env[rack_header_name_convert(key)] || resource.headers[key]
|
21
|
+
end
|
22
|
+
|
23
|
+
def rack_header_name_convert(header_name)
|
24
|
+
"HTTP_#{header_name.upcase.tr('-', '_')}"
|
25
|
+
end
|
26
|
+
|
27
|
+
def create_hostname(resource)
|
28
|
+
host = URI.parse(resource.url).host
|
29
|
+
port = URI.parse(resource.url).port
|
30
|
+
"#{host}:#{port}"
|
31
|
+
end
|
32
|
+
|
33
|
+
def create_path(request)
|
34
|
+
path = request.path
|
35
|
+
query_string = request.env['QUERY_STRING']
|
36
|
+
path << "?#{query_string}" unless query_string.nil?
|
37
|
+
path
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'endpointer/performers/method'
|
2
|
+
require 'endpointer/response_presenter'
|
3
|
+
require 'rest-client'
|
4
|
+
|
5
|
+
module Endpointer
|
6
|
+
module Performers
|
7
|
+
class Post < Method
|
8
|
+
def execute(request, resource)
|
9
|
+
begin
|
10
|
+
url = File.join(create_hostname(resource), create_path(request))
|
11
|
+
response = RestClient.post(
|
12
|
+
url,
|
13
|
+
request.body.string,
|
14
|
+
create_headers(request, resource)
|
15
|
+
)
|
16
|
+
rescue RestClient::ExceptionWithResponse => e
|
17
|
+
response = e.response
|
18
|
+
end
|
19
|
+
|
20
|
+
Endpointer::ResponsePresenter.new.present(status: response.code, body: response.body, headers: response.headers)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'endpointer/cacher'
|
2
|
+
require 'endpointer/performer_factory'
|
3
|
+
|
4
|
+
module Endpointer
|
5
|
+
class ResourceExecutor
|
6
|
+
|
7
|
+
def perform(request, resource, options)
|
8
|
+
begin
|
9
|
+
cache.get(resource)
|
10
|
+
rescue Endpointer::Errors::CachedItemNotFoundError => e
|
11
|
+
response = Endpointer::PerformerFactory.create(resource.method).execute(request, resource)
|
12
|
+
cache.set(resource, response)
|
13
|
+
response
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def cache
|
20
|
+
@cache ||= Endpointer::Cacher.new(Endpointer::CACHE_DIR)
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Endpointer
|
2
|
+
class ResponsePresenter
|
3
|
+
|
4
|
+
def present(status: , body: , headers: )
|
5
|
+
Response.new(
|
6
|
+
status,
|
7
|
+
body,
|
8
|
+
sanitise_headers(uglify_headers(headers))
|
9
|
+
)
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def uglify_headers(headers)
|
15
|
+
headers.inject({}) { |out, (key, value)|
|
16
|
+
out[key.to_s.upcase.gsub('_', '-')] = value
|
17
|
+
out
|
18
|
+
}
|
19
|
+
end
|
20
|
+
|
21
|
+
def sanitise_headers(headers)
|
22
|
+
headers.reject {|header, value|
|
23
|
+
header.match(/TRANSFER-ENCODING/)
|
24
|
+
}
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
data/lib/endpointer.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require "endpointer/version"
|
2
|
+
require "endpointer/argument_parser"
|
3
|
+
require "endpointer/app_creator"
|
4
|
+
|
5
|
+
module Endpointer
|
6
|
+
|
7
|
+
CACHE_DIR = File.join(Dir.tmpdir, "endpointer_cache")
|
8
|
+
|
9
|
+
def self.run(arguments)
|
10
|
+
argument_parser = ArgumentParser.new(arguments)
|
11
|
+
options = argument_parser.parse_options
|
12
|
+
Cacher.new(CACHE_DIR).invalidate if options.invalidate
|
13
|
+
app = AppCreator.new.create(argument_parser.parse_resources, options)
|
14
|
+
app.run!
|
15
|
+
end
|
16
|
+
end
|
metadata
ADDED
@@ -0,0 +1,217 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: endpointer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Zen Kyprianou
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-12-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.12'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: codeclimate-test-reporter
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: simplecov
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rack-test
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: webmock
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: rest-client
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :runtime
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: sinatra
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :runtime
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
description: Allows you to define endpoints to serve via a simple caching proxy dsl
|
154
|
+
email:
|
155
|
+
- endpointer@zenonas.com
|
156
|
+
executables:
|
157
|
+
- endpointer
|
158
|
+
extensions: []
|
159
|
+
extra_rdoc_files: []
|
160
|
+
files:
|
161
|
+
- ".gitignore"
|
162
|
+
- ".rspec"
|
163
|
+
- ".ruby-version"
|
164
|
+
- ".travis.yml"
|
165
|
+
- CODE_OF_CONDUCT.md
|
166
|
+
- Gemfile
|
167
|
+
- LICENSE.txt
|
168
|
+
- README.md
|
169
|
+
- Rakefile
|
170
|
+
- bin/console
|
171
|
+
- bin/endpointer
|
172
|
+
- bin/setup
|
173
|
+
- endpointer.gemspec
|
174
|
+
- lib/endpointer.rb
|
175
|
+
- lib/endpointer/app.rb
|
176
|
+
- lib/endpointer/app_creator.rb
|
177
|
+
- lib/endpointer/argument_parser.rb
|
178
|
+
- lib/endpointer/cache_container.rb
|
179
|
+
- lib/endpointer/cacher.rb
|
180
|
+
- lib/endpointer/errors/cached_item_not_found_error.rb
|
181
|
+
- lib/endpointer/errors/performer_not_found_error.rb
|
182
|
+
- lib/endpointer/options.rb
|
183
|
+
- lib/endpointer/performer_factory.rb
|
184
|
+
- lib/endpointer/performers.rb
|
185
|
+
- lib/endpointer/performers/get.rb
|
186
|
+
- lib/endpointer/performers/method.rb
|
187
|
+
- lib/endpointer/performers/post.rb
|
188
|
+
- lib/endpointer/resource.rb
|
189
|
+
- lib/endpointer/resource_executor.rb
|
190
|
+
- lib/endpointer/response.rb
|
191
|
+
- lib/endpointer/response_presenter.rb
|
192
|
+
- lib/endpointer/version.rb
|
193
|
+
homepage: https://github.com/zenonas/endpointer
|
194
|
+
licenses:
|
195
|
+
- MIT
|
196
|
+
metadata: {}
|
197
|
+
post_install_message:
|
198
|
+
rdoc_options: []
|
199
|
+
require_paths:
|
200
|
+
- lib
|
201
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
202
|
+
requirements:
|
203
|
+
- - ">="
|
204
|
+
- !ruby/object:Gem::Version
|
205
|
+
version: '0'
|
206
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
207
|
+
requirements:
|
208
|
+
- - ">="
|
209
|
+
- !ruby/object:Gem::Version
|
210
|
+
version: '0'
|
211
|
+
requirements: []
|
212
|
+
rubyforge_project:
|
213
|
+
rubygems_version: 2.6.8
|
214
|
+
signing_key:
|
215
|
+
specification_version: 4
|
216
|
+
summary: Faster dev environment with this simple caching proxy DSL
|
217
|
+
test_files: []
|