class Pairity

Constants

VERSION

Attributes

name[RW]

Public Class Methods

home() click to toggle source
# File lib/pairity.rb, line 16
def self.home
  ENV['HOME'] || '/tmp'
end
new(name) click to toggle source
# File lib/pairity.rb, line 54
def initialize(name)
  self.name = name.upcase
end
root() click to toggle source
# File lib/pairity.rb, line 20
def self.root
  @root ||= File.join(home, '.pairity')
end
root=(path) click to toggle source
# File lib/pairity.rb, line 24
def self.root=(path)
  @root = path
end
with_key_paths(*key_names, &blk) click to toggle source
# File lib/pairity.rb, line 28
def self.with_key_paths(*key_names, &blk)
  keys = key_names.map { |name| new(name) }

  paths = keys.map(&:path)

  result = blk.call *paths

  keys.map(&:unlink_temp_file)

  result
end
with_keys(*key_names, &blk) click to toggle source
# File lib/pairity.rb, line 40
def self.with_keys(*key_names, &blk)
  keys = key_names.map { |name| new(name) }

  contents = keys.map(&:content)

  result = blk.call *contents

  keys.map(&:unlink_temp_file)

  result
end

Public Instance Methods

content() click to toggle source
# File lib/pairity.rb, line 81
def content
  File.read path
end
default_path() click to toggle source
# File lib/pairity.rb, line 98
def default_path
  File.join(self.class.root, "#{friendly_name}.key")
end
env() click to toggle source
# File lib/pairity.rb, line 62
def env
  ENV[name]
end
friendly_name() click to toggle source
# File lib/pairity.rb, line 58
def friendly_name
  name.downcase.gsub('_', '-')
end
path() click to toggle source
# File lib/pairity.rb, line 69
def path
  if File.exist?(default_path)
    default_path
  elsif path_env
    path_env
  elsif env
    temp_path
  else
    raise KeyNotConfigured, name
  end
end
path_env() click to toggle source
# File lib/pairity.rb, line 65
def path_env
  ENV["#{name}_PATH"]
end
temp_file() click to toggle source
# File lib/pairity.rb, line 89
def temp_file
  return @temp_file if @temp_file && File.exist?(@temp_file)

  file = Tempfile.new 'temp-key'
  file.write env
  file.close
  @temp_path = file
end
temp_path() click to toggle source
# File lib/pairity.rb, line 85
def temp_path
  temp_file.path
end