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

Instance Method Details

#generateObject



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
275
276
277
278
279
280
281
282
283
284
285
286
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
# File 'lib/bunch/url_generator.rb', line 249

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]}" }.join('&')

  puts url + '?' + query_string
end