leif 0.0.5 → 0.0.6

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: 9ea567a178df95bf78d96ecd8785fdc0efe759bc
4
- data.tar.gz: a28cad7fb674260307e5e582f8b13a245cb0d814
3
+ metadata.gz: 3610c76b9d9f405c0e4bdb4e0a32e9762cd51cb4
4
+ data.tar.gz: ef233fb2ad75558607817a0c0497263ea4a6d681
5
5
  SHA512:
6
- metadata.gz: 1bb6da4ca06176c47e46c2a8647efac6d8af5eff9e4b44aa799d94a16e6b958966a490adc33ae1072b7a03a6d1b0e5a5eba0f3c139ec232dfcb607be6ec3df48
7
- data.tar.gz: ca287bcfc08378281846e479c30e2cb07a5430de571bfcfd50fcd4d55d85550009dc33ebf477b96c9be5761ed849a9e337f5d2e6da3c4b863fa8ff7e65d07aa6
6
+ metadata.gz: 1fe9d60703d464079f71339e5aeaf3ec2a7d8fc9adc89f809bca82deb98e21f8da1665c5711496aa1ff279fa517dcd42865a9240c08bf03f0c159db0a04cb55c
7
+ data.tar.gz: 1b5a54e33254a55c1d06e63ff73c889631ed7253d0dcb034d3687bf164be395d61ba60eb499d606f63bf306a20158c065eb6de316545784aa922778f82a54cce
data/bin/leif CHANGED
@@ -3,15 +3,10 @@ require 'leif/cli'
3
3
  require 'leif/collection_json'
4
4
  require 'leif/section'
5
5
 
6
- trap('INT') do
7
- puts
8
- exit
9
- end
10
-
11
6
  Leif::Cli.new.tap do |cli|
12
7
  cli.get_root
13
8
  loop do
14
- cli.print_response
9
+ cli.print_overview
15
10
  cli.get_next_action
16
11
  end
17
12
  end
data/lib/leif/cli.rb CHANGED
@@ -52,29 +52,15 @@ module Leif
52
52
  make_request @response.env[:url].request_uri
53
53
  end
54
54
 
55
- def print_response
56
- banner 'Request' do |out|
57
- out.print "#{@response.env[:method].upcase} #{@response.env[:url]}"
58
- out.print @response.env[:request_headers].map {|header, value|
59
- "#{header}: #{value}"
60
- }
61
- end
62
-
63
- banner 'Response' do |out|
64
- out.print @response.headers.map {|header, value|
65
- "#{header}: #{value}"
66
- }
67
- end
55
+ def print_overview
56
+ print_request
57
+ print_response
68
58
 
69
59
  banner 'Body' do |out|
70
60
  out.print JSON.pretty_generate(@response.body).lines
71
61
  end
72
62
 
73
- banner 'Links' do |out|
74
- unless collection.link_relations.empty?
75
- out.print collection.link_relations.join(', ')
76
- end
77
- end
63
+ print_links collection
78
64
  end
79
65
 
80
66
  def request_basic_authentication(username = :ask, password = :ask)
@@ -89,24 +75,21 @@ module Leif
89
75
  retry_request
90
76
  end
91
77
 
92
- def follow_link(relation = :ask)
78
+ def follow_link(subject, relation = :ask)
93
79
  relation = ask('Relation: ') if relation == :ask
94
- make_request collection.link_href(relation)
80
+ make_request subject.link_href(relation)
95
81
  end
96
82
 
97
83
  def create_item
98
84
  template = collection.template
99
85
 
100
86
  loop do
101
- banner 'Create Item' do |out|
102
- out.print JSON.pretty_generate(template).lines
103
- end
104
-
87
+ print_template template, 'Create Item'
105
88
  puts
106
89
  puts 'Fill the template to create a new item.'
107
90
  name = ask('Name (empty to submit): ')
108
91
  break if name.empty?
109
- value = ask('Value: ')
92
+ value = ask_for_primitive('Value: ')
110
93
 
111
94
  template = template.fill_field name, value
112
95
  end
@@ -114,33 +97,16 @@ module Leif
114
97
  make_request template.href, template.convert_to_json, template.method
115
98
  end
116
99
 
117
- def update_item
118
- item = collection.items.find do |item|
119
- banner 'Item' do |out|
120
- out.print JSON.pretty_generate(item).lines
121
- end
122
-
123
- puts
124
- response = ask('Select this item to update [y,n]? ') do |q|
125
- q.character = true
126
- q.validate = /\A[yn]\Z/
127
- end
128
-
129
- response == 'y'
130
- end
131
-
100
+ def update(item)
132
101
  template = collection.item_template item
133
102
 
134
103
  loop do
135
- banner 'Update Item' do |out|
136
- out.print JSON.pretty_generate(template).lines
137
- end
138
-
104
+ print_template template, 'Update Item'
139
105
  puts
140
106
  puts 'Fill the template to update the item.'
141
107
  name = ask('Name (empty to submit): ')
142
108
  break if name.empty?
143
- value = ask('Value: ')
109
+ value = ask_for_primitive('Value: ')
144
110
 
145
111
  template = template.fill_field name, value
146
112
  end
@@ -148,6 +114,64 @@ module Leif
148
114
  make_request template.href, template.convert_to_json, template.method
149
115
  end
150
116
 
117
+ def print_request
118
+ banner 'Request' do |out|
119
+ out.print "#{@response.env[:method].upcase} #{@response.env[:url]}"
120
+ out.print @response.env[:request_headers].map {|header, value|
121
+ "#{header}: #{value}"
122
+ }
123
+ end
124
+ end
125
+
126
+ def print_response
127
+ banner 'Response' do |out|
128
+ out.print @response.headers.map {|header, value|
129
+ "#{header}: #{value}"
130
+ }
131
+ end
132
+ end
133
+
134
+ def print_links(subject)
135
+ banner 'Links' do |out|
136
+ unless subject.link_relations.empty?
137
+ out.print subject.link_relations.join(', ')
138
+ end
139
+ end
140
+ end
141
+
142
+ def print_template(template = collection.template, label = 'Template')
143
+ banner label do |out|
144
+ out.print JSON.pretty_generate(template).lines
145
+ end
146
+ end
147
+
148
+ def print_items
149
+ item = select_item
150
+ print_links item
151
+ get_next_item_action item
152
+ rescue Interrupt
153
+ print '^C'
154
+ end
155
+
156
+ def print_item(item)
157
+ banner 'Item' do |out|
158
+ out.print JSON.pretty_generate(item).lines
159
+ end
160
+ end
161
+
162
+ def select_item
163
+ collection.items.find do |item|
164
+ print_item item
165
+ puts
166
+ response = ask('Select this item [y,n]? ') do |q|
167
+ q.character = true
168
+ q.validate = /\A[yn]\Z/
169
+ end
170
+
171
+ response == 'y'
172
+ end
173
+ end
174
+
151
175
  def print_debug
152
176
  banner 'Debug' do |out|
153
177
  debug_output.rewind
@@ -156,42 +180,109 @@ module Leif
156
180
  end
157
181
 
158
182
  def print_help
159
- puts <<EOS
160
- root:
161
- Go back to the root
183
+ banner 'Help' do |out|
184
+ out.print <<EOS.lines
185
+ root:
186
+ Go back to the root.
162
187
 
163
- follow <rel>:
164
- Follow link with the given relation.
188
+ follow <rel>:
189
+ Follow link with the relation <rel> on the collection.
165
190
 
166
- template [<name>=<value>...]:
167
- Fill the template with the given name/value pairs and submit.
191
+ create:
192
+ Begin editing the template to create a new item.
168
193
 
169
- basic [<username> [<password>]]:
170
- Authenticate with HTTP Basic and reload the current resource. Will be
171
- prompted for username and password if omitted.
194
+ request:
195
+ Reprint the details of the last request.
172
196
 
173
- token <token>:
174
- Authenticate using the given token and reload the current resource.
197
+ response:
198
+ Reprint the details of the last response.
175
199
 
176
- debug:
177
- Print debug output from the previous HTTP request and response.
200
+ template:
201
+ Print the template from the last response.
202
+
203
+ items:
204
+ Print each item from the last response one at a time in order to update,
205
+ delete, or follow an item's link.
206
+
207
+ basic [<username> [<password>]]:
208
+ Authenticate with HTTP Basic and reload the current resource. Will be
209
+ prompted for username and password if omitted.
210
+
211
+ token <token>:
212
+ Authenticate using the given token and reload the current resource.
213
+
214
+ debug:
215
+ Print debug output from the previous HTTP request and response.
216
+
217
+ quit:
218
+ Exit leif.
178
219
  EOS
