kapellmeister 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.editorconfig +18 -0
- data/.gitignore +12 -0
- data/.rubocop.yml +45 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +26 -0
- data/README.md +79 -0
- data/Rakefile +3 -0
- data/bin/console +12 -0
- data/bin/setup +7 -0
- data/kapellmeister.gemspec +33 -0
- data/lib/generators/kapellmeister/add_service_generator.rb +52 -0
- data/lib/generators/kapellmeister/templates/initializers/third_party_initializer.rb.tt +7 -0
- data/lib/generators/kapellmeister/templates/lib/third_party/client.rb.tt +1 -0
- data/lib/generators/kapellmeister/templates/lib/third_party/configuration.rb.tt +16 -0
- data/lib/generators/kapellmeister/templates/lib/third_party/responder.rb.tt +0 -0
- data/lib/generators/kapellmeister/templates/lib/third_party/routes.yml.tt +11 -0
- data/lib/generators/kapellmeister/templates/lib/third_party.rb.tt +14 -0
- data/lib/kapellmeister/base.rb +54 -0
- data/lib/kapellmeister/dispatcher.rb +80 -0
- data/lib/kapellmeister/requests_extension.rb +25 -0
- data/lib/kapellmeister/responder.rb +30 -0
- data/lib/kapellmeister/version.rb +3 -0
- data/lib/kapellmeister.rb +1 -0
- metadata +52 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 00ad63e15092782d16bc142285ca607c2431195140be3b4e36ec7748d912c5a6
|
4
|
+
data.tar.gz: a40c93cc46323f5e702313e9fe889e0d9733e2446dfca525badff7edf87d4d1f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 34c19a5f6b955bab605f2af31a9b4f33c242ee684b0edfa6affce868a0dcedbd73c041733e40d1b12f12f50efb3b81d5351164f6ab1b27d63a8523b2f2e39e3e
|
7
|
+
data.tar.gz: 4279bb8b32269f64d1ddcedbd28ad6fea79b89c346892f8f54401f33d1b602019560ba2459f714e3814ddecb2129e6ca3d3c6979268b54c236252deed5acbe0e
|
data/.editorconfig
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# EditorConfig helps developers define and maintain consistent
|
2
|
+
# coding styles between different editors and IDEs
|
3
|
+
# editorconfig.org
|
4
|
+
|
5
|
+
root = true
|
6
|
+
[*]
|
7
|
+
# Change these settings to your own preference
|
8
|
+
indent_style = space
|
9
|
+
indent_size = 2
|
10
|
+
|
11
|
+
# We recommend you to keep these unchanged
|
12
|
+
end_of_line = lf
|
13
|
+
charset = utf-8
|
14
|
+
trim_trailing_whitespace = true
|
15
|
+
insert_final_newline = true
|
16
|
+
|
17
|
+
[*.md]
|
18
|
+
trim_trailing_whitespace = false
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require:
|
2
|
+
- rubocop-performance
|
3
|
+
- rubocop-rspec
|
4
|
+
|
5
|
+
AllCops:
|
6
|
+
TargetRubyVersion: 3.1.1
|
7
|
+
DisplayCopNames: true
|
8
|
+
NewCops: enable
|
9
|
+
|
10
|
+
Exclude:
|
11
|
+
- bin/**/*
|
12
|
+
- db/**/*
|
13
|
+
- test/**/*
|
14
|
+
- node_modules/**/*
|
15
|
+
- tmp/**/*
|
16
|
+
- Gemfile
|
17
|
+
- vendor/**/*
|
18
|
+
- gemfiles/**/*
|
19
|
+
|
20
|
+
Style/FrozenStringLiteralComment:
|
21
|
+
Enabled: false
|
22
|
+
Style/Documentation:
|
23
|
+
Enabled: false
|
24
|
+
Style/DoubleNegation:
|
25
|
+
Enabled: false
|
26
|
+
Style/ClassAndModuleChildren:
|
27
|
+
EnforcedStyle: compact
|
28
|
+
|
29
|
+
Metrics/AbcSize:
|
30
|
+
Max: 25
|
31
|
+
Layout/LineLength:
|
32
|
+
Max: 120
|
33
|
+
Metrics/MethodLength:
|
34
|
+
Max: 25
|
35
|
+
Metrics/BlockLength:
|
36
|
+
Max: 30
|
37
|
+
Exclude:
|
38
|
+
- "Rakefile"
|
39
|
+
- "**/*.rake"
|
40
|
+
- "spec/**/*.rb"
|
41
|
+
|
42
|
+
Performance/MethodObjectAsBlock:
|
43
|
+
Enabled: false
|
44
|
+
Performance/OpenStruct:
|
45
|
+
Enabled: true
|
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
kapellmeister
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-3.1.1
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
|
3
|
+
ruby '3.1.1'
|
4
|
+
|
5
|
+
gemspec
|
6
|
+
|
7
|
+
# debug
|
8
|
+
group :development do
|
9
|
+
gem 'byebug'
|
10
|
+
gem 'web-console'
|
11
|
+
gem 'listen'
|
12
|
+
gem 'rerun', git: 'https://github.com/alexch/rerun.git'
|
13
|
+
end
|
14
|
+
|
15
|
+
# listings
|
16
|
+
group :development, :test do
|
17
|
+
gem 'rubocop', require: false
|
18
|
+
gem 'rubocop-performance'
|
19
|
+
gem 'rubocop-rspec'
|
20
|
+
gem 'rubycritic', require: false
|
21
|
+
gem 'ruby_gntp'
|
22
|
+
end
|
23
|
+
|
24
|
+
source 'https://rubygems.org'
|
25
|
+
|
26
|
+
gemspec
|
data/README.md
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
# HTTP requests dispatcher
|
2
|
+
|
3
|
+
This template-service allows you to define http requests to a third party through a lightweight set of instructions, including a route parser in yaml format
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
Add kapellmeister to your Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'kapellmeister', '~> 0.0.1', require: false
|
11
|
+
```
|
12
|
+
|
13
|
+
### Add new third party configuration:
|
14
|
+
|
15
|
+
$ bin/rails g kapellmeister:add_service %<ThirdPartyName> %<options> --%<flags>
|
16
|
+
|
17
|
+
`ThirdPartyName` — Pass the lib name, either CamelCased or under_scored
|
18
|
+
`options` — Pass the configuration keys, usually host, key and version
|
19
|
+
`flags` — This generator have one flag. This flag is `responder`, default is `false`. If you set to `true` will be generated responder.rb used for parsing response.
|
20
|
+
|
21
|
+
All the instructions are lightweight files in your /lib folder. Here's the example of structure:
|
22
|
+
|
23
|
+
``` Capfile
|
24
|
+
└── app
|
25
|
+
└── lib
|
26
|
+
└── third_party_service
|
27
|
+
├── client.rb
|
28
|
+
├── configuration.rb
|
29
|
+
├── responder.rb
|
30
|
+
└── routes.yml
|
31
|
+
└── third_party_service.rb
|
32
|
+
└── initializers
|
33
|
+
└── third_party_service.rb
|
34
|
+
```
|
35
|
+
|
36
|
+
initializers/third_party_service.rb
|
37
|
+
|
38
|
+
If you use the Rails gem you have the `initializers` folder in your application. Add the secret keys to config.
|
39
|
+
|
40
|
+
app/lib/third_party_service.rb
|
41
|
+
|
42
|
+
Main file of your integration. Make it module and include the Kapellmeister::Base
|
43
|
+
|
44
|
+
app/lib/third_party_service
|
45
|
+
|
46
|
+
Folder contains `routes scheme`, `client`, `configuration` and optional `responder`.
|
47
|
+
|
48
|
+
`routes.yml` — Routes to third party in nested format.
|
49
|
+
|
50
|
+
``` yaml
|
51
|
+
foo: => Wrapper for method
|
52
|
+
bar: => Method name
|
53
|
+
scheme: => Scheme description
|
54
|
+
method: POST => Request type
|
55
|
+
use_wrapper: true => Default true
|
56
|
+
path: buz => Real path
|
57
|
+
mock: => Mock for development
|
58
|
+
token: blablabla
|
59
|
+
|
60
|
+
# ThirdParty::Client.foo_bar { a: 'b' } => POST https://third_party.com/foo/buz DATA: { a: 'b' }
|
61
|
+
```
|
62
|
+
|
63
|
+
`client.rb` — Nested from main dispatcher and you can add some configuration methods, custom headers and requests options.
|
64
|
+
|
65
|
+
`configuration.rb` — Add path to third party, config url and logger
|
66
|
+
|
67
|
+
`responder.rb` — By default uses standard responders parsed response in json. But you can write your own.
|
68
|
+
|
69
|
+
## Contributing
|
70
|
+
|
71
|
+
Pull requests welcome: fork, make a topic branch, commit (squash when possible) *with tests* and I'll happily consider.
|
72
|
+
|
73
|
+
## License
|
74
|
+
|
75
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
76
|
+
|
77
|
+
## Copyright
|
78
|
+
|
79
|
+
Copyright (c) 2022 Denis Arushanov aka DarkWater
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
ßrequire "bundler/setup"
|
2
|
+
require "locum_best_practices"
|
3
|
+
|
4
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
5
|
+
# with your gem easier. You can also use a different console, if you like.
|
6
|
+
|
7
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
8
|
+
# require "pry"
|
9
|
+
# Pry.start
|
10
|
+
|
11
|
+
require "irb"
|
12
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'kapellmeister/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |gem|
|
6
|
+
gem.name = 'kapellmeister'
|
7
|
+
gem.version = Kapellmeister::VERSION
|
8
|
+
gem.authors = %w[DarkWater]
|
9
|
+
gem.email = %w[denis.arushanov@darkcreative.ru]
|
10
|
+
|
11
|
+
gem.summary = 'HTTP requests dispatcher'
|
12
|
+
gem.description = 'Contains third party routes parser'
|
13
|
+
gem.homepage = 'https://github.com/DarkWater666/kapellmeister'
|
14
|
+
|
15
|
+
gem.license = 'MIT'
|
16
|
+
|
17
|
+
if gem.respond_to?(:metadata)
|
18
|
+
gem.metadata['allowed_push_host'] = 'https://rubygems.org'
|
19
|
+
else
|
20
|
+
fail 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
|
21
|
+
end
|
22
|
+
|
23
|
+
gem.files = `git ls-files -z`.split("\x0").reject { |f| f.match(/^(test|spec|features)/) }
|
24
|
+
gem.bindir = 'exe'
|
25
|
+
gem.executables = gem.files.grep(/^exe/) { |f| File.basename(f) }
|
26
|
+
gem.require_paths = %w[lib]
|
27
|
+
|
28
|
+
gem.required_ruby_version = '>= 2.4.2'
|
29
|
+
|
30
|
+
gem.add_development_dependency 'bundler', '~> 2.0', '>= 2.0.2'
|
31
|
+
gem.add_development_dependency 'redcarpet', '~> 1.17', '>= 1.17.0'
|
32
|
+
gem.add_development_dependency 'yard', '~> 0.7', '>= 0.7.5'
|
33
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module Kapellmeister
|
2
|
+
class AddServiceGenerator < Rails::Generators::NamedBase
|
3
|
+
source_root File.expand_path('templates', __dir__)
|
4
|
+
|
5
|
+
desc <<-EOF
|
6
|
+
Prepares application to possibility to get third parties requests.
|
7
|
+
EOF
|
8
|
+
|
9
|
+
class_option :responder, type: :boolean, default: false
|
10
|
+
|
11
|
+
argument :attributes, type: :array, default: [], banner: 'attribute'
|
12
|
+
|
13
|
+
def copy_initializer_file
|
14
|
+
template 'initializers/add_service_initializer.rb', "config/initializers/#{file_name}.rb"
|
15
|
+
end
|
16
|
+
|
17
|
+
def copy_base_file
|
18
|
+
template 'lib/add_service.rb', "app/lib/#{file_name}.rb"
|
19
|
+
end
|
20
|
+
|
21
|
+
def copy_lib_folder
|
22
|
+
copy_client_file
|
23
|
+
copy_configuration_file
|
24
|
+
copy_responder_file
|
25
|
+
copy_routes_file
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def copy_client_file
|
31
|
+
template 'lib/add_service/client.rb', "app/lib/#{file_name}/client.rb"
|
32
|
+
end
|
33
|
+
|
34
|
+
def copy_configuration_file
|
35
|
+
template 'lib/add_service/configuration.rb', "app/lib/#{file_name}/configuration.rb"
|
36
|
+
end
|
37
|
+
|
38
|
+
def copy_responder_file
|
39
|
+
return unless options[:responder]
|
40
|
+
|
41
|
+
template 'lib/add_service/responder.rb', "app/lib/#{file_name}/responder.rb"
|
42
|
+
end
|
43
|
+
|
44
|
+
def copy_routes_file
|
45
|
+
template 'lib/add_service/routes.yml', "app/lib/#{file_name}/routes.yml"
|
46
|
+
end
|
47
|
+
|
48
|
+
def initialize_signatures
|
49
|
+
attributes.map { |attr| attr.name.to_sym }
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
class <%= class_name %>::Client < Kapellmeister::Dispatcher; end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class <%= class_name %>::Configuration
|
2
|
+
attr_accessor <%= initialize_signatures.join(', ') %>
|
3
|
+
|
4
|
+
def initialize
|
5
|
+
@path = ''
|
6
|
+
@logger = ::Logger.new(Rails.env.development? ? $stdout : 'log/<%=file_name %>.log')
|
7
|
+
end
|
8
|
+
|
9
|
+
def url
|
10
|
+
raise "No given host to #{self.class.name}" unless host
|
11
|
+
|
12
|
+
https = ssl.presence || true
|
13
|
+
|
14
|
+
"URI::HTTP#{https.to_b ? 'S' : ''}".build(host:, path: [path, version].join('/'))
|
15
|
+
end
|
16
|
+
end
|
File without changes
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# NOTE: Small docs
|
2
|
+
# foo: => Wrapper for method
|
3
|
+
# bar: => Method name
|
4
|
+
# scheme: => Scheme description
|
5
|
+
# method: POST => Request type
|
6
|
+
# use_wrapper: true => Default true
|
7
|
+
# path: buz => Real path
|
8
|
+
# mock: => Mock for development
|
9
|
+
# token: blablabla
|
10
|
+
#
|
11
|
+
# Client.foo_bar { a: 'b' } => POST http://host/foo/buz DATA: { a: 'b' }
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module <%= class_name %>
|
2
|
+
ROUTES = ::Kapellmeister::Base.routes_scheme_parse('app/lib/<%= file_name %>/routes.yml')
|
3
|
+
|
4
|
+
class << self
|
5
|
+
include ::Kapellmeister::Base
|
6
|
+
|
7
|
+
<%- initialize_signatures.each do |attr| -%>
|
8
|
+
<%= "def #{attr}" %>
|
9
|
+
<%= "@#{attr} ||= configuration.#{attr}" %>
|
10
|
+
<%= "end" %>
|
11
|
+
|
12
|
+
<%- end -%>
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module Kapellmeister::Base
|
2
|
+
def configuration
|
3
|
+
@configuration ||= self::Configuration.new
|
4
|
+
end
|
5
|
+
|
6
|
+
def report(data)
|
7
|
+
responder.new(data)
|
8
|
+
end
|
9
|
+
|
10
|
+
def configure
|
11
|
+
yield(configuration)
|
12
|
+
end
|
13
|
+
|
14
|
+
def logger
|
15
|
+
@logger ||= configuration.logger
|
16
|
+
end
|
17
|
+
|
18
|
+
def requests
|
19
|
+
@requests ||= generate_routes(self::ROUTES).transform_values! do |value|
|
20
|
+
value.except!(:use_wrapper)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def responder
|
25
|
+
@responder ||= defined?(self::Responder) ? self::Responder : Kapellmeister::Responder
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.routes_scheme_parse(path)
|
29
|
+
YAML.safe_load(ERB.new(File.read(path)).result, aliases: true, permitted_classes: [Time]).deep_symbolize_keys
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def generate_routes(json_scheme)
|
34
|
+
json_scheme.each_with_object({}) do |(key, value), scheme|
|
35
|
+
scheme[key] = value.delete(:scheme) if (value.is_a?(Hash) && value.key?(:scheme)) || value.is_a?(String)
|
36
|
+
next if value.length.zero?
|
37
|
+
|
38
|
+
generate_routes(value).map { |deep_key, deep_value| mapping(deep_key, deep_value, key, scheme) }
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def mapping(deep_key, deep_value, key, scheme)
|
43
|
+
old_path = deep_value[:path]
|
44
|
+
name = old_path.split('/').map { |part| part.gsub(/%<.*?>/, '') }.reject(&:empty?)
|
45
|
+
deep_value[:path] = [key, old_path].join('/')
|
46
|
+
|
47
|
+
use_wrapper = deep_value.key?(:use_wrapper) ? deep_value[:use_wrapper] : true
|
48
|
+
new_key = if name.size == 1
|
49
|
+
deep_key
|
50
|
+
else
|
51
|
+
use_wrapper ? [name.first.presence || key, deep_key].join('_').to_sym : deep_key
|
52
|
+
end
|
53
|
+
scheme[new_key] = deep_value
|
54
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require 'faraday_middleware'
|
2
|
+
|
3
|
+
class Kapellmeister::Dispatcher
|
4
|
+
def self.inherited(base)
|
5
|
+
base.extend(RequestsExtension)
|
6
|
+
|
7
|
+
delegate :report, :configuration, :logger, to: base.module_parent
|
8
|
+
|
9
|
+
def custom_headers
|
10
|
+
@custom_headers ||= try(:headers) || {}
|
11
|
+
end
|
12
|
+
|
13
|
+
def custom_request_options
|
14
|
+
@custom_request_options ||= try(:request_options) || {}
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
FailedResponse = Struct.new(:success?, :response, :payload)
|
19
|
+
|
20
|
+
def connection_by(method, path, data = {})
|
21
|
+
headers_params = data.delete(:headers) || {}
|
22
|
+
requests_data = data.delete(:request) || {}
|
23
|
+
|
24
|
+
generated_connection = connection(additional_headers: headers_params, requests_data:)
|
25
|
+
|
26
|
+
case method.upcase.to_sym
|
27
|
+
when :GET
|
28
|
+
process generated_connection.get([path, data.to_query].compact_blank!.join('?'))
|
29
|
+
when :POST
|
30
|
+
process generated_connection.post(path, data.to_json)
|
31
|
+
when :PUT
|
32
|
+
process generated_connection.put(path, data.to_json)
|
33
|
+
else
|
34
|
+
raise "Library can't process method #{method} yet"
|
35
|
+
end
|
36
|
+
rescue Faraday::ConnectionFailed, Faraday::TimeoutError => e
|
37
|
+
failed_response(details: e.message)
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def connection(additional_headers:, requests_data:)
|
43
|
+
::Faraday.new(url: configuration.url,
|
44
|
+
headers: headers_generate(**additional_headers),
|
45
|
+
request: requests_generate(**requests_data)) do |faraday|
|
46
|
+
faraday.request :authorization, *authorization
|
47
|
+
faraday.request :json, content_type: 'application/json'
|
48
|
+
faraday.request :multipart
|
49
|
+
faraday.response :logger, logger
|
50
|
+
faraday.response :json, content_type: 'application/json; charset=utf-8'
|
51
|
+
faraday.adapter :typhoeus do |http|
|
52
|
+
http.timeout = 20
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def headers_generate(**additional)
|
58
|
+
{
|
59
|
+
authority: Credentials.send_it.host,
|
60
|
+
accept: 'application/json, text/plain, */*',
|
61
|
+
**additional,
|
62
|
+
**custom_headers
|
63
|
+
}
|
64
|
+
end
|
65
|
+
|
66
|
+
def requests_generate(**requests_data)
|
67
|
+
{
|
68
|
+
**requests_data,
|
69
|
+
**custom_request_options
|
70
|
+
}
|
71
|
+
end
|
72
|
+
|
73
|
+
def process(data)
|
74
|
+
report(data).result
|
75
|
+
end
|
76
|
+
|
77
|
+
def failed_response(**args)
|
78
|
+
FailedResponse.new(false, { message: "#{self.class} no connection" }, { status: 555, **args })
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Kapellmeister::RequestsExtension
|
2
|
+
def self.request_processing
|
3
|
+
proc do |name, request_data|
|
4
|
+
define_method name do |data = {}|
|
5
|
+
proc { |method:, path:, mock:| # rubocop:disable Lint/UnusedBlockArgument:
|
6
|
+
# return mock if ENV['APP_ENV'] == 'development'
|
7
|
+
|
8
|
+
path = path.split('/').map do |part|
|
9
|
+
next part unless part.include? '%<'
|
10
|
+
|
11
|
+
data.delete(part.match(/%<(.*)>/).to_a.last.to_sym)
|
12
|
+
end.join('/')
|
13
|
+
|
14
|
+
new.connection_by(method, path, data)
|
15
|
+
}.call(**request_data)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
rescue NoMethodError
|
19
|
+
raise "You need to define #{self} class with connection_by method"
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.extended(base)
|
23
|
+
base.module_parent.requests.each(&request_processing)
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
class Kapellmeister::Responder
|
2
|
+
Result = Struct.new(:success?, :response, :payload)
|
3
|
+
attr_reader :response, :payload
|
4
|
+
|
5
|
+
delegate :body, :status, to: :response
|
6
|
+
|
7
|
+
def initialize(response, **args)
|
8
|
+
@response = response
|
9
|
+
@payload = args
|
10
|
+
end
|
11
|
+
|
12
|
+
def result
|
13
|
+
error = !/2\d{2}/.match?(status.to_s)
|
14
|
+
|
15
|
+
Result.new(!error, parsed_body, { status: status }.merge(payload))
|
16
|
+
rescue JSON::ParserError => e
|
17
|
+
Result.new(false, e)
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def parsed_body
|
23
|
+
return body if body.length == 0
|
24
|
+
|
25
|
+
case body
|
26
|
+
when Hash then body
|
27
|
+
else JSON.parse(body, symbolize_names: true, quirks_mode: true)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
module Kapellmeister; end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kapellmeister
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- DarkWater
|
@@ -31,32 +31,77 @@ dependencies:
|
|
31
31
|
- !ruby/object:Gem::Version
|
32
32
|
version: 2.0.2
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
|
-
name:
|
34
|
+
name: redcarpet
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
36
36
|
requirements:
|
37
37
|
- - "~>"
|
38
38
|
- !ruby/object:Gem::Version
|
39
|
-
version: '
|
39
|
+
version: '1.17'
|
40
40
|
- - ">="
|
41
41
|
- !ruby/object:Gem::Version
|
42
|
-
version:
|
42
|
+
version: 1.17.0
|
43
43
|
type: :development
|
44
44
|
prerelease: false
|
45
45
|
version_requirements: !ruby/object:Gem::Requirement
|
46
46
|
requirements:
|
47
47
|
- - "~>"
|
48
48
|
- !ruby/object:Gem::Version
|
49
|
-
version: '
|
49
|
+
version: '1.17'
|
50
50
|
- - ">="
|
51
51
|
- !ruby/object:Gem::Version
|
52
|
-
version:
|
52
|
+
version: 1.17.0
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: yard
|
55
|
+
requirement: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - "~>"
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0.7'
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 0.7.5
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - "~>"
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0.7'
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: 0.7.5
|
53
73
|
description: Contains third party routes parser
|
54
74
|
email:
|
55
75
|
- denis.arushanov@darkcreative.ru
|
56
76
|
executables: []
|
57
77
|
extensions: []
|
58
78
|
extra_rdoc_files: []
|
59
|
-
files:
|
79
|
+
files:
|
80
|
+
- ".editorconfig"
|
81
|
+
- ".gitignore"
|
82
|
+
- ".rubocop.yml"
|
83
|
+
- ".ruby-gemset"
|
84
|
+
- ".ruby-version"
|
85
|
+
- CODE_OF_CONDUCT.md
|
86
|
+
- Gemfile
|
87
|
+
- README.md
|
88
|
+
- Rakefile
|
89
|
+
- bin/console
|
90
|
+
- bin/setup
|
91
|
+
- kapellmeister.gemspec
|
92
|
+
- lib/generators/kapellmeister/add_service_generator.rb
|
93
|
+
- lib/generators/kapellmeister/templates/initializers/third_party_initializer.rb.tt
|
94
|
+
- lib/generators/kapellmeister/templates/lib/third_party.rb.tt
|
95
|
+
- lib/generators/kapellmeister/templates/lib/third_party/client.rb.tt
|
96
|
+
- lib/generators/kapellmeister/templates/lib/third_party/configuration.rb.tt
|
97
|
+
- lib/generators/kapellmeister/templates/lib/third_party/responder.rb.tt
|
98
|
+
- lib/generators/kapellmeister/templates/lib/third_party/routes.yml.tt
|
99
|
+
- lib/kapellmeister.rb
|
100
|
+
- lib/kapellmeister/base.rb
|
101
|
+
- lib/kapellmeister/dispatcher.rb
|
102
|
+
- lib/kapellmeister/requests_extension.rb
|
103
|
+
- lib/kapellmeister/responder.rb
|
104
|
+
- lib/kapellmeister/version.rb
|
60
105
|
homepage: https://github.com/DarkWater666/kapellmeister
|
61
106
|
licenses:
|
62
107
|
- MIT
|