plz 0.0.6 → 0.0.8

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
  SHA1:
3
- metadata.gz: 6fed69be3c08f69644d4a0324f6cb595922227b5
4
- data.tar.gz: 5b55fa2b2532c5179af56a384188aabfe995ee84
3
+ metadata.gz: 74ec91c41138b2f96d02372d1166737067d0bb1a
4
+ data.tar.gz: 3f8766d51ffb4bc9dd2e72bc318de6d0fd3c02b9
5
5
  SHA512:
6
- metadata.gz: f1f9b49e49ed042fffc77b83416ffb977c12c4977cb8048475bad1184366a8d8d94fe8627dc84569457e3c2ee7ad1ea58107498e302940d6185114e29ef3f80b
7
- data.tar.gz: 616d398921414557c4da80cc82163ff2c3cbaac42f814185ed3abe86bbbf5fd48edbafdc9af5b058a54e7d3d08be421dcc29900e93952b7a71afc5bcd6d54ccb
6
+ metadata.gz: d87b4bd04dfa82276006b3e1340b6f2b34250fc4403d6b74895de0d967ea5fe9eb401422afa63b6ac1947bad3770ad1df653fd9cdb78b065828e65d7b4c81c4c
7
+ data.tar.gz: 442b673abd50fafd745cd3996e4c436ebb8de0f7cb3b28711ece3d7a3f0adc97284332a7efc5c3cbf6c3fb8aa696cf3b2ac6fb46c9b68b0fba5761f23da4d477
@@ -1,3 +1,9 @@
1
+ ## 0.0.8
2
+ * Fix bug :bug:
3
+
4
+ ## 0.0.7
5
+ * Add command examples in help message
6
+
1
7
  ## 0.0.6
2
8
  * Better error handling
3
9
  * Add some command line options
data/README.md CHANGED
@@ -10,19 +10,20 @@ $ gem install plz
10
10
 
11
11
  ### Synopsis
12
12
  ```sh
13
- $ plz <action> <target> [headers|params]
13
+ $ plz <action> <target> [headers|params] [options]
14
+ | | | | |
15
+ | | | | `-- --no-response-header
16
+ | | | | --no-response-body
17
+ | | | | --no-color
18
+ | | | | --help, -h
14
19
  | | | |
15
- | | | `---- key=value ({"key":"value"}) or key:=value ({"key":value})
16
- | | | params can be:
17
- | | | * URI Template variable
18
- | | | * Query string in GET method
19
- | | | * Request body in other methods
20
+ | | | `---------- key=value or key:=value
20
21
  | | |
21
- | | `----------- Key:value
22
+ | | `----------------- Key:value
22
23
  | |
23
- | `--------------------- target resource name (e.g. user, recipe, etc.)
24
+ | `--------------------------- target name
24
25
  |
25
- `------------------------------ action name (e.g. show, list, create, delete, etc.)
26
+ `------------------------------------ action name
26
27
  ```
27
28
 
28
29
  ### Schema
@@ -31,6 +32,28 @@ that describes about the API where you want to send HTTP request.
31
32
  Plz interprets command-line arguments based on that JSON Schema, then sends HTTP request.
32
33
  See [schema.yml](schema.yml) as an example.
33
34
 
35
+ ### Headers
36
+ To set custom request headers you can use `Key:value` syntax in command line argument.
37
+
38
+ ```sh
39
+ $ plz list user Api-Access-Token:123
40
+ ```
41
+
42
+ ### Params
43
+ Params are used for the following purpose:
44
+
45
+ * URI Template variables
46
+ * Query string in GET method
47
+ * Request body in other methods
48
+
49
+ You can set params by `key=value` or `key:=value` syntax in command line argument.
50
+ `key=value` is parsed into String value,
51
+ while `key:=value` is parsed into JSON value (e.g. key:=17 will be `{"key":17}`).
52
+
53
+ ```sh
54
+ $ plz create user name=alice age:=17
55
+ ```
56
+
34
57
  ### Example
35
58
  ```sh