220
+ end
221
+ end
222
+
223
+ def print_item_help
224
+ banner 'Help' do |out|
225
+ out.print <<EOS.lines
226
+ item:
227
+ Print the selected item.
228
+
229
+ cancel:
230
+ Cancel item selection and go back to the collection.
231
+
232
+ follow <rel>:
233
+ Follow link with the relation <rel> on the selected item.
234
+
235
+ update:
236
+ Begin editing the template to update the selected item.
237
+
238
+ quit:
239
+ Exit leif.
240
+ EOS
241
+ end
242
+ end
243
+
244
+ def get_next_item_action(item)
245
+ command, args = ask_for_action
246
+ case command
247
+ when 'item' then print_item(item); get_next_item_action(item)
248
+ when 'update' then update(item)
249
+ when 'f', 'follow' then follow_link(item, *args)
250
+ when 'cancel'
251
+ when 'q', 'quit' then exit
252
+ when '?', 'help' then print_item_help; get_next_item_action(item)
253
+ else puts 'Try again.'; get_next_item_action(item)
254
+ end
255
+ rescue Interrupt
256
+ print '^C'
179
257
  end
180
258
 
181
259
  def get_next_action
182
260
  command, args = ask_for_action
183
261
  case command
184
262
  when 'r', 'root' then get_root
185
- when 'f', 'follow' then follow_link(*args)
263
+ when 'f', 'follow' then follow_link(collection, *args)
186
264
  when 'create' then create_item
187
265
  when 'update' then update_item
266
+
267
+ when 'request' then print_request; get_next_action
268
+ when 'response' then print_response; get_next_action
269
+ when 'template' then print_template; get_next_action
270
+ when 'items' then print_items
271
+
188
272
  when 'b', 'basic' then request_basic_authentication(*args)
189
273
  when 't', 'token' then request_token_authentication(*args)
274
+
190
275
  when 'd', 'debug' then print_debug; get_next_action
191
276
  when '?', 'help' then print_help; get_next_action
192
277
  when 'q', 'quit' then exit
193
278
  else puts 'Try again.'; get_next_action
194
279
  end
280
+ rescue Interrupt
281
+ print '^C'
282
+ get_next_action
283
+ rescue EOFError
284
+ puts
285
+ exit
195
286
  end
196
287
 
197
288
  def ask_for_action
@@ -199,5 +290,24 @@ EOS
199
290
  input = ask('> ') {|q| q.readline = true }.split(/\s/)
200
291
  [ input.first, input[1..-1] ]
201
292
  end
293
+
294
+ def ask_for_primitive(message)
295
+ value = ask(message)
296
+ case value
297
+ when 'null', 'nil' then nil
298
+ when '"null"' then 'null'
299
+ when '"nil"' then 'nil'
300
+
301
+ when 'true' then true
302
+ when 'false' then false
303
+ when '"true"' then 'true'
304
+ when '"false"' then 'false'
305
+
306
+ when /\A\d+\Z/ then Integer(value)
307
+ when /\A"\d+"\Z/ then value[1..-2]
308
+
309
+ else value
310
+ end
311
+ end
202
312
  end
203
313
  end
@@ -3,14 +3,7 @@ require 'forwardable'
3
3
 
4
4
  module Leif
5
5
  module CollectionJson
6
- class Collection
7
- extend Forwardable
8
- def_delegators :@data, :fetch, :has_key?
9
-
10
- def initialize(body)
11
- @data = body.fetch('collection')
12
- end
13
-
6
+ module Linked
14
7
  def link_href(relation)
15
8
  links.find {|link| link.fetch('rel') == relation }.fetch('href')
16
9
  end
@@ -23,10 +16,34 @@ module Leif
23
16
  return [] unless has_key?('links')
24
17
  fetch('links')
25
18
  end
19
+ end
20
+
21
+ class Collection
22
+ extend Forwardable
23
+ include Linked
24
+
25
+ def_delegators :@data, :fetch, :has_key?
26
+
27
+ def initialize(body)
28
+ @data = body.fetch('collection')
29
+ end
30
+
31
+ # def link_href(relation)
32
+ # links.find {|link| link.fetch('rel') == relation }.fetch('href')
33
+ # end
34
+
35
+ # def link_relations
36
+ # links.map {|link| link.fetch('rel') }
37
+ # end
38
+
39
+ # def links
40
+ # return [] unless has_key?('links')
41
+ # fetch('links')
42
+ # end
26
43
 
