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.



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

def initialize(simplepass)
  @database = simplepass
end

Instance Method Details

#add(text) ⇒ Object



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

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



349
350
351
352
353
354
355
# File 'lib/simplepass.rb', line 349

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



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

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

#listObject



357
358
359
# File 'lib/simplepass.rb', line 357

def list
  @database.keys.sort
end

#new_entry(domain) ⇒ Object



385
386
387
388
389
390
391
392
# File 'lib/simplepass.rb', line 385

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

#remove(domain) ⇒ Object



394
395
396
397
398
399
400
401
# File 'lib/simplepass.rb', line 394

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



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

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