racli 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
+ SHA256:
3
+ metadata.gz: 2264b02359fcb40d6d11a9f2aa42d0d65a6772d9652b95e4ac4dfde9eea2c4ca
4
+ data.tar.gz: 8c99e8815393c5bd160f72805d0d212d7fce8d488894f12fb8f070069c3769a7
5
+ SHA512:
6
+ metadata.gz: 2ae2832d08e38baac324031c3b0d9028523f25ae7263ca5fe581576027c52e09b00a6ec89eba5c412b7f446ebda800a4ba15353c84db0388708a405d72c509fb
7
+ data.tar.gz: f30b52ef7013359404bb6f80e053235f955bd59baa71bf0f51bd8f9fe8d439566665649a1c294abedc9b0fff29d96e4e0f7419093da7d3958a4dadd41cf769b3
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ /vendor/bundle
13
+ /.idea
14
+ /config.ru
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.1
5
+ before_install: gem install bundler -v 1.16.1
data/Gemfile ADDED
@@ -0,0 +1,8 @@
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 racli.gemspec
6
+ gemspec
7
+
8
+ gem 'pry-byebug'
data/Gemfile.lock ADDED
@@ -0,0 +1,47 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ racli (0.1.0)
5
+ rack
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ byebug (10.0.2)
11
+ coderay (1.1.2)
12
+ diff-lcs (1.3)
13
+ method_source (0.9.0)
14
+ pry (0.11.3)
15
+ coderay (~> 1.1.0)
16
+ method_source (~> 0.9.0)
17
+ pry-byebug (3.6.0)
18
+ byebug (~> 10.0)
19
+ pry (~> 0.10)
20
+ rack (2.0.5)
21
+ rake (10.5.0)
22
+ rspec (3.7.0)
23
+ rspec-core (~> 3.7.0)
24
+ rspec-expectations (~> 3.7.0)
25
+ rspec-mocks (~> 3.7.0)
26
+ rspec-core (3.7.1)
27
+ rspec-support (~> 3.7.0)
28
+ rspec-expectations (3.7.0)
29
+ diff-lcs (>= 1.2.0, < 2.0)
30
+ rspec-support (~> 3.7.0)
31
+ rspec-mocks (3.7.0)
32
+ diff-lcs (>= 1.2.0, < 2.0)
33
+ rspec-support (~> 3.7.0)
34
+ rspec-support (3.7.1)
35
+
36
+ PLATFORMS
37
+ ruby
38
+
39
+ DEPENDENCIES
40
+ bundler (~> 1.16)
41
+ pry-byebug
42
+ racli!
43
+ rake (~> 10.0)
44
+ rspec (~> 3.0)
45
+
46
+ BUNDLED WITH
47
+ 1.16.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Makoto Tajitsu
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,45 @@
1
+ # Racli
2
+
3
+ CLI Tool with Rack Application.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'racli'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install racli
20
+
21
+ ## Usage
22
+
23
+ put config.ru
24
+ ```ruby
25
+ run lambda { |env| [200, {}, ['hello']] }
26
+ ```
27
+
28
+ Run racl on directory where you put config.ru on
29
+ ```bash
30
+ racl
31
+ ```
32
+
33
+ ## Development
34
+
35
+ 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.
36
+
37
+ 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).
38
+
39
+ ## Contributing
40
+
41
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/racli.
42
+
43
+ ## License
44
+
45
+ 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,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "racli"
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
data/exe/racli ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'racli'
4
+ Bundler.require
5
+
6
+ config_params, request_params = Racli::OptionParser.new.call(ARGV)
7
+
8
+ cli = Racli::CLI.new(**config_params)
9
+ cli.call(**request_params)
data/lib/racli.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'racli/version'
2
+ require 'racli/cli'
3
+ require 'racli/option_parser'
4
+
5
+ module Racli
6
+ end
data/lib/racli/cli.rb ADDED
@@ -0,0 +1,63 @@
1
+ require 'racli/rack'
2
+ require 'cgi'
3
+ require 'json'
4
+
5
+ Dir['./lib/racli/handlers/**/*.rb'].each { |f| require f }
6
+
7
+ module Racli
8
+ class CLI
9
+ attr_accessor :method, :path, :params, :config
10
+
11
+ def initialize(config:, rcfile:)
12
+ config_ruby_string = File.read(config)
13
+ @rackapp = eval "::Racli::Rack.new { #{config_ruby_string} }.to_app", TOPLEVEL_BINDING, '(racli)', 0
14
+ @default_handler = Racli::Handlers::DefaultHandler.new(self)
15
+
16
+ eval(File.read(rcfile)) if File.exists?(rcfile)
17
+ end
18
+
19
+ def call(method:, path:, params:)
20
+ request_params = request_params(method: method, path: path, params: params)
21
+ status, headers, body = @rackapp.call(request_params)
22
+
23
+ original_args = { method: method, path: path, params: params }
24
+ catch(:abort) do
25
+ response_handlers = handlers + [@default_handler]
26
+ response_handlers.reduce([status, headers, body]) do |(status, headers, body), handler|
27
+ handler.call(status, headers, body, original_args)
28
+ end
29
+ end
30
+ end
31
+
32
+ def add_handler(handler_klass)
33
+ handler = handler_klass.new(self)
34
+ handlers.push handler
35
+ end
36
+
37
+ private
38
+
39
+ def request_params(method:, path:, params:)
40
+ query_string = to_query_string(params)
41
+ request_params = {
42
+ 'PATH_INFO' => path || '/',
43
+ 'REQUEST_METHOD' => method || 'GET',
44
+ }
45
+ if method == 'GET'
46
+ request_params['QUERY_STRING'] = query_string
47
+ request_params['rack.input'] = StringIO.new('')
48
+ else
49
+ request_params['QUERY_STRING'] = ''
50
+ request_params['rack.input'] = StringIO.new(query_string)
51
+ end
52
+ request_params
53
+ end
54
+
55
+ def handlers
56
+ @handlers ||= []
57
+ end
58
+
59
+ def to_query_string(params)
60
+ params.map { |k, v| "#{k}=#{CGI.escape(v)}" }.join('&')
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,11 @@
1
+ module Racli
2
+ module Handlers
3
+ class Base
4
+ attr_accessor :cli
5
+
6
+ def initialize(cli)
7
+ @cli = cli
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,16 @@
1
+ require 'racli/handlers/base'
2
+
3
+ module Racli
4
+ module Handlers
5
+ class DebugHandler < Base
6
+ def call(status, headers, body, original_args)
7
+ puts '*** debug ***'
8
+ puts status
9
+ puts headers
10
+ puts body
11
+ puts '******'
12
+ [status, headers, body]
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,19 @@
1
+ require 'racli/handlers/base'
2
+
3
+ module Racli
4
+ module Handlers
5
+ class DefaultHandler < Base
6
+ def call(status, headers, body, original_args)
7
+ case status.to_i
8
+ when 200...300
9
+ puts body[0]
10
+ when 300...400
11
+
12
+ else
13
+ STDERR.puts body[0]
14
+ end
15
+ [status, headers, body]
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,36 @@
1
+ require 'racli/handlers/base'
2
+
3
+ module Racli
4
+ module Handlers
5
+ class RetryHandler < Base
6
+ DEFAULT_MAX_RETRY_COUNTER = 3
7
+
8
+ attr_accessor :retry_counter, :default_max_retry_counter
9
+
10
+ def initialize(cli)
11
+ @retry_counter = 0
12
+ @retry_max_counter = DEFAULT_MAX_RETRY_COUNTER
13
+ super
14
+ end
15
+
16
+ def call(status, headers, body, original_args)
17
+ if ((300...400) === status.to_i) && headers['Location']
18
+ unless within_max_retry_count?
19
+ STDERR.puts 'Too many redirection!'
20
+ throw :abort
21
+ end
22
+ @retry_counter += 1
23
+ original_args[:method] = 'GET'
24
+ original_args[:path] = headers['Location']
25
+ cli.call(**original_args)
26
+ throw :abort
27
+ end
28
+ [status, headers, body]
29
+ end
30
+
31
+ def within_max_retry_count?
32
+ @retry_counter < DEFAULT_MAX_RETRY_COUNTER
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,42 @@
1
+ require 'optparse'
2
+
3
+ module Racli
4
+ class OptionParser
5
+ def call(args)
6
+ options, args = parse(args)
7
+ config_params = { config: options[:config], rcfile: options[:rcfile] }
8
+
9
+ params = { method: 'GET', path: '/' }
10
+ params[:method] = args[0] if args.length > 0
11
+ params[:path] = args[1] if args.length > 1
12
+
13
+ request_params = options.dup
14
+ request_params.delete(:config)
15
+ request_params.delete(:rcfile)
16
+ params[:params] = request_params
17
+
18
+ [config_params, params]
19
+ end
20
+
21
+ private
22
+
23
+ def parse(args)
24
+ keys = args.select { |key| key.include?('--') }
25
+ .map { |key| key.gsub(/^\-\-/, '') }
26
+ .reject { |key| ['config', 'rcfile'].include?(key) }
27
+ options = {}
28
+ options[:config] = File.expand_path('./config.ru', Dir.pwd)
29
+ options[:rcfile] = File.expand_path('./.raclirc', Dir.pwd)
30
+
31
+ parser = ::OptionParser.new
32
+ parser.on('-c', '--config VALUE') { |v| options[:config] = v }
33
+ parser.on('--rcfile VALUE') { |v| options[:rcfile] = v }
34
+ keys.each do |key|
35
+ parser.on("--#{key} VALUE") { |v| options[key.to_s] = v }
36
+ end
37
+
38
+ args = parser.parse(ARGV)
39
+ [options, args]
40
+ end
41
+ end
42
+ end
data/lib/racli/rack.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'rack/builder'
2
+
3
+ module Racli
4
+ class Rack < Rack::Builder
5
+ end
6
+ end
@@ -0,0 +1,3 @@
1
+ module Racli
2
+ VERSION = "0.1.0"
3
+ end
data/racli.gemspec ADDED
@@ -0,0 +1,28 @@
1
+
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'racli/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'racli'
8
+ spec.version = Racli::VERSION
9
+ spec.authors = ['tzmfreedom']
10
+ spec.email = ['makoto_tajitsu@hotmail.co.jp']
11
+
12
+ spec.summary = %q{CLI Backend with Rack}
13
+ spec.description = %q{CLI Backend with Rack}
14
+ spec.homepage = 'https://github.com/tzmfreedom/racli'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_runtime_dependency 'rack'
25
+ spec.add_development_dependency 'bundler', '~> 1.16'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ spec.add_development_dependency 'rspec', '~> 3.0'
28
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: racli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - tzmfreedom
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-06-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: CLI Backend with Rack
70
+ email:
71
+ - makoto_tajitsu@hotmail.co.jp
72
+ executables:
73
+ - racli
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - Gemfile.lock
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - bin/setup
87
+ - exe/racli
88
+ - lib/racli.rb
89
+ - lib/racli/cli.rb
90
+ - lib/racli/handlers/base.rb
91
+ - lib/racli/handlers/debug_handler.rb
92
+ - lib/racli/handlers/default_handler.rb
93
+ - lib/racli/handlers/retry_handler.rb
94
+ - lib/racli/option_parser.rb
95
+ - lib/racli/rack.rb
96
+ - lib/racli/version.rb
97
+ - racli.gemspec
98
+ homepage: https://github.com/tzmfreedom/racli
99
+ licenses:
100
+ - MIT
101
+ metadata: {}
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 2.7.6
119
+ signing_key:
120
+ specification_version: 4
121
+ summary: CLI Backend with Rack
122
+ test_files: []