Class: BunchFinder

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

Overview

File search functions

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Prompt

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

Constructor Details

#initializeBunchFinder

Returns a new instance of BunchFinder.



207
208
209
210
211
212
213
214
215
216
# File 'lib/bunch/url_generator.rb', line 207

def initialize
  config_dir = `osascript -e 'tell app "#{TARGET_APP}" to get preference "Folder"'`.strip
  config_dir.sub!(%r{^file://}, '')
  config_dir = File.expand_path(config_dir)
  if File.directory?(config_dir)
    @config_dir = config_dir
  else
    throw 'Unable to retrieve Bunches Folder'
  end
end

Instance Attribute Details

#config_dirObject

Returns the value of attribute config_dir.



205
206
207
# File 'lib/bunch/url_generator.rb', line 205

def config_dir
  @config_dir
end

Instance Method Details

#bunches_to_itemsObject



218
219
220
221
222
223
# File 'lib/bunch/url_generator.rb', line 218

def bunches_to_items
  items = []
  bunches = `osascript -e 'tell app "#{TARGET_APP}" to list bunches'`.strip.split(/,/).map(&:strip)
  bunches.sort_by(&:downcase).each { |b| items << MenuItem.new(b, b, b) }
  items
end

#choose_bunchObject



237
238
239
240
241
242
243
244
245
246
# File 'lib/bunch/url_generator.rb', line 237

def choose_bunch
  items = bunches_to_items
  # items.map! do |item|
  #   item.title = File.basename(item.title, '.bunch')
  #   item.value = File.basename(item.title, '.bunch')
  #   item
  # end
  menu = Menu.new(items)
  menu.choose('Select a Bunch')
end

#choose_snippetObject



248
249
250
251
252
# File 'lib/bunch/url_generator.rb', line 248

def choose_snippet
  items = files_to_items(@config_dir, '*')
  menu = Menu.new(items)
  menu.choose('Select a Snippet')
end

#contents(snippet) ⇒ Object



258
259
260
# File 'lib/bunch/url_generator.rb', line 258

def contents(snippet)
  IO.read(File.join(@config_dir, snippet))
end

#expand_path(file) ⇒ Object



254
255
256
# File 'lib/bunch/url_generator.rb', line 254

def expand_path(file)
  File.join(@config_dir, file)
end

#files_to_items(dir, pattern) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
# File 'lib/bunch/url_generator.rb', line 225

def files_to_items(dir, pattern)
  Dir.chdir(dir)
  items = []
  Dir.glob(pattern) do |f|
    if f.text?
      filename = File.basename(f)
      items << MenuItem.new(filename, filename, filename)
    end
  end
  items
end

#fill_variables(text) ⇒ Object



269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/bunch/url_generator.rb', line 269

def fill_variables(text)
  vars = variables(text)
  output = []
  unless vars.empty?
    puts 'Enter values for variables'
    vars.each do |var|
      res = get_line(var)
      output << [var, CGI.escape(res)] unless res.empty?
    end
  end
  output
end

#variables(content) ⇒ Object



262
263
264
265
266
267
# File 'lib/bunch/url_generator.rb', line 262

def variables(content)
  matches = content.scan(/\$\{(\S+)(:.*?)?\}/)
  variables = []
  matches.each { |m| variables << m[0].sub(/:\S+$/, '') }
  variables.uniq
end