plz 0.0.3 → 0.0.5

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: d16e7dc1ce2ee151b95707d38a2c7e01c083e8e4
4
- data.tar.gz: 7c3589d4cafde4a6b993ec2d5b073c2cc43b0ba3
3
+ metadata.gz: f5260f7a973909cd914441eb5b29d5611e6a1aff
4
+ data.tar.gz: 501f2095a5c66ff2b40ec1ebe742f6ded54f3f4e
5
5
  SHA512:
6
- metadata.gz: b252fcd39c83256aba2b89a0f1e64c00c753865cac668c8124ba0fb56e78eb022c3d1df42a3089a529f553de65f403e96bb643f5f0296d5ff5243311286946cd
7
- data.tar.gz: 4e379afb816dc5f2a2cf4b2e0812f4bc8c1b4c7cf76a17aa5d5cf19d4b62b673d7e2b82664a4383a95f23ab7f494cd99f8f4dda1316e94768a898b63d5c753f7
6
+ metadata.gz: f29827446649900133e5fd58ce91086434fbd4a3f0d7454876b6d298ca83dc4a3b35a9f94665caae6875139f636015f4556f8b81ab7bf112c804aa5bfbe3e689
7
+ data.tar.gz: 0432bc2eb51b8efc96ca242a612d05d3d64a568aa99f2594834bbbe0232f6f832d3fbd8782aa0cdb3de737c3a56d6ae62db1f09e14ec31dec6a8d5f47ea19a17
@@ -1,3 +1,9 @@
1
+ ## 0.0.5
2
+ * Support URI Template in API path
3
+
4
+ ## 0.0.4
5
+ * Implement headers & params parser
6
+
1
7
  ## 0.0.3
2
8
  * Color header value
3
9
 
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # Plz
2
2
  JSON Schema based command line HTTP client.
3
3
 
4
- ## Usage
4
+ ![screenshot](images/screenshot.png)
5
5
 
6
6
  ### Install
7
7
  ```sh
@@ -29,6 +29,7 @@ $ plz <action> <target> [headers|params]
29
29
  To use Plz, you need to have a JSON Schema file at `./schema.json` or `./schema.yaml`,
30
30
  that describes about the API where you want to send HTTP request.
31
31
  Plz interprets command-line arguments based on that JSON Schema, then sends HTTP request.
32
+ See [schema.yml](schema.yml) as an example.
32
33
 
33
34
  ### Example
34
35
  ```sh
Binary file
data/lib/plz.rb CHANGED
@@ -1,3 +1,6 @@
1
+ require "active_support/core_ext/hash/keys"
2
+ require "active_support/core_ext/hash/slice"
3
+ require "active_support/core_ext/module/delegation"
1
4
  require "active_support/core_ext/string/inflections"
2
5
  require "active_support/core_ext/string/strip"
3
6
  require "faraday"
@@ -11,6 +14,7 @@ require "rouge"
11
14
  require "yaml"
12
15
 
13
16
  require "plz/error"
17
+ require "plz/arguments"
14
18
  require "plz/command"
15
19
  require "plz/command_builder"
16
20
  require "plz/error_command"