27
44
  def items
28
45
  return [] unless has_key?('items')
29
- fetch('items')
46
+ fetch('items').map {|item| Item.new(item) }
30
47
  end
31
48
 
32
49
  def template(href = fetch('href'), method = :post)
@@ -40,6 +57,10 @@ module Leif
40
57
  }
41
58
  end
42
59
 
60
+ class Item < SimpleDelegator
61
+ include Linked
62
+ end
63
+
43
64
  class Template < SimpleDelegator
44
65
  attr_accessor :href, :method
45
66
 
data/lib/leif/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Leif
2
- VERSION = '0.0.5'
2
+ VERSION = '0.0.6'
3
3
  end
data/man/leif.1 CHANGED
@@ -16,15 +16,39 @@
16
16
  .
17
17
  .TP
18
18
  \fBroot\fR
19
- Go back to the root
19
+ Go back to the root\.
20
20
  .
21
21
  .TP
22
22
  \fBfollow\fR \fIrel\fR
23
- Follow link with the given relation\.
23
+ Follow link with the relation \fIrel\fR on the collection or selected item\.
24
24
  .
25
25
  .TP
26
- \fBtemplate\fR [\fIname\fR=\fIvalue\fR\.\.\.]
27
- Fill the template with the given name/value pairs and submit\.
26
+ \fBcreate\fR
27
+ Begin editing the template to create a new item\.
28
+ .
29
+ .TP
30
+ \fBupdate\fR
31
+ Begin editing the template to update the item selected with \fBitems\fR\.
32
+ .
33
+ .TP
34
+ \fBrequest\fR
35
+ Reprint the details of the last request\.
36
+ .
37
+ .TP
38
+ \fBresponse\fR
39
+ Reprint the details of the last response\.
40
+ .
41
+ .TP
42
+ \fBtemplate\fR
43
+ Print the template from the last response\.
44
+ .
45
+ .TP
46
+ \fBitems\fR
47
+ Print each item from the last response one at a time in order to update, delete, or follow an item\'s link\.
48
+ .
49
+ .TP
50
+ \fBitem\fR
51
+ Print the selected item\.
28
52
  .
29
53
  .TP
30
54
  \fBbasic\fR [\fIusername\fR [\fIpassword\fR]]
@@ -38,6 +62,14 @@ Authenticate using the given token and reload the current resource\.
38
62
  \fBdebug\fR
39
63
  Print debug output from the previous HTTP request and response\.
40
64
  .
65
+ .TP
66
+ \fBhelp\fR
67
+ Print available commands\.
68
+ .
69
+ .TP
70
+ \fBquit\fR
71
+ Exit \fBleif\fR\.
72
+ .
41
73
  .SH "EXAMPLES"
42
74
  Follow a rel=account link:
43
75
  .
@@ -119,4 +151,23 @@ Name (empty to submit):
119
151
  .fi
120
152
  .
121
153
  .IP "" 0
154
+ .
155
+ .P
156
+ JSON primitives (null, integers, and booleans) are interpreted as such\. Use quotes for literal strings\.
157
+ .
158
+ .IP "" 4
159
+ .
160
+ .nf
161
+
162
+ > create
163
+
164
+ Name (empty to submit): private
165
+ Value: true
166
+
167
+ Name (empty to submit): name
168
+ Value: "true"
169
+ .
170
+ .fi
171
+ .
172
+ .IP "" 0
122
173
 
data/man/leif.1.html CHANGED
@@ -90,13 +90,22 @@ programs.</p>
90
90
  <h2 id="INTERACTIVE-COMMANDS">INTERACTIVE COMMANDS</h2>
91
91
 
92
92
  <dl>
93
- <dt class="flush"><code>root</code></dt><dd><p>Go back to the root</p></dd>
94
- <dt><code>follow</code> <var>rel</var></dt><dd><p>Follow link with the given relation.</p></dd>
95
- <dt><code>template</code> [<var>name</var>=<var>value</var>...]</dt><dd><p>Fill the template with the given name/value pairs and submit.</p></dd>
93
+ <dt class="flush"><code>root</code></dt><dd><p>Go back to the root.</p></dd>
94
+ <dt><code>follow</code> <var>rel</var></dt><dd><p>Follow link with the relation <var>rel</var> on the collection or selected item.</p></dd>
95
+ <dt class="flush"><code>create</code></dt><dd><p>Begin editing the template to create a new item.</p></dd>
96
+ <dt class="flush"><code>update</code></dt><dd><p>Begin editing the template to update the item selected with <code>items</code>.</p></dd>
97
+ <dt class="flush"><code>request</code></dt><dd><p>Reprint the details of the last request.</p></dd>
98
+ <dt><code>response</code></dt><dd><p>Reprint the details of the last response.</p></dd>
99
+ <dt><code>template</code></dt><dd><p>Print the template from the last response.</p></dd>
100
+ <dt class="flush"><code>items</code></dt><dd><p>Print each item from the last response one at a time in order to update,
101
+ delete, or follow an item's link.</p></dd>
102
+ <dt class="flush"><code>item</code></dt><dd><p>Print the selected item.</p></dd>
96
103
  <dt><code>basic</code> [<var>username</var> [<var>password</var>]]</dt><dd><p>Authenticate with HTTP Basic and reload the current resource. Will be
97
104
  prompted for username and password if omitted.</p></dd>
98
105
  <dt><code>token</code> <var>token</var></dt><dd><p>Authenticate using the given token and reload the current resource.</p></dd>
99
106
  <dt class="flush"><code>debug</code></dt><dd><p>Print debug output from the previous HTTP request and response.</p></dd>
107
+ <dt class="flush"><code>help</code></dt><dd><p>Print available commands.</p></dd>
108
+ <dt class="flush"><code>quit</code></dt><dd><p>Exit <code>leif</code>.</p></dd>
100
109
  </dl>
101
110
 
102
111
 
@@ -144,6 +153,18 @@ Value: towel
144
153
  Name (empty to submit):
145
154
  </code></pre>
146
155
 
156
+ <p>JSON primitives (null, integers, and booleans) are interpreted as such. Use
157
+ quotes for literal strings.</p>
158
+
159
+ <pre><code>&gt; create
160
+
161
+ Name (empty to submit): private
162
+ Value: true
163
+
164
+ Name (empty to submit): name
165
+ Value: "true"
166
+ </code></pre>
167
+
147
168
 
148
169
  <ol class='man-decor man-foot man foot'>
149
170
  <li class='tl'></li>
data/man/leif.1.ronn CHANGED
@@ -13,13 +13,32 @@ programs.
13
13
  ## INTERACTIVE COMMANDS
14
14
 
15
15
  - `root`:
16
- Go back to the root
16
+ Go back to the root.
17
17
 
18
18
  - `follow` <rel>:
19
- Follow link with the given relation.
19
+ Follow link with the relation <rel> on the collection or selected item.
20
20
 
21
- - `template` [<name>=<value>...]:
22
- Fill the template with the given name/value pairs and submit.
21
+ - `create`:
22
+ Begin editing the template to create a new item.
23
+
24
+ - `update`:
25
+ Begin editing the template to update the item selected with `items`.
26
+
27
+ - `request`:
28
+ Reprint the details of the last request.
29
+
30
+ - `response`:
31
+ Reprint the details of the last response.
32
+
33
+ - `template`:
34
+ Print the template from the last response.
35
+
36
+ - `items`:
37
+ Print each item from the last response one at a time in order to update,
38
+ delete, or follow an item's link.
39
+
40
+ - `item`:
41
+ Print the selected item.
23
42
 
24
43
  - `basic` [<username> [<password>]]:
25
44
  Authenticate with HTTP Basic and reload the current resource. Will be
@@ -31,6 +50,12 @@ programs.
31
50
  - `debug`:
32
51
  Print debug output from the previous HTTP request and response.
33
52
 
53
+ - `help`:
54
+ Print available commands.
55
+
56
+ - `quit`:
57
+ Exit `leif`.
58
+
34
59
  ## EXAMPLES
35
60
 
36
61
  Follow a rel=account link:
@@ -69,3 +94,14 @@ Update an item filling the `password` field:
69
94
  Value: towel
70
95
 
71
96
  Name (empty to submit):
97
+
98
+ JSON primitives (null, integers, and booleans) are interpreted as such. Use
99
+ quotes for literal strings.
100
+
101
+ > create
102
+
103
+ Name (empty to submit): private
104
+ Value: true
105
+
106
+ Name (empty to submit): name
107
+ Value: "true"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: leif
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Larry Marburger