Class: Leif::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/leif/cli.rb

Instance Method Summary collapse

Instance Method Details

#ask_for_actionObject



288
289
290
291
292
# File 'lib/leif/cli.rb', line 288

def ask_for_action
  puts
  input = ask('> ') {|q| q.readline = true }.split(/\s/)
  [ input.first, input[1..-1] ]
end


27
28
29
30
# File 'lib/leif/cli.rb', line 27

def banner(banner, &message)
  puts
  Section.banner(banner, &message)
end

#collectionObject



43
44
45
# File 'lib/leif/cli.rb', line 43

def collection
  Leif::CollectionJson::Collection.new(@response.body)
end

#connObject



10
11
12
13
14
15
16
17
# File 'lib/leif/cli.rb', line 10

def conn
  @conn ||= Faraday.new(url: 'https://api.getcloudapp.com') do |config|
    config.request  :url_encoded
    config.response :logger, logger
    config.response :json, :content_type => /\bjson$/
    config.adapter  Faraday.default_adapter
  end
end

#convert_to_primitive(value) ⇒ Object



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/leif/cli.rb', line 294

def convert_to_primitive(value)
  case value
  when 'null', 'nil', '' then nil
  when '"null"' then 'null'
  when '"nil"'  then 'nil'
  when '""'     then ''

  when 'true'    then true
  when 'false'   then false
  when '"true"'  then 'true'
  when '"false"' then 'false'

  when /\A\d+\Z/   then Integer(value)
  when /\A"\d+"\Z/ then value[1..-2]

  else value
  end
end

#create_itemObject



94
95
96
97
# File 'lib/leif/cli.rb', line 94

def create_item
  fill_and_submit_template collection.template,
                           'Fill the template to create a new item.'
end

#debug_outputObject



23
24
25
# File 'lib/leif/cli.rb', line 23

def debug_output
  @debug_output ||= StringIO.new
end

#fill_and_submit_template(template, label) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/leif/cli.rb', line 79

def fill_and_submit_template(template, label)
  print_template template
  puts
  puts label
  template.each do |name, value|
    printed_value = value.inspect
    printed_value = 'null' if printed_value == 'nil'
    new_value = ask("#{name} [#{printed_value}]: ")
    new_value = new_value.empty? ? value : convert_to_primitive(new_value)
    template  = template.fill_field name, new_value
  end

  make_request template.href, template.convert_to_json, template.method
end


74
75
76
77
# File 'lib/leif/cli.rb', line 74

def follow_link(subject, relation = :ask)
  relation = ask('Relation: ') if relation == :ask
  make_request subject.link_href(relation)
end

#get_next_actionObject



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/leif/cli.rb', line 258

def get_next_action
  command, args = ask_for_action
  case command
  when 'r', 'root'   then get_root
  when 'f', 'follow' then follow_link(collection, *args)
  when      'create' then create_item

  when 'request'     then print_request;    get_next_action
  when 'response'    then print_response;   get_next_action
  when 'body'        then print_body;       get_next_action
  when 'collection'  then print_collection; get_next_action
  when 'template'    then print_template;   get_next_action
  when 'items'       then print_items

  when 'b', 'basic'  then request_basic_authentication(*args)
  when 't', 'token'  then request_token_authentication(*args)

  when 'd', 'debug'  then print_debug; get_next_action
  when '?', 'help'   then print_help; get_next_action
  when 'q', 'quit'   then exit
  else puts 'Try again.'; get_next_action
  end
rescue Interrupt
  print '^C'
  get_next_action
rescue EOFError
  puts
  exit
end

#get_next_item_action(item) ⇒ Object



243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/leif/cli.rb', line 243

def get_next_item_action(item)
  command, args = ask_for_action
  case command
  when 'item'        then print_item(item); get_next_item_action(item)
  when 'update'      then update(item)
  when 'f', 'follow' then follow_link(item, *args)
  when 'cancel'
  when 'q', 'quit'   then exit
  when '?', 'help'   then print_item_help; get_next_item_action(item)
  else puts 'Try again.'; get_next_item_action(item)
  end
rescue Interrupt
  print '^C'
end

#get_rootObject



47
48
49
# File 'lib/leif/cli.rb', line 47

def get_root
  make_request '/'
end

#loggerObject



19
20
21
# File 'lib/leif/cli.rb', line 19

def logger
  @logger ||= Logger.new(debug_output)
end

#make_request(uri, data = {}, method = :unset) ⇒ Object



37
38
39
40
41
# File 'lib/leif/cli.rb', line 37

def make_request(uri, data = {}, method = :unset)
  method = data.empty? ? :get : :post if method == :unset
  reset_debug_output
  @response = conn.send(method, uri, data)
end


121
122
123
124
125
# File 'lib/leif/cli.rb', line 121

def print_body
  banner 'Body' do |out|
    out.print JSON.pretty_generate(@response.body).lines
  end
end


135
136
137
138
139
# File 'lib/leif/cli.rb', line 135

def print_collection
  banner 'Collection' do |out|
    out.print JSON.pretty_generate(collection).lines
  end
end


174
175
176
177
178
179
# File 'lib/leif/cli.rb', line 174

def print_debug
  banner 'Debug' do |out|
    debug_output.rewind
    out.print debug_output.readlines
  end
end


181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/leif/cli.rb', line 181

def print_help
  banner 'Help' do |out|
    out.print <<EOS.lines
root:
  Go back to the root.

follow <rel>:
  Follow link with the relation <rel> on the collection.

create:
  Begin editing the template to create a new item.

request:
  Reprint the details of the last request.

response:
  Reprint the details of the last response.

template:
  Print the template from the last response.

items:
  Print each item from the last response one at a time in order to update,
  delete, or follow an item's link.

basic [<username> [<password>]]:
  Authenticate with HTTP Basic and reload the current resource. Will be
  prompted for username and password if omitted.

token <token>:
  Authenticate using the given token and reload the current resource.

debug:
  Print debug output from the previous HTTP request and response.

quit:
  Exit leif.
EOS
  end
end


155
156
157
158
159
# File 'lib/leif/cli.rb', line 155

def print_item(item)
  banner 'Item' do |out|
    out.print JSON.pretty_generate(item).lines
  end
end


222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/leif/cli.rb', line 222

def print_item_help
  banner 'Help' do |out|
    out.print <<EOS.lines
item:
  Print the selected item.

cancel:
  Cancel item selection and go back to the collection.

follow <rel>:
  Follow link with the relation <rel> on the selected item.

update:
  Begin editing the template to update the selected item.

quit:
  Exit leif.
EOS
  end
end


147
148
149
150
151
152
153
# File 'lib/leif/cli.rb', line 147

def print_items
  item = select_item
  print_links item
  get_next_item_action item
rescue Interrupt
  print '^C'
end


127
128
129
130
131
132
133
# File 'lib/leif/cli.rb', line 127

def print_links(subject)
  banner 'Links' do |out|
    unless subject.link_relations.empty?
      out.print subject.link_relations.join(', ')
    end
  end
end


55
56
57
58
59
60
# File 'lib/leif/cli.rb', line 55

def print_overview
  print_request
  print_response
  print_body
  print_links collection
end


104
105
106
107
108
109
110
111
# File 'lib/leif/cli.rb', line 104

def print_request
  banner 'Request' do |out|
    out.print "#{@response.env[:method].upcase} #{@response.env[:url]}"
    out.print @response.env[:request_headers].map {|header, value|
      "#{header}: #{value}"
    }
  end
end


113
114
115
116
117
118
119
# File 'lib/leif/cli.rb', line 113

def print_response
  banner 'Response' do |out|
    out.print @response.headers.map {|header, value|
      "#{header}: #{value}"
    }
  end
end


141
142
143
144
145
# File 'lib/leif/cli.rb', line 141

def print_template(template = collection.template)
  banner 'Template' do |out|
    out.print JSON.pretty_generate(template).lines
  end
end

#request_basic_authentication(username = :ask, password = :ask) ⇒ Object



62
63
64
65
66
67
# File 'lib/leif/cli.rb', line 62

def request_basic_authentication(username = :ask, password = :ask)
  username = ask('Username: ')                     if username == :ask
  password = ask('Password: ') {|q| q.echo = '*' } if password == :ask
  conn.basic_auth username, password
  retry_request
end

#request_token_authentication(token = '2x033S09401z300E') ⇒ Object



69
70
71
72
# File 'lib/leif/cli.rb', line 69

def request_token_authentication(token = '2x033S09401z300E')
  conn.headers['Authorization'] = "Token token=#{token.inspect}"
  retry_request
end

#reset_debug_outputObject



32
33
34
35
# File 'lib/leif/cli.rb', line 32

def reset_debug_output
  debug_output.rewind
  debug_output.truncate 0
end

#retry_requestObject



51
52
53
# File 'lib/leif/cli.rb', line 51

def retry_request
  make_request @response.env[:url].request_uri
end

#select_itemObject



161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/leif/cli.rb', line 161

def select_item
  collection.items.find do |item|
    print_item item
    puts
    response = ask('Select this item [y,n]? ') do |q|
      q.character = true
      q.validate  = /\A[yn]\Z/
    end

    response == 'y'
  end
end

#update(item) ⇒ Object



99
100
101
102
# File 'lib/leif/cli.rb', line 99

def update(item)
  fill_and_submit_template collection.item_template(item),
                           'Fill the template to update the item.'
end