Class: BunchURLGenerator

Inherits:
Object
  • Object
show all
Includes:
Prompt, Util
Defined in:
lib/bunch/url_generator.rb

Instance Method Summary collapse

Methods included from Util

#bundle_id

Methods included from Prompt

#choose_number, #get_line, #get_text, #url_encode_text, #yn

Instance Method Details

#generateObject



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/bunch/url_generator.rb', line 287

def generate
  menu_items = [
    MenuItem.new('open', 'Open a Bunch', 'open'),
    MenuItem.new('close', 'Close a Bunch', 'close'),
    MenuItem.new('toggle', 'Toggle a Bunch', 'toggle'),
    MenuItem.new('snippet', 'Load a Snippet', 'snippet'),
    MenuItem.new('raw', 'Load raw text', 'raw')
  ]

  menu = Menu.new(menu_items)
  finder = BunchFinder.new

  selection = menu.choose
  Process.exit 0 unless selection
  url = "x-bunch://#{selection.value}"
  parameters = []
  case selection.id
  when /(open|close|toggle)/
    parameters << ['bunch', CGI.escape(finder.choose_bunch.value)]
  when /snippet/
    filename = finder.choose_snippet.value
    parameters << ['file', filename]
    filename = finder.expand_path(filename)
    snippet = Snippet.new(filename)
    fragment = snippet.choose_fragment
    if fragment
      parameters << ['fragment', CGI.escape(fragment.title)]
      contents = fragment.value
    else
      contents = snippet.contents
    end
    variables = finder.fill_variables(contents)
    parameters.concat(variables) if variables.length
  when /raw/
    parameters << ['text', menu.url_encode_text]
  else
    Process.exit 0
  end

  menu.items = [
    MenuItem.new('app', 'Application', 'find_bid'),
    MenuItem.new('url', 'URL', 'get_line(')
  ]

  selection = menu.choose('Add success action? (Enter to skip)')

  if selection
    case selection.id
    when /app/
      app = get_line('Application name')
      value = bundle_id(app)
    when /url/
      value = get_line('URL')
    end

    parameters << ['x-success', value] if value

    delay = get_line('Delay for success action')
    parameters << ['x-delay', delay.to_s] if delay =~ /^\d+$/
  end

  query_string = parameters.map { |param| "#{param[0]}=#{param[1].gsub(/\+/, '%20')}" }.join('&')
  full_url = "#{url}?#{query_string}".strip

  res = yn('Copy URL to clipboard')
  if res
    `echo '#{full_url}'|tr -d '\n'|pbcopy`
    warn 'Copied to clipboard'
  else
    puts full_url
  end
end