Class: Leif::Cli
- Inherits:
-
Object
- Object
- Leif::Cli
- Defined in:
- lib/leif/cli.rb
Instance Method Summary collapse
- #ask_for_action ⇒ Object
- #banner(banner, &message) ⇒ Object
- #collection ⇒ Object
- #conn ⇒ Object
- #convert_to_primitive(value) ⇒ Object
- #create_item ⇒ Object
- #debug_output ⇒ Object
- #fill_and_submit_template(template, label) ⇒ Object
- #follow_link(subject, relation = :ask) ⇒ Object
- #get_next_action ⇒ Object
- #get_next_item_action(item) ⇒ Object
- #get_root ⇒ Object
- #logger ⇒ Object
- #make_request(uri, data = {}, method = :unset) ⇒ Object
- #print_body ⇒ Object
- #print_collection ⇒ Object
- #print_debug ⇒ Object
- #print_help ⇒ Object
- #print_item(item) ⇒ Object
- #print_item_help ⇒ Object
- #print_items ⇒ Object
- #print_links(subject) ⇒ Object
- #print_overview ⇒ Object
- #print_request ⇒ Object
- #print_response ⇒ Object
- #print_template(template = collection.template) ⇒ Object
- #request_basic_authentication(username = :ask, password = :ask) ⇒ Object
- #request_token_authentication(token = '2x033S09401z300E') ⇒ Object
- #reset_debug_output ⇒ Object
- #retry_request ⇒ Object
- #select_item ⇒ Object
- #update(item) ⇒ Object
Instance Method Details
#ask_for_action ⇒ Object
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 |
#banner(banner, &message) ⇒ Object
27 28 29 30 |
# File 'lib/leif/cli.rb', line 27 def (, &) puts Section.(, &) end |
#collection ⇒ Object
43 44 45 |
# File 'lib/leif/cli.rb', line 43 def collection Leif::CollectionJson::Collection.new(@response.body) end |
#conn ⇒ Object
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_item ⇒ Object
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_output ⇒ Object
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 |
#follow_link(subject, relation = :ask) ⇒ Object
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_action ⇒ Object
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_root ⇒ Object
47 48 49 |
# File 'lib/leif/cli.rb', line 47 def get_root make_request '/' end |
#logger ⇒ Object
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 |
#print_body ⇒ Object
121 122 123 124 125 |
# File 'lib/leif/cli.rb', line 121 def print_body 'Body' do |out| out.print JSON.pretty_generate(@response.body).lines end end |
#print_collection ⇒ Object
135 136 137 138 139 |
# File 'lib/leif/cli.rb', line 135 def print_collection 'Collection' do |out| out.print JSON.pretty_generate(collection).lines end end |
#print_debug ⇒ Object
174 175 176 177 178 179 |
# File 'lib/leif/cli.rb', line 174 def print_debug 'Debug' do |out| debug_output.rewind out.print debug_output.readlines end end |
#print_help ⇒ Object
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 '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 |
#print_item(item) ⇒ Object
155 156 157 158 159 |
# File 'lib/leif/cli.rb', line 155 def print_item(item) 'Item' do |out| out.print JSON.pretty_generate(item).lines end end |
#print_item_help ⇒ Object
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 '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 |
#print_items ⇒ Object
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 |
#print_links(subject) ⇒ Object
127 128 129 130 131 132 133 |
# File 'lib/leif/cli.rb', line 127 def print_links(subject) 'Links' do |out| unless subject.link_relations.empty? out.print subject.link_relations.join(', ') end end end |
#print_overview ⇒ Object
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 |
#print_request ⇒ Object
104 105 106 107 108 109 110 111 |
# File 'lib/leif/cli.rb', line 104 def print_request '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 |
#print_response ⇒ Object
113 114 115 116 117 118 119 |
# File 'lib/leif/cli.rb', line 113 def print_response 'Response' do |out| out.print @response.headers.map {|header, value| "#{header}: #{value}" } end end |
#print_template(template = collection.template) ⇒ Object
141 142 143 144 145 |
# File 'lib/leif/cli.rb', line 141 def print_template(template = collection.template) '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_output ⇒ Object
32 33 34 35 |
# File 'lib/leif/cli.rb', line 32 def reset_debug_output debug_output.rewind debug_output.truncate 0 end |
#retry_request ⇒ Object
51 52 53 |
# File 'lib/leif/cli.rb', line 51 def retry_request make_request @response.env[:url].request_uri end |
#select_item ⇒ Object
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 |