36
59
  # GET /users
data/lib/plz.rb CHANGED
@@ -16,8 +16,17 @@ require "yaml"
16
16
 
17
17
  require "plz/error"
18
18
  require "plz/arguments"
19
- require "plz/command"
20
19
  require "plz/command_builder"
20
+ require "plz/commands/base_url_not_found"
21
+ require "plz/commands/help"
22
+ require "plz/commands/invalid_schema"
23
+ require "plz/commands/link_not_found"
24
+ require "plz/commands/no_action_name"
25
+ require "plz/commands/no_target_name"
26
+ require "plz/commands/request"
27
+ require "plz/commands/schema_file_not_found"
28
+ require "plz/commands/undecodable_schema_file"
29
+ require "plz/commands/unparsable_json_param"
21
30
  require "plz/error_command"
22
31
  require "plz/response_renderer"
23
32
  require "plz/version"
@@ -18,7 +18,7 @@ module Plz
18
18
  # @return [Hash] Params parsed from given arguments
19
19
  # @raise [Plz::UnparsableJsonParam]
20
20
  def params
21
- ARGV[2..-1].inject({}) do |result, section|
21
+ @params ||= ARGV[2..-1].inject({}) do |result, section|
22
22
  case
23
23
  when /(?<key>.+):=(?<value>.+)/ =~ section
24
24
  begin
@@ -26,42 +26,56 @@ module Plz
26
26
 
27
27
  # @return [Plz::Command] Callable command object
28
28
  def call
29
- exit_if_help
30
- validate!
31
- Command.new(
32
- method: method,
33
- base_url: base_url,
34
- path: path,
35
- headers: headers,
36
- params: request_params,
37
- options: options,
38
- )
39
- rescue Error => error
40
- ErrorCommand.new(error)
41
- end
42
-
43
- private
44
-
45
- # @raise [Plz::Error] Raises if invalid arguments given
46
- def validate!
47
29
  case
48
- when !has_action_name?
49
- raise NoActionName
50
- when !has_target_name?
51
- raise NoTargetName
52
30
  when !has_schema_file?
53
- raise SchemaFileNotFound
31
+ Commands::SchemaFileNotFound.new
54
32
  when !has_decodable_schema_file?
55
- raise UndecodableSchemaFile, pathname: schema_file_pathname
33
+ Commands::UndecodableSchemaFile.new(pathname: schema_file_pathname)
56
34
  when !has_valid_schema_file?
57
- raise InvalidSchema, pathname: schema_file_pathname
35
+ Commands::InvalidSchema.new(pathname: schema_file_pathname, error: @json_schema_error)
58
36
  when !has_base_url?
59
- raise BaseUrlNotFound, pathname: schema_file_pathname
37
+ Commands::BaseUrlNotFound.new(pathname: schema_file_pathname)
38
+ when has_help?
39
+ Commands::Help.new(options: options, schema: json_schema)
40
+ when !has_action_name?
41
+ Commands::NoActionName.new
42
+ when !has_target_name?
43
+ Commands::NoTargetName.new
60
44
  when !has_link?
61
- raise LinkNotFound, pathname: schema_file_pathname, action_name: action_name, target_name: target_name
45
+ Commands::LinkNotFound.new(
46
+ pathname: schema_file_pathname,
47
+ action_name: action_name,
48
+ target_name: target_name
49
+ )
50
+ when has_unparsable_json_param?
51
+ Commands::UnparsableJsonParam.new(error: @json_parse_error)
52
+ else
53
+ Commands::Request.new(
54
+ method: method,
55
+ base_url: base_url,
56
+ path: path,
57
+ headers: headers,
58
+ params: request_params,
59
+ options: options,
60
+ )
62
61
  end
63
62
  end
64
63
 
64
+ private
65
+
66
+ def has_unparsable_json_param?
67
+ params
68
+ false
69
+ rescue UnparsableJsonParam => exception
70
+ @json_parse_error = exception
71
+ true
72
+ end
73
+
74
+ # @return [true, false] True if --help or -h given
75
+ def has_help?
76
+ options[:help]
77
+ end
78
+
65
79
  # @return [true, false] True if given arguments include action name
66
80
  def has_action_name?
67
81
  !!action_name
@@ -79,7 +93,7 @@ module Plz
79
93
 
80
94
  # @return [true, false] True if no error occured in parsing schema file as JSON Schema
81
95
  def has_valid_schema_file?
82
- !!json_schema
96
+ !json_schema.nil?
83
97
  end
84
98
 
85
99
  # @return [true, false] True if no error occured in decoding schema file
@@ -195,7 +209,9 @@ module Plz
195
209
  # @return [JsonSchema::Schema, nil]
196
210
  def json_schema
197
211
  @json_schema ||= JsonSchema.parse!(@schema).tap(&:expand_references!)
198
- rescue JsonSchema::SchemaError
212
+ rescue => exception
213
+ @json_schema_error = exception
214
+ nil
199
215
  end
200
216
 
201
217
  # @return [Plz::Arguments] Wrapper of Raw ARGV
@@ -205,17 +221,13 @@ module Plz
205
221
 
206
222
  # @return [Hash] Command line options
207
223
  def options
208
- @options ||= Slop.parse!(@argv, help: true) do
224
+ @options ||= Slop.parse!(@argv) do
209
225
  banner Error::USAGE
210
- on "no-response-header", "Hide response header"
211
- on "no-response-body", "Hide response body"
226
+ on "h", "help", "Display help message"
212
227
  on "no-color", "Disable coloring output"
213
- end.to_hash
214
- end
215
-
216
- # Display the help message and exits the program if --help or -h given
217
- def exit_if_help
218
- options
228
+ on "no-response-body", "Hide response body"
229
+ on "no-response-header", "Hide response header"
230
+ end
219
231
  end
220
232
  end
221
233
  end
@@ -0,0 +1,13 @@
1
+ module Plz
2
+ module Commands
3
+ class BaseUrlNotFound
4
+ def initialize(pathname: nil)
5
+ @pathname = pathname
6
+ end
7
+
8
+ def call
9
+ puts "#{@pathname} has no base URL at top-level links property"
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,30 @@
1
+ module Plz
2
+ module Commands
3
+ class Help
4
+ # @param options [Slop]
5
+ # @param schema [JsonSchema::Schema]
6
+ def initialize(options: nil, schema: nil)
7
+ @options = options
8
+ @schema = schema
9
+ end
10
+
11
+ # Logs out help message
12
+ def call
13
+ puts %<#{@options}\nExamples:\n#{links.join("\n")}>
14
+ end
15
+
16
+ private
17
+
18
+ # @return [Array<String>]
19
+ def links
20
+ @schema.properties.map do |target_name, schema|
21
+ schema.links.map do |link|
22
+ if link.href && link.method && link.title
23
+ " plz #{link.title.underscore} #{target_name.underscore}"
24
+ end
25
+ end.compact
26
+ end.flatten
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,20 @@
1
+ module Plz
2
+ module Commands
3
+ class InvalidSchema
4
+ def initialize(pathname: nil, error: error)
5
+ @pathname = pathname
6
+ @error = error
7
+ end
8
+
9
+ def call
10
+ puts "#{@pathname} was invalid:#{error_message}"
11
+ end
12
+
13
+ private
14
+
15
+ def error_message
16
+ @error.to_s.gsub("#: ", "\n")
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,15 @@
1
+ module Plz
2
+ module Commands
3
+ class LinkNotFound
4
+ def initialize(pathname: nil, action_name: action_name, target_name: target_name)
5
+ @pathname = pathname
6
+ @action_name = action_name
7
+ @target_name = target_name
8
+ end
9
+
10
+ def call
11
+ puts "#{@pathname} has no definition for `#{@action_name} #{@target_name}`"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,9 @@
1
+ module Plz
2
+ module Commands
3
+ class NoActionName
4
+ def call
5
+ puts "You didn't pass action name\n#{Error::USAGE}"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Plz
2
+ module Commands
3
+ class NoTargetName
4
+ def call
5
+ puts "You didn't pass target name\n#{Error::USAGE}"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,61 @@
1
+ module Plz
2
+ module Commands
3
+ class Request
4
+ # @param headers [Hash]
5
+ # @param params [Hash]
6
+ # @param method [String]
7
+ # @param base_url [String]
8
+ # @param path [String]
9
+ # @param options [Hash]
10
+ def initialize(headers: nil, params: nil, method: nil, base_url: nil, path: nil, options: nil)
11
+ @headers = headers
12
+ @params = params
13
+ @method = method
14
+ @base_url = base_url
15
+ @path = path
16
+ @options = options
17
+ end
18
+
19
+ # Sends an HTTP request and logs out the response
20
+ def call
21
+ response = client.send(@method.downcase, @path, @params, @headers)
22
+ print ResponseRenderer.call(
23
+ status: response.status,
24
+ headers: response.headers,
25
+ body: response.body,
26
+ response_header: flag_to_show_response_header,
27
+ response_body: flag_to_show_response_body,
28
+ color: flag_to_color_response,
29
+ )
30
+ rescue Faraday::ConnectionFailed => exception
31
+ puts exception
32
+ end
33
+
34
+ private
35
+
36
+ # @return [Faraday::Connection]
37
+ def client
38
+ Faraday.new(url: @base_url) do |connection|
39
+ connection.request :json
40
+ connection.response :json
41
+ connection.adapter :net_http
42
+ end
43
+ end
44
+
45
+ # @return [true, false]
46
+ def flag_to_show_response_header
47
+ !@options[:"no-response-header"]
48
+ end
49
+
50
+ # @return [true, false]
51
+ def flag_to_show_response_body
52
+ !@options[:"no-response-body"]
53
+ end
54
+
55
+ # @return [true, false]
56
+ def flag_to_color_response
57
+ !@options[:"no-color"]
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,9 @@
1
+ module Plz
2
+ module Commands
3
+ class SchemaFileNotFound
4
+ def call
5
+ puts "Schema file was not found in #{CommandBuilder::SCHEMA_FILE_PATH_PATTERN}"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,13 @@
1
+ module Plz
2
+ module Commands
3
+ class UndecodableSchemaFile
4
+ def initialize(pathname: nil)
5
+ @pathname = pathname
6
+ end
7
+
8
+ def call
9
+ puts "Failed to decode #{@pathname}"
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module Plz
2
+ module Commands
3
+ class UnparsableJsonParam
4
+ def initialize(error: error)
5
+ @error = error
6
+ end
7
+
8
+ def call
9
+ puts "Given `#{@error.value}` was not valid as JSON"
10
+ end
11
+ end
12
+ end
13
+ end
@@ -3,67 +3,11 @@ module Plz
3
3
  USAGE = "Usage: plz <action> <target> [headers|params] [options]"
4
4
  end
5
5
 
6
- class NoActionName < Error
7
- def to_s
8
- "You didn't pass action name\n#{USAGE}"
9
- end
10
- end
11
-
12
- class NoTargetName < Error
13
- def to_s
14
- "You didn't pass target name\n#{USAGE}"
15
- end
16
- end
17
-
18
- class SchemaFileNotFound < Error
19
- def to_s
20
- "Schema file was not found in #{SCHEMA_FILE_PATH_PATTERN}"
21
- end
22
- end
23
-
24
6
  class UnparsableJsonParam < Error
7
+ attr_reader :value
8
+
25
9
  def initialize(value: nil)
26
10
  @value = value
27
11
  end
28
-
29
- def to_s
30
- "Given `#{@value}` was not valid as JSON"
31
- end
32
- end
33
-
34
- class InvalidSchema < Error
35
- def initialize(pathname: nil)
36
- @pathname = pathname
37
- end
38
- end
39
-
40
- class UndecodableSchemaFile < InvalidSchema
41
- def to_s
42
- "Failed to decode #{@pathname}"
43
- end
44
- end
45
-
46
- class InvalidSchemaFile < InvalidSchema
47
- def to_s
48
- "#{@pathname} was invalid JSON Schema"
49
- end
50
- end
51
-
52
- class BaseUrlNotFound < InvalidSchema
53
- def to_s
54
- "#{@pathname} has no base URL at top-level links property"
55
- end
56
- end
57
-
58
- class LinkNotFound < InvalidSchema
59
- def initialize(action_name: action_name, target_name: target_name, **args)
60
- super(**args)
61
- @action_name = action_name
62
- @target_name = target_name
63
- end
64
-
65
- def to_s
66
- "#{@pathname} has no definition for `#{@action_name} #{@target_name}`"
67
- end
68
12
  end
69
13
  end
@@ -1,3 +1,3 @@
1
1
  module Plz
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.8"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: plz
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
@@ -196,8 +196,17 @@ files:
196
196
  - images/screenshot.png
197
197
  - lib/plz.rb
198
198
  - lib/plz/arguments.rb
199
- - lib/plz/command.rb
200
199
  - lib/plz/command_builder.rb
200
+ - lib/plz/commands/base_url_not_found.rb
201
+ - lib/plz/commands/help.rb
202
+ - lib/plz/commands/invalid_schema.rb
203
+ - lib/plz/commands/link_not_found.rb
204
+ - lib/plz/commands/no_action_name.rb
205
+ - lib/plz/commands/no_target_name.rb
206
+ - lib/plz/commands/request.rb
207
+ - lib/plz/commands/schema_file_not_found.rb
208
+ - lib/plz/commands/undecodable_schema_file.rb
209
+ - lib/plz/commands/unparsable_json_param.rb
201
210
  - lib/plz/error.rb
202
211
  - lib/plz/error_command.rb
203
212
  - lib/plz/response_renderer.rb
@@ -1,59 +0,0 @@
1
- module Plz
2
- class Command
3
- # @param headers [Hash]
4
- # @param params [Hash]
5
- # @param method [String]
6
- # @param base_url [String]
7
- # @param path [String]
8
- # @param options [Hash]
9
- def initialize(headers: nil, params: nil, method: nil, base_url: nil, path: nil, options: nil)
10
- @headers = headers
11
- @params = params
12
- @method = method
13
- @base_url = base_url
14
- @path = path
15
- @options = options
16
- end
17
-
18
- # Sends an HTTP request and logs out the response
19
- def call
20
- response = client.send(@method.downcase, @path, @params, @headers)
21
- print ResponseRenderer.call(
22
- status: response.status,
23
- headers: response.headers,
24
- body: response.body,
25
- response_header: flag_to_show_response_header,
26
- response_body: flag_to_show_response_body,
27
- color: flag_to_color_response,
28
- )
29
- rescue Faraday::ConnectionFailed => exception
30
- puts exception
31
- end
32
-
33
- private
34
-
35
- # @return [Faraday::Connection]
36
- def client
37
- Faraday.new(url: @base_url) do |connection|
38
- connection.request :json
39
- connection.response :json
40
- connection.adapter :net_http
41
- end
42
- end
43
-
44
- # @return [true, false]
45
- def flag_to_show_response_header
46
- !@options[:"no-response-header"]
47
- end
48
-
49
- # @return [true, false]
50
- def flag_to_show_response_body
51
- !@options[:"no-response-body"]
52
- end
53
-
54
- # @return [true, false]
55
- def flag_to_color_response
56
- !@options[:"no-color"]
57
- end
58
- end
59
- end