@@ -0,0 +1,49 @@
1
+ module Plz
2
+ class Arguments
3
+ # @param arguments [Array] Raw ARGV
4
+ def initialize(argv)
5
+ @argv = argv
6
+ end
7
+
8
+ # @return [String, nil] Given action name
9
+ def action_name
10
+ ARGV[0]
11
+ end
12
+
13
+ # @return [String, nil] Given target name
14
+ def target_name
15
+ ARGV[1]
16
+ end
17
+
18
+ # @return [Hash] Params parsed from given arguments
19
+ # @raise [Plz::UnparsableJsonParam]
20
+ def params
21
+ ARGV[2..-1].inject({}) do |result, section|
22
+ case
23
+ when /(?<key>.+):=(?<value>.+)/ =~ section
24
+ begin
25
+ result.merge(key => JSON.parse(%<{"key":#{value}}>)["key"])
26
+ rescue JSON::ParserError
27
+ raise UnparsableJsonParam, value: value
28
+ end
29
+ when /(?<key>.+)=(?<value>.+)/ =~ section
30
+ result.merge(key => value)
31
+ else
32
+ result
33
+ end
34
+ end
35
+ end
36
+
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
47
+ end
48
+ end
49
+ end
@@ -10,9 +10,17 @@ module Plz
10
10
  new(arguments).call
11
11
  end
12
12
 
13
- # @param [Array<String>] ARGV
14
- def initialize(arguments)
15
- @arguments = arguments
13
+ delegate(
14
+ :action_name,
15
+ :target_name,
16
+ :headers,
17
+ :params,
18
+ to: :arguments,
19
+ )
20
+
21
+ # @param argv [Array<String>] Raw ARGV
22
+ def initialize(argv)
23
+ @argv = argv
16
24
  end
17
25
 
18
26
  # @return [Plz::Command] Callable command object
@@ -23,7 +31,7 @@ module Plz
23
31
  base_url: base_url,
24
32
  path: path,
25
33
  headers: headers,
26
- params: params,
34
+ params: request_params,
27
35
  )
28
36
  rescue Error => error
29
37
  ErrorCommand.new(error)
@@ -109,32 +117,44 @@ module Plz
109
117
  @schema_file_pathname ||= Pathname.glob(SCHEMA_FILE_PATH_PATTERN).first
110
118
  end
111
119
 
112
- # @return [String, nil] Given action name
113
- def action_name
114
- ARGV[0]
120
+ # @return [String]
121
+ # @example
122
+ # path #=> "/users"
123
+ def path
124
+ path_with_template % path_params.symbolize_keys
115
125
  end
116
126
 
117
- # @return [String, nil] Given target name
118
- def target_name
119
- ARGV[1]
127
+ # @return [String]
128
+ # @example
129
+ # path_with_template #=> "/apps/%{id}"
130
+ def path_with_template
131
+ link.href.gsub(/{(.+)}/) do |matched|
132
+ key = CGI.unescape($1).gsub(/[()]/, "").split("/").last
133
+ "%{#{key}}"
134
+ end
120
135
  end
121
136
 
122
- # TODO
123
- # @return [Hash] Params made from given arguments
124
- def params
125
- {}
137
+ # @return [Array<String>] Parameter names required for path
138
+ # @exmaple
139
+ # path_keys #=> ["id"]
140
+ def path_keys
141
+ link.href.scan(/{(.+)}/).map do |str|
142
+ CGI.unescape($1).gsub(/[()]/, "").split("/").last
143
+ end
126
144
  end
127
145
 
128
- # TODO
129
- # @return [Hash] Headers made from given arguments
130
- def headers
146
+ # @return [Hash] Params to be embedded into path
147
+ # @example
148
+ # path_params #=> { "id" => 1 }
149
+ def path_params
150
+ params.slice(*path_keys)
131
151
  end
132
152
 
133
- # @return [String]
153
+ # @return [Hash] Params to be used for request body or query string
134
154
  # @example
135
- # path #=> "/users"
136
- def path
137
- link.href
155
+ # request_params #=> { "name" => "example" }
156
+ def request_params
157
+ params.except(*path_keys)
138
158
  end
139
159
 
140
160
  # @return [String]
@@ -175,62 +195,9 @@ module Plz
175
195
  rescue JsonSchema::SchemaError
176
196
  end
177
197
 
178
- class Error < Error
179
- USAGE = "Usage: plz <action> <target> [headers|params]"
180
- end
181
-
182
- class NoActionName < Error
183
- def to_s
184
- "You didn't pass action name\n#{USAGE}"
185
- end
186
- end
187
-
188
- class NoTargetName < Error
189
- def to_s
190
- "You didn't pass target name\n#{USAGE}"
191
- end
192
- end
193
-
194
- class SchemaFileNotFound < Error
195
- def to_s
196
- "Schema file was not found in #{SCHEMA_FILE_PATH_PATTERN}"
197
- end
198
- end
199
-
200
- class InvalidSchema < Error
201
- def initialize(pathname: pathname)
202
- @pathname = pathname
203
- end
204
- end
205
-
206
- class UndecodableSchemaFile < InvalidSchema
207
- def to_s
208
- "Failed to decode #{@pathname}"
209
- end
210
- end
211
-
212
- class InvalidSchemaFile < InvalidSchema
213
- def to_s
214
- "#{@pathname} was invalid JSON Schema"
215
- end
216
- end
217
-
218
- class BaseUrlNotFound < InvalidSchema
219
- def to_s
220
- "#{@pathname} has no base URL at top-level links property"
221
- end
222
- end
223
-
224
- class LinkNotFound < InvalidSchema
225
- def initialize(action_name: action_name, target_name: target_name, **args)
226
- super(**args)
227
- @action_name = action_name
228
- @target_name = target_name
229
- end
230
-
231
- def to_s
232
- "#{@pathname} has no definition for `#{@action_name} #{@target_name}`"
233
- end
198
+ # @return [Plz::Arguments] Wrapper of Raw ARGV
199
+ def arguments
200
+ @arguments ||= Arguments.new(@argv)
234
201
  end
235
202
  end
236
203
  end
@@ -1,4 +1,69 @@
1
1
  module Plz
2
2
  class Error < StandardError
3
+ USAGE = "Usage: plz <action> <target> [headers|params]"
4
+ end
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
+ class UnparsableJsonParam < Error
25
+ def initialize(value: nil)
26
+ @value = value
27
+ 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
3
68
  end
4
69
  end
@@ -1,3 +1,3 @@
1
1
  module Plz
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -25,4 +25,6 @@ Gem::Specification.new do |spec|
25
25
  spec.add_dependency "rouge"
26
26
  spec.add_development_dependency "bundler", "~> 1.6"
27
27
  spec.add_development_dependency "rake"
28
+ spec.add_development_dependency "rspec", "2.14.1"
29
+ spec.add_development_dependency "rack-json_schema"
28
30
  end
data/schema.yml CHANGED
@@ -74,8 +74,8 @@ properties:
74
74
  "$ref": "#/definitions/user"
75
75
  type:
76
76
  - object
77
- description: API server for TQKey
77
+ description: Example API for Plz
78
78
  links:
79
79
  - href: http://localhost:8080
80
80
  rel: self
81
- title: Dummy API
81
+ title: Example API
@@ -0,0 +1,5 @@
1
+ require "spec_helper"
2
+
3
+ describe Plz do
4
+ it { should be_true }
5
+ end
@@ -0,0 +1,7 @@
1
+ require "plz"
2
+
3
+ RSpec.configure do |config|
4
+ config.treat_symbols_as_metadata_keys_with_true_values = true
5
+ config.run_all_when_everything_filtered = true
6
+ config.filter_run :focus
7
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: plz
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-12 00:00:00.000000000 Z
11
+ date: 2014-06-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -136,6 +136,34 @@ dependencies:
136
136
  - - ">="
137
137
  - !ruby/object:Gem::Version
138
138
  version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rspec
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - '='
144
+ - !ruby/object:Gem::Version
145
+ version: 2.14.1
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - '='
151
+ - !ruby/object:Gem::Version
152
+ version: 2.14.1
153
+ - !ruby/object:Gem::Dependency
154
+ name: rack-json_schema
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
139
167
  description:
140
168
  email:
141
169
  - r7kamura@gmail.com
@@ -151,7 +179,9 @@ files:
151
179
  - README.md
152
180
  - Rakefile
153
181
  - bin/plz
182
+ - images/screenshot.png
154
183
  - lib/plz.rb
184
+ - lib/plz/arguments.rb
155
185
  - lib/plz/command.rb
156
186
  - lib/plz/command_builder.rb
157
187
  - lib/plz/error.rb
@@ -160,6 +190,8 @@ files:
160
190
  - lib/plz/version.rb
161
191
  - plz.gemspec
162
192
  - schema.yml
193
+ - spec/plz_spec.rb
194
+ - spec/spec_helper.rb
163
195
  homepage: https://github.com/r7kamura/plz
164
196
  licenses:
165
197
  - MIT
@@ -184,5 +216,7 @@ rubygems_version: 2.2.2
184
216
  signing_key:
185
217
  specification_version: 4
186
218
  summary: JSON Schema based command line HTTP client.
187
- test_files: []
219
+ test_files:
220
+ - spec/plz_spec.rb
221
+ - spec/spec_helper.rb
188
222
  has_rdoc: