parallel_batch_api 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a998b5afbad4016b65439206dd37780b15d3f084
4
+ data.tar.gz: 365417b1886e9975572f7e7f3c193e8c032d9ab7
5
+ SHA512:
6
+ metadata.gz: 213bb81438d448e89ae4dd5d59c6ac36d96ed719c12fdec0b03ad9469fc1e1408abfc7e9df0d46ef734b34ed42d7355c39c0c53fb5e4de8bfcc562ef37fa6ff2
7
+ data.tar.gz: aff4cacb3d93eb09ae4ffd57e275bc530831a4bc9f496a5fb992542d3c079cb0eaf33d859af738cbe030af2f4426b157d75174d7474eae04337167896f48e6b2
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in parallel_batch_api.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,22 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ parallel_batch_api (0.1.0)
5
+ parallel
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ parallel (1.12.1)
11
+ rake (10.5.0)
12
+
13
+ PLATFORMS
14
+ ruby
15
+
16
+ DEPENDENCIES
17
+ bundler (~> 1.16)
18
+ parallel_batch_api!
19
+ rake (~> 10.0)
20
+
21
+ BUNDLED WITH
22
+ 1.16.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 山野井侑
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,111 @@
1
+ # ParallelBatchApi
2
+
3
+ This gem is a Rack Middleware to send batch parallel request
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'parallel_batch_api'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install parallel_batch_api
20
+
21
+ ## Usage
22
+
23
+ write this code in your config/application.rb
24
+
25
+ ```ruby
26
+ config.middleware.insert_before ActionDispatch::ParamsParser, ParallelBatchApi::Middleware
27
+ ```
28
+
29
+ Request JSON example
30
+
31
+ ```json
32
+ {
33
+ "requests": [
34
+ {
35
+ "method": "GET",
36
+ "url": "/api/posts/1000"
37
+ },
38
+ {
39
+ "method": "GET",
40
+ "url": "/api/posts/2000"
41
+ },
42
+ {
43
+ "method": "GET",
44
+ "url": "/api/posts/3000",
45
+ "params": {
46
+ "page": "1"
47
+ }
48
+ }
49
+ ]
50
+ }
51
+ ```
52
+
53
+ Response
54
+
55
+ ```json
56
+ {
57
+ "responses": [
58
+ {
59
+ "status": "200",
60
+ "header": {
61
+ },
62
+ "body": {
63
+ },
64
+ "url": "/api/posts/1000",
65
+ "key": "api/posts#show"
66
+ },
67
+ {
68
+ "status": "200",
69
+ "header": {
70
+ },
71
+ "body": {
72
+ },
73
+ "url": "/api/posts/2000",
74
+ "key": "api/posts#show"
75
+ },
76
+ {
77
+ "status": "200",
78
+ "header": {
79
+ },
80
+ "body": {
81
+ },
82
+ "url": "/api/posts/3000",
83
+ "key": "api/posts#show"
84
+ }
85
+ ]
86
+ }
87
+ ```
88
+
89
+ ## Configuration
90
+ config/initializers/parallel_batch_api.rb
91
+
92
+ ```ruby
93
+ ParallelBatchApi.configure do |config|
94
+ config.batch_path = your api path default '/api/batch'
95
+ config.thread = number of thread default 1
96
+ end
97
+ ```
98
+
99
+ ## Development
100
+
101
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
102
+
103
+ 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).
104
+
105
+ ## Contributing
106
+
107
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/parallel_batch_api.
108
+
109
+ ## License
110
+
111
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "parallel_batch_api"
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(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,4 @@
1
+ require 'parallel_batch_api/version'
2
+ require 'parallel_batch_api/middleware'
3
+
4
+ module ParallelBatchApi; end
@@ -0,0 +1,13 @@
1
+ module ParallelBatchApi
2
+ module Builder
3
+ def build_requests(env)
4
+ request = Rack::Request.new(env.deep_dup)
5
+ json_body = JSON.parse(request.body.read)
6
+ json_body['requests']
7
+ end
8
+
9
+ def build_response(responses)
10
+ [200, { 'Content-Type' => 'application/json' }, [{ responses: responses }.to_json]]
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,14 @@
1
+ module ParallelBatchApi
2
+ module Configuration
3
+ OPTIONS = %i(
4
+ batch_path
5
+ thread
6
+ ).freeze
7
+
8
+ attr_accessor *OPTIONS
9
+
10
+ def configure
11
+ yield self
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,22 @@
1
+ module ParallelBatchApi
2
+ class ErrorResponse
3
+ attr_reader :error, :code
4
+
5
+ def initialize(error)
6
+ @error = error
7
+ @code = @error.status_code if @error.respond_to?(:status_code)
8
+ end
9
+
10
+ def execute
11
+ [code, { 'Content-Type' => 'application/json' }, [body.to_json]]
12
+ end
13
+
14
+ def body
15
+ { message: error.message }
16
+ end
17
+
18
+ def code
19
+ @code || 500
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,48 @@
1
+ require 'parallel_batch_api/builder'
2
+ require 'parallel_batch_api/processor'
3
+ require 'parallel_batch_api/configuration'
4
+
5
+ module ParallelBatchApi
6
+ extend ParallelBatchApi::Configuration
7
+
8
+ HTTP_METHOD_GET = 'GET'.freeze
9
+ HTTP_METHOD_POST = 'POST'.freeze
10
+ DEFAULT_BATCH_PATH = '/api/batch'.freeze
11
+
12
+ class Middleware
13
+ include ParallelBatchApi::Builder
14
+ include ParallelBatchApi::Processor
15
+
16
+ def initialize(app)
17
+ @app = app
18
+ end
19
+
20
+ def call(env)
21
+ if batch_request?(env)
22
+ process(env)
23
+ else
24
+ @app.call(env)
25
+ end
26
+ end
27
+
28
+ def batch_request?(env)
29
+ valid_path?(env) && valid_method?(env)
30
+ end
31
+
32
+ def valid_path?(env)
33
+ batch_path.include?(env['PATH_INFO'])
34
+ end
35
+
36
+ def valid_method?(env)
37
+ env['REQUEST_METHOD'] == HTTP_METHOD_POST
38
+ end
39
+
40
+ def batch_path
41
+ [ParallelBatchApi.batch_path]
42
+ end
43
+ end
44
+
45
+ def self.batch_path
46
+ super || DEFAULT_BATCH_PATH
47
+ end
48
+ end
@@ -0,0 +1,26 @@
1
+ require 'parallel_batch_api/rack_processor'
2
+ module ParallelBatchApi
3
+ module Processor
4
+ def process(env)
5
+ build_response(parallel_process(env.deep_dup))
6
+ end
7
+
8
+ def parallel_process(env)
9
+ Parallel.map(requests(env), in_threads: thread_size) do |item|
10
+ ActiveRecord::Base.connection_pool.with_connection do
11
+ e = env.deep_dup
12
+ rack = ParallelBatchApi::RackProcessor.new(e, item, @app)
13
+ rack.process
14
+ end
15
+ end
16
+ end
17
+
18
+ def requests(env)
19
+ build_requests(env)
20
+ end
21
+
22
+ def thread_size
23
+ ParallelBatchApi.thread || 1
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,71 @@
1
+ require 'parallel_batch_api/response'
2
+ require 'parallel_batch_api/error_response'
3
+
4
+ module ParallelBatchApi
5
+ class RackProcessor
6
+ attr_reader :app, :env, :url, :params, :headers, :method
7
+
8
+ def initialize(env, options, app)
9
+ @method = options['method'] || ParallelBatchApi::HTTP_METHOD_GET
10
+ @url = options['url']
11
+ @params = options['params'] || {}
12
+ @env = env
13
+ @headers = options['headers'] || {}
14
+ @app = app
15
+ @params = params_for_rails
16
+ end
17
+
18
+ def process
19
+ build
20
+ res = begin
21
+ app.call(env)
22
+ rescue => er
23
+ ParallelBatchApi::ErrorResponse.new(er).execute
24
+ end
25
+
26
+ ParallelBatchApi::Response.new(res, url, params).execute
27
+ end
28
+
29
+ # set value into @env
30
+ def build
31
+ path, qs = url.split('?')
32
+
33
+ headrs = (headers || {}).inject({}) do |heads, (k, v)|
34
+ heads.tap { |h| h["HTTP_#{k.gsub(/\-/, '_').upcase}"] = v }
35
+ end
36
+
37
+ env.merge!(headrs)
38
+
39
+ env['REQUEST_METHOD'] = method.upcase
40
+
41
+ if env['REQUEST_URI']
42
+ env['REQUEST_URI'] = env['REQUEST_URI'].gsub(/#{ParallelBatchApi.batch_path}.*/, url)
43
+ end
44
+
45
+ env['REQUEST_PATH'] = env['PATH_INFO'] = path
46
+ env['ORIGINAL_FULLPATH'] = url
47
+
48
+ env['rack.request.query_string'] = qs
49
+ env['QUERY_STRING'] = qs
50
+
51
+ env['rack.request.form_hash'] = params
52
+ env['rack.request.query_hash'] = method == ParallelBatchApi::HTTP_METHOD_GET ? params : nil
53
+
54
+ env['action_dispatch.request.parameters'] = params
55
+ env['action_dispatch.request.request_parameters'] = params
56
+
57
+ env['CONTENT_TYPE'] = 'text/plain'
58
+ env['HTTP_CONTENT_TYPE'] = 'text/plain'
59
+ env['ROW_POST_DATA'] = ''
60
+ end
61
+
62
+ private
63
+
64
+ def params_for_rails
65
+ path_params = Rails.application.routes.recognize_path(url)
66
+ params.merge(path_params)
67
+ rescue
68
+ params
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,21 @@
1
+ module ParallelBatchApi
2
+ class Response
3
+ attr_reader :status, :header, :body, :url, :params
4
+
5
+ def initialize(res, url, params)
6
+ @status, @header, @body = res
7
+ @url = url
8
+ @params = params
9
+ end
10
+
11
+ def execute
12
+ base_body = ''
13
+ body.each { |str| base_body << str }
14
+ { status: status, header: header, body: JSON.parse(base_body), url: url, key: key }
15
+ end
16
+
17
+ def key
18
+ "#{params[:controller]}##{params[:action]}"
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module ParallelBatchApi
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,35 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "parallel_batch_api/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "parallel_batch_api"
8
+ spec.version = ParallelBatchApi::VERSION
9
+ spec.authors = ["山野井侑"]
10
+ spec.email = ["yuu.yamanoi1222@gmail.com"]
11
+
12
+ spec.summary = %q{Parallel batch api}
13
+ spec.description = %q{Parallel batch api}
14
+ spec.homepage = "https://github.com/yyamanoi1222/parallel_batch_api"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against " \
22
+ "public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
26
+ f.match(%r{^(test|spec|features)/})
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_development_dependency "bundler", "~> 1.16"
33
+ spec.add_development_dependency "rake", "~> 10.0"
34
+ spec.add_runtime_dependency 'parallel'
35
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: parallel_batch_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - 山野井侑
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-06-28 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.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
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: parallel
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Parallel batch api
56
+ email:
57
+ - yuu.yamanoi1222@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - Gemfile.lock
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - lib/parallel_batch_api.rb
71
+ - lib/parallel_batch_api/builder.rb
72
+ - lib/parallel_batch_api/configuration.rb
73
+ - lib/parallel_batch_api/error_response.rb
74
+ - lib/parallel_batch_api/middleware.rb
75
+ - lib/parallel_batch_api/processor.rb
76
+ - lib/parallel_batch_api/rack_processor.rb
77
+ - lib/parallel_batch_api/response.rb
78
+ - lib/parallel_batch_api/version.rb
79
+ - parallel_batch_api.gemspec
80
+ homepage: https://github.com/yyamanoi1222/parallel_batch_api
81
+ licenses:
82
+ - MIT
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.5.1
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Parallel batch api
104
+ test_files: []