Class: Leif::Cli

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

Constant Summary collapse

DEFAULT_ENDPOINT =
'https://api.getcloudapp.com'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint, connection_options = {}) ⇒ Cli

Returns a new instance of Cli.



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

def initialize(endpoint, connection_options = {})
  @endpoint           = endpoint || DEFAULT_ENDPOINT
  @connection_options = connection_options
  @connection         = Connection.to_url(self.endpoint,
                                          self.connection_options)
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



8
9
10
# File 'lib/leif/cli.rb', line 8

def connection
  @connection
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



8
9
10
# File 'lib/leif/cli.rb', line 8

def endpoint
  @endpoint
end

#last_exchangeObject (readonly)

Returns the value of attribute last_exchange.



8
9
10
# File 'lib/leif/cli.rb', line 8

def last_exchange
  @last_exchange
end

Instance Method Details

#ask_for_actionObject



276
277
278
279
280
# File 'lib/leif/cli.rb', line 276

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


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

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

#basic_auth(username, password) ⇒ Object



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

def basic_auth(username, password)
  @connection = Connection.to_url(endpoint,
                                  connection_options(username: username,
                                                     password: password))
end

#collectionObject



41
42
43
# File 'lib/leif/cli.rb', line 41

def collection
  Leif::CollectionJson::Collection.new(last_exchange.response_body)
end

#connection_options(options = {}) ⇒ Object



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

def connection_options(options = {})
  @connection_options.merge(options)
end

#convert_to_primitive(value) ⇒ Object



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/leif/cli.rb', line 282

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



92
93
94
95
# File 'lib/leif/cli.rb', line 92

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

#fill_and_submit_template(template, label) ⇒ Object



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

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

  request template.href, template.convert_to_json, template.method
end


72
73
74
75
# File 'lib/leif/cli.rb', line 72

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

#get_next_actionObject



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/leif/cli.rb', line 246

def get_next_action
  command, args = ask_for_action
  case command
  when      'root'   then get_root
  when      'reload' then reload
  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 set_basic_auth(*args)
  when 't', 'token'  then set_token_auth(*args)

  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



231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/leif/cli.rb', line 231

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



45
46
47
# File 'lib/leif/cli.rb', line 45

def get_root
  request '/'
end


119
120
121
122
123
# File 'lib/leif/cli.rb', line 119

def print_body
  banner 'Body' do |out|
    out.print JSON.pretty_generate(last_exchange.response_body).lines
  end
end


133
134
135
136
137
# File 'lib/leif/cli.rb', line 133

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


172
173
174
175
176
177
178
179
180
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
# File 'lib/leif/cli.rb', line 172

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.

quit:
  Exit leif.
EOS
  end
end


153
154
155
156
157
# File 'lib/leif/cli.rb', line 153

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


210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/leif/cli.rb', line 210

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


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

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


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

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


53
54
55
56
57
58
# File 'lib/leif/cli.rb', line 53

def print_overview
  print_request
  print_response
  print_body
  print_links collection
end


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

def print_request
  banner 'Request' do |out|
    out.print "#{last_exchange.method} #{last_exchange.uri}"
    out.print last_exchange.request_headers.map {|header, value|
      "#{header}: #{value}"
    }
  end
end


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

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


139
140
141
142
143
# File 'lib/leif/cli.rb', line 139

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

#reloadObject



49
50
51
# File 'lib/leif/cli.rb', line 49

def reload
  request last_exchange.uri
end

#request(*args) ⇒ Object



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

def request(*args)
  @last_exchange = connection.request(*args)
end

#select_itemObject



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

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

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



60
61
62
63
64
65
# File 'lib/leif/cli.rb', line 60

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

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



67
68
69
70
# File 'lib/leif/cli.rb', line 67

def set_token_auth(token = '2x033S09401z300E')
  token_auth token
  reload
end

#token_auth(token) ⇒ Object



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

def token_auth(token)
  @connection = Connection.to_url(endpoint,
                                  connection_options(token: token))
end

#update(item) ⇒ Object



97
98
99
100
# File 'lib/leif/cli.rb', line 97

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