Class: SimplePass::UI

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

Constant Summary collapse

EDIT_HEADER =
"Fill the the fields below. Leave the first line, with the name of the domain,
untouched. The notes field can accept multiple lines, including blank lines.\n" + ('-' * 20 + "\n")

Instance Method Summary collapse

Constructor Details

#initialize(simplepass) ⇒ UI

Returns a new instance of UI.



328
329
330
# File 'lib/simplepass.rb', line 328

def initialize(simplepass)
  @database = simplepass
end

Instance Method Details

#add(text) ⇒ Object



348
349
350
351
352
353
354
355
356
357
358
359
# File 'lib/simplepass.rb', line 348

def add(text)
  entry = {}
  # strip off header text, which is just for instructions
  text = strip_header(text)
  # split text into lines
  # first line is the domain
  domain = text.split("\n")[0].chomp
   = text.match(/^login:(.*)/)[1].strip
  password = text.match(/^password:(.*)/)[1].strip
  notes = text.split(/^notes:\s*/, 2)[1].strip
  @database[domain] = {:login => , :password => password, :notes => notes}
end

#decrypt(domain) ⇒ Object



332
333
334
335
336
337
338
# File 'lib/simplepass.rb', line 332

def decrypt(domain)
  matching_key = @database.keys.detect {|k| k == domain}
  return nil unless matching_key
  # format the YAML to remove weird symbol markup
  result = @database[matching_key]
  formatted_result = "#{matching_key}\nlogin: #{result[:login]}\npassword: #{result[:password]}\nnotes: #{result[:notes]}\n"
end

#dump(domain = nil) ⇒ Object



361
362
363
364
365
366
# File 'lib/simplepass.rb', line 361

def dump(domain=nil)
  unless domain
    return @database.to_yaml
  end
  @database[domain].to_yaml
end

#listObject



340
341
342
# File 'lib/simplepass.rb', line 340

def list
  @database.keys.sort
end

#new_entry(domain) ⇒ Object



368
369
370
371
372
373
374
375
# File 'lib/simplepass.rb', line 368

def new_entry(domain)
  return <<END
#{domain}
login:  
password:  
notes:  
END
end

#remove(domain) ⇒ Object



377
378
379
380
381
382
383
384
# File 'lib/simplepass.rb', line 377

def remove(domain)
  unless decrypt(domain)
    return "There is no entry for #{domain} in the database."
  end
  @database.delete(domain)
  @database.save!
  puts = "#{domain} deleted."
end

#strip_header(text) ⇒ Object



344
345
346
# File 'lib/simplepass.rb', line 344

def strip_header(text)
  text.split(('-' * 20) + "\n", 2)[-1]
end