plz 0.1.0 → 0.1.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9f816c8d4bae071f929bcf28be92356e8e0f09e6
4
- data.tar.gz: 629ccafae62941b40f0bba8f2c940f930224db9e
3
+ metadata.gz: 438be4824e988fa0a6ee8c9d115d1a3f0a484a96
4
+ data.tar.gz: 3355e4e54c324e5bab8470a27b03f147c32ad17e
5
5
  SHA512:
6
- metadata.gz: c89e94a5db1039fb38a36acdc2d1a73c5b36adc4789e8421b0436f314015b29e118bb773afa2c587d8451e60421ebd5f9918bab6b28b5740c0f6156dc08b7a7d
7
- data.tar.gz: 52dc00c48044fd44de75551d6bd909eb9135381e6578f56ed6021147e120e941f2f857032fcb1d5f733c5adc83666aff8385f129ed7a7271341b82f54a4d9c13
6
+ metadata.gz: 97b85d081396b16d302f8bb6471526197ebdab8b4df168cdc7e4b6260443eb302b2183491548047cbbc8d70ca3741527715e4097edc0cb49d2aec72f710ed79b
7
+ data.tar.gz: 563281675be4f77725203203cb59e4a4306e51189b756f9d6bea9a2e06b8a4ee8ece3d3fae92425d1784491c1a65e1fe06317da54dfc597f14312e938ee5ba50
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ ## 0.1.1
2
+ * Provide support for reading STDIN
3
+
1
4
  ## 0.1.0
2
5
  * Shape up --help feature
3
6
 
data/README.md CHANGED
@@ -54,6 +54,14 @@ while `key:=value` is parsed into JSON value (e.g. key:=17 will be `{"key":17}`)
54
54
  $ plz create user name=alice age:=17
55
55
  ```
56
56
 
57
+ ### Stdin
58
+ You can pass params via STDIN, instead of command line arguments.
59
+
60
+ ```sh
61
+ $ plz create user < params.json
62
+ $ cat params.json | plz create user
63
+ ```
64
+
57
65
  ### Options
58
66
  Plz takes some command line options.
59
67
 
data/lib/plz.rb CHANGED
@@ -20,6 +20,7 @@ require "plz/arguments"
20
20
  require "plz/command_builder"
21
21
  require "plz/commands/base_url_not_found"
22
22
  require "plz/commands/help"
23
+ require "plz/commands/invalid_json_from_stdin"
23
24
  require "plz/commands/invalid_schema"
24
25
  require "plz/commands/link_not_found"
25
26
  require "plz/commands/no_action_name"
data/lib/plz/arguments.rb CHANGED
@@ -15,10 +15,48 @@ module Plz
15
15
  ARGV[1]
16
16
  end
17
17
 
18
- # @return [Hash] Params parsed from given arguments
18
+ # @return [Hash] Params parsed from given arguments & STDIN
19
19
  # @raise [Plz::UnparsableJsonParam]
20
20
  def params
21
- @params ||= ARGV[2..-1].inject({}) do |result, section|
21
+ params_from_stdin.merge(params_from_argv)
22
+ end
23
+
24
+ # @return [Hash] Headers parsed from given arguments
25
+ def headers
26
+ ARGV[2..-1].inject({}) do |result, section|
27
+ case
28
+ when /(?<key>.+):(?<value>[^=]+)/ =~ section
29
+ result.merge(key => value)
30
+ else
31
+ result
32
+ end
33
+ end
34
+ end
35
+
36
+ # @return [true, false] true if invalid JSON input is given via STDIN
37
+ def has_invalid_json_input?
38
+ params_from_stdin
39
+ false
40
+ rescue JSON::ParserError
41
+ true
42
+ end
43
+
44
+ private
45
+
46
+ # @return [Hash] Params extracted from STDIN
47
+ def params_from_stdin
48
+ @params_from_stdin ||= begin
49
+ if has_input_from_stdin?
50
+ JSON.parse(STDIN.read)
51
+ else
52
+ {}
53
+ end
54
+ end
55
+ end
56
+
57
+ # @return [Hash] Params extracted from ARGV
58
+ def params_from_argv
59
+ @params_from_argv ||= ARGV[2..-1].inject({}) do |result, section|
22
60
  case
23
61
  when /(?<key>.+):=(?<value>.+)/ =~ section
24
62
  begin
@@ -34,16 +72,19 @@ module Plz
34
72
  end
35
73
  end
36
74
 
37
- # @return [Hash] Headers parsed from given arguments
38
- def headers
39
- ARGV[2..-1].inject({}) do |result, section|
40
- case
41
- when /(?<key>.+):(?<value>[^=]+)/ =~ section
42
- result.merge(key => value)
43
- else
44
- result
45
- end
46
- end
75
+ # @return [true, false] True if any input given via STDIN
76
+ def has_input_from_stdin?
77
+ has_pipe_input? || has_redirect_input?
78
+ end
79
+
80
+ # @return [true, false] True if any input given from redirection
81
+ def has_redirect_input?
82
+ File.select([STDIN], [], [], 0) != nil
83
+ end
84
+
85
+ # @return [true, false] True if any input given from pipe
86
+ def has_pipe_input?
87
+ File.pipe?(STDIN)
47
88
  end
48
89
  end
49
90
  end
@@ -16,6 +16,7 @@ module Plz
16
16
  :target_name,
17
17
  :headers,
18
18
  :params,
19
+ :has_invalid_json_input?,
19
20
  to: :arguments,
20
21
  )
21
22
 
@@ -47,6 +48,8 @@ module Plz
47
48
  action_name: action_name,
48
49
  target_name: target_name
49
50
  )
51
+ when has_invalid_json_input?
52
+ Commands::InvalidJsonFromStdin.new
50
53
  when has_unparsable_json_param?
51
54
  Commands::UnparsableJsonParam.new(error: @json_parse_error)
52
55
  else
@@ -23,9 +23,10 @@ module Plz
23
23
  end.map do |link|
24
24
  str = " plz #{link.title.underscore} #{target_name}"
25
25
  if key = link.href[/{(.+)}/, 1]
26
- name = CGI.unescape(key).gsub(/[()]/, "").split("/").last
27
- if property = link.parent.properties[name]
28
- if example = property.data["example"]
26
+ path = CGI.unescape(key).gsub(/[()]/, "")
27
+ name = path.split("/").last
28
+ if property = JsonPointer.evaluate(@schema.data, path)
29
+ if example = property["example"]
29
30
  str << " #{name}=#{example}"
30
31
  end
31
32
  end
@@ -0,0 +1,9 @@
1
+ module Plz
2
+ module Commands
3
+ class InvalidJsonFromStdin
4
+ def call
5
+ puts "Invalid JSON was given from STDIN"
6
+ end
7
+ end
8
+ end
9
+ end
data/lib/plz/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Plz
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
data/schema.yml CHANGED
@@ -49,12 +49,12 @@ definitions:
49
49
  rel: create
50
50
  title: Create
51
51
  - description: Update the user
52
- href: "/users/{(#/definitions/users/definitions/id)}"
52
+ href: "/users/{(#/definitions/user/definitions/id)}"
53
53
  method: PATCH
54
54
  rel: update
55
55
  title: Update
56
56
  - description: Delete the user
57
- href: "/users/{(#/definitions/users/definitions/id)}"
57
+ href: "/users/{(#/definitions/user/definitions/id)}"
58
58
  method: DELETE
59
59
  rel: delete
60
60
  title: Delete
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.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
@@ -199,6 +199,7 @@ files:
199
199
  - lib/plz/command_builder.rb
200
200
  - lib/plz/commands/base_url_not_found.rb
201
201
  - lib/plz/commands/help.rb
202
+ - lib/plz/commands/invalid_json_from_stdin.rb
202
203
  - lib/plz/commands/invalid_schema.rb
203
204
  - lib/plz/commands/link_not_found.rb
204
205
  - lib/plz/commands/no_action_name.rb