racli 0.1.1 → 0.1.2

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
  SHA256:
3
- metadata.gz: 5cfe3fca6a17318872134daba03be398d9763a88dd7083d6c3bb936d900632eb
4
- data.tar.gz: 7e27ee5bfddcc13467af8d0a498957c719379d3fc56418d7822f2a5e7f14ed6c
3
+ metadata.gz: 692db1ac3147aa4605a0ea9d6ac4febb5d4f181e2c830bd76ffb4917efb4fd1f
4
+ data.tar.gz: 709d75e96471bff4864ed669c865d5b0e7e3a356292e5d9c379ca83e4059ecd8
5
5
  SHA512:
6
- metadata.gz: 52611f47390d8de801027888e4cb39736278094e19b622ccb7d0f0a879525e081a740c22b5839f6e2aef5041a6dd1201bd770a02c42974e81e52c3166baa1198
7
- data.tar.gz: 9b420c0c471266d21117e01848b1ad1fd2c9c0813448e1d02aa8933438c56734157b729e31f63e0ba1ac335af23987a3c4367a4773a26fd638c9abed87812ec5
6
+ metadata.gz: 952da0b5d635090c3e3dc2da434b84fa0129139b1965e78130db49c770ab2cbc29282339cc5b4c8a0692f8f40bcc2287110019baca452eb543a2257feeb3b4d6
7
+ data.tar.gz: 67c415aa050b8b2676a0e143a74c2ad2a7b13818b18e51812cb4647ee1fe230726397f4f510d405b3fc42196d6c41e1da2402c5353f58afdbfb17f29be3f2a99
data/.gitignore CHANGED
@@ -12,3 +12,4 @@
12
12
  /vendor/bundle
13
13
  /.idea
14
14
  /config.ru
15
+ Gemfile.lock
@@ -0,0 +1,26 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5
3
+
4
+ Lint/ShadowingOuterLocalVariable:
5
+ Exclude:
6
+ - 'lib/racli/cli.rb'
7
+
8
+ Metrics/AbcSize:
9
+ Max: 20
10
+
11
+ Metrics/MethodLength:
12
+ Max: 30
13
+
14
+ Security/Eval:
15
+ Exclude:
16
+ - 'lib/racli/cli.rb'
17
+
18
+ Style/Documentation:
19
+ Enabled: false
20
+
21
+ Style/TrivialAccessors:
22
+ Exclude:
23
+ - 'lib/racli/cli.rb'
24
+
25
+ Metrics/LineLength:
26
+ Max: 100
data/Gemfile CHANGED
@@ -1,6 +1,8 @@
1
- source "https://rubygems.org"
1
+ # frozen_string_literal: true
2
2
 
3
- git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
6
 
5
7
  # Specify your gem's dependencies in racli.gemspec
6
8
  gemspec
data/README.md CHANGED
@@ -25,9 +25,28 @@ put config.ru
25
25
  run lambda { |env| [200, {}, ['hello']] }
26
26
  ```
27
27
 
28
- Run racl on directory where you put config.ru on
28
+ Run racli on directory where you put config.ru.
29
29
  ```bash
30
- racl
30
+ racli
31
+ ```
32
+
33
+ By default, racli requests root path, that is '/', with GET method.
34
+ You can specify HTTP method and path.
35
+ ```bash
36
+ racli {METHOD} {PATH} # ex) racli POST /users
37
+ ```
38
+
39
+ The rack configuration file is `config.ru` on current directory by default.
40
+ If you want to use other file as rack configuration file, you should specify `--config` option
41
+ ```bash
42
+ racli --config /path/to/config_file
43
+ ```
44
+
45
+ If you want to add rack response handler, you should put .raclirc on the same directory as config.ru is put.
46
+ The following is sample .raclirc
47
+ ```ruby
48
+ add_handler Racli::Handlers::RetryHandler
49
+ # add_handler Racli::Handlers::DebugHandler
31
50
  ```
32
51
 
33
52
  ## Development
data/Rakefile CHANGED
@@ -1,6 +1,8 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
3
5
 
4
6
  RSpec::Core::RakeTask.new(:spec)
5
7
 
6
- task :default => :spec
8
+ task default: :spec
@@ -1,7 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
- require "bundler/setup"
4
- require "racli"
4
+ require 'bundler/setup'
5
+ require 'racli'
5
6
 
6
7
  # You can add fixtures and/or initialization code here to make experimenting
7
8
  # with your gem easier. You can also use a different console, if you like.
@@ -10,5 +11,5 @@ require "racli"
10
11
  # require "pry"
11
12
  # Pry.start
12
13
 
13
- require "irb"
14
+ require 'irb'
14
15
  IRB.start(__FILE__)
data/exe/racli CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
4
  require 'racli'
4
5
  Bundler.require
@@ -6,4 +7,4 @@ Bundler.require
6
7
  config_params, request_params = Racli::OptionParser.new.call(ARGV)
7
8
 
8
9
  cli = Racli::CLI.new(**config_params)
9
- cli.call(**request_params)
10
+ cli.call(**request_params)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'racli/version'
2
4
  require 'racli/cli'
3
5
  require 'racli/option_parser'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'racli/rack'
2
4
  require 'cgi'
3
5
  require 'json'
@@ -6,42 +8,72 @@ Dir[File.expand_path('./handlers/**/*.rb', __dir__)].each { |f| require f }
6
8
 
7
9
  module Racli
8
10
  class CLI
9
- attr_accessor :method, :path, :params, :config
10
-
11
11
  def initialize(config:, rcfile:)
12
12
  config_ruby_string = File.read(config)
13
- @rackapp = eval "::Racli::Rack.new { #{config_ruby_string} }.to_app", TOPLEVEL_BINDING, '(racli)', 0
13
+ eval_string = "::Racli::Rack.new { #{config_ruby_string} }.to_app"
14
+ @rackapp = TOPLEVEL_BINDING.eval eval_string, '(racli)', 0
14
15
  @default_handler = Racli::Handlers::DefaultHandler.new(self)
16
+ @handlers = []
17
+ @aliases = {}
18
+ @default_headers = {}
15
19
 
16
- eval(File.read(rcfile)) if File.exists?(rcfile)
20
+ eval(File.read(rcfile)) if File.exist?(rcfile)
17
21
  end
18
22
 
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
23
+ def call(method:, path:, params:, headers: @default_headers)
24
+ if @aliases.include?(method.to_sym)
25
+ alias_setting = @aliases[method.to_sym]
26
+ method = alias_setting[:method]
27
+ path = alias_setting[:path]
28
+ headers.merge!(alias_setting[:headers])
29
29
  end
30
+
31
+ request_params = request_params(
32
+ method: method,
33
+ path: path,
34
+ params: params,
35
+ headers: headers
36
+ )
37
+ status, headers, body = @rackapp.call(request_params)
38
+ handle_response(
39
+ status: status,
40
+ headers: headers,
41
+ body: body,
42
+ original_args: { method: method, path: path, params: params }
43
+ )
30
44
  end
31
45
 
32
46
  def add_handler(handler_klass)
33
47
  handler = handler_klass.new(self)
34
- handlers.push handler
48
+ @handlers.push handler
49
+ end
50
+
51
+ def add_alias(alias_name, method = 'GET', path = '/', headers = {})
52
+ @aliases[alias_name] = { method: method, path: path, headers: headers }
53
+ end
54
+
55
+ def default_headers(headers)
56
+ @default_headers = headers
35
57
  end
36
58
 
37
59
  private
38
60
 
39
- def request_params(method:, path:, params:)
61
+ def handle_response(status:, headers:, body:, original_args:)
62
+ catch(:abort) do
63
+ response_handlers = @handlers + [@default_handler]
64
+ response_handlers.each do |handler|
65
+ status, headers, body = handler.call(status, headers, body, original_args)
66
+ end
67
+ end
68
+ end
69
+
70
+ def request_params(method:, path:, params:, headers:)
40
71
  query_string = to_query_string(params)
41
72
  request_params = {
42
73
  'PATH_INFO' => path || '/',
43
- 'REQUEST_METHOD' => method || 'GET',
44
- }
74
+ 'REQUEST_METHOD' => method || 'GET'
75
+ }.merge(headers)
76
+
45
77
  if method == 'GET'
46
78
  request_params['QUERY_STRING'] = query_string
47
79
  request_params['rack.input'] = StringIO.new('')
@@ -52,10 +84,6 @@ module Racli
52
84
  request_params
53
85
  end
54
86
 
55
- def handlers
56
- @handlers ||= []
57
- end
58
-
59
87
  def to_query_string(params)
60
88
  params.map { |k, v| "#{k}=#{CGI.escape(v)}" }.join('&')
61
89
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Racli
2
4
  module Handlers
3
5
  class Base
@@ -1,9 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'racli/handlers/base'
2
4
 
3
5
  module Racli
4
6
  module Handlers
5
7
  class DebugHandler < Base
6
- def call(status, headers, body, original_args)
8
+ def call(status, headers, body, _original_args)
7
9
  puts '*** debug ***'
8
10
  puts status
9
11
  puts headers
@@ -1,16 +1,16 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'racli/handlers/base'
2
4
 
3
5
  module Racli
4
6
  module Handlers
5
7
  class DefaultHandler < Base
6
- def call(status, headers, body, original_args)
8
+ def call(status, headers, body, _original_args)
7
9
  case status.to_i
8
- when 200...300
9
- puts body[0]
10
- when 300...400
11
-
12
- else
13
- STDERR.puts body[0]
10
+ when 200...300
11
+ puts body[0]
12
+ when 400...600
13
+ STDERR.puts body[0]
14
14
  end
15
15
  [status, headers, body]
16
16
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'racli/handlers/base'
2
4
 
3
5
  module Racli
@@ -14,7 +16,7 @@ module Racli
14
16
  end
15
17
 
16
18
  def call(status, headers, body, original_args)
17
- if ((300...400) === status.to_i) && headers['Location']
19
+ if (300...400).cover?(status.to_i) && headers['Location']
18
20
  unless within_max_retry_count?
19
21
  STDERR.puts 'Too many redirection!'
20
22
  throw :abort
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'optparse'
2
4
 
3
5
  module Racli
@@ -7,7 +9,7 @@ module Racli
7
9
  config_params = { config: options[:config], rcfile: options[:rcfile] }
8
10
 
9
11
  params = { method: 'GET', path: '/' }
10
- params[:method] = args[0] if args.length > 0
12
+ params[:method] = args[0] unless args.empty?
11
13
  params[:path] = args[1] if args.length > 1
12
14
 
13
15
  request_params = options.dup
@@ -22,8 +24,8 @@ module Racli
22
24
 
23
25
  def parse(args)
24
26
  keys = args.select { |key| key.include?('--') }
25
- .map { |key| key.gsub(/^\-\-/, '') }
26
- .reject { |key| ['config', 'rcfile'].include?(key) }
27
+ .map { |key| key.gsub(/^\-\-/, '') }
28
+ .reject { |key| %w[config rcfile].include?(key) }
27
29
  options = {}
28
30
  options[:config] = File.expand_path('./config.ru', Dir.pwd)
29
31
  options[:rcfile] = File.expand_path('./.raclirc', Dir.pwd)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rack/builder'
2
4
 
3
5
  module Racli
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Racli
2
- VERSION = "0.1.1"
4
+ VERSION = '0.1.2'
3
5
  end
@@ -1,5 +1,6 @@
1
+ # frozen_string_literal: true
1
2
 
2
- lib = File.expand_path('../lib', __FILE__)
3
+ lib = File.expand_path('lib', __dir__)
3
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
5
  require 'racli/version'
5
6
 
@@ -9,8 +10,8 @@ Gem::Specification.new do |spec|
9
10
  spec.authors = ['tzmfreedom']
10
11
  spec.email = ['makoto_tajitsu@hotmail.co.jp']
11
12
 
12
- spec.summary = %q{CLI Backend with Rack}
13
- spec.description = %q{CLI Backend with Rack}
13
+ spec.summary = 'CLI Backend with Rack'
14
+ spec.description = 'CLI Backend with Rack'
14
15
  spec.homepage = 'https://github.com/tzmfreedom/racli'
15
16
  spec.license = 'MIT'
16
17
 
@@ -25,4 +26,5 @@ Gem::Specification.new do |spec|
25
26
  spec.add_development_dependency 'bundler', '~> 1.16'
26
27
  spec.add_development_dependency 'rake', '~> 10.0'
27
28
  spec.add_development_dependency 'rspec', '~> 3.0'
29
+ spec.add_development_dependency 'rubocop'
28
30
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: racli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - tzmfreedom
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-06-10 00:00:00.000000000 Z
11
+ date: 2018-06-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
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'
69
83
  description: CLI Backend with Rack
70
84
  email:
71
85
  - makoto_tajitsu@hotmail.co.jp
@@ -76,6 +90,7 @@ extra_rdoc_files: []
76
90
  files:
77
91
  - ".gitignore"
78
92
  - ".rspec"
93
+ - ".rubocop.yml"
79
94
  - ".travis.yml"
80
95
  - Gemfile
81
96
  - LICENSE.txt