Class: HelloTxt::Keyloader

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

Overview

manages the YAML file containing the keys - encryption might be nice, might be overkill

Constant Summary collapse

KEY_PATH =
(RUBY_PLATFORM =~ /mswin32/ ? ENV['USERPROFILE'] : ENV['HOME'])
KEY_FILE =
'.hellotxt_keys.yml'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keyfile = File.expand_path(File.join(KEY_PATH, KEY_FILE))) ⇒ Keyloader

Returns a new instance of Keyloader.



22
23
24
25
26
27
28
# File 'lib/hellotxt/keyloader.rb', line 22

def initialize(keyfile = File.expand_path(File.join(KEY_PATH, KEY_FILE)))
  @api_key = nil
  @keyfile = keyfile

  # load keys on init
  load_keys!
end

Instance Attribute Details

#api_keyObject

hellotxt uses this as the key for the registered application



14
15
16
# File 'lib/hellotxt/keyloader.rb', line 14

def api_key
  @api_key
end

#keyfileObject

Path to YAML file containing keys



11
12
13
# File 'lib/hellotxt/keyloader.rb', line 11

def keyfile
  @keyfile
end

#user_keyObject

hellotxt uses this as the key for the user



17
18
19
# File 'lib/hellotxt/keyloader.rb', line 17

def user_key
  @user_key
end

Instance Method Details

#has_keys?Boolean

if keys have been loaded successfully

Returns:

  • (Boolean)


46
47
48
49
# File 'lib/hellotxt/keyloader.rb', line 46

def has_keys?
  return true unless @api_key.nil? or @user_key.nil?
  return false
end

#load_keys(keyfile) ⇒ Object

load a new set of keys



31
32
33
34
35
36
37
38
# File 'lib/hellotxt/keyloader.rb', line 31

def load_keys(keyfile)
  if File.exist?(keyfile) and File.readable?(keyfile)
    data = YAML::load_file(keyfile)
    @keyfile = keyfile if @keyfile.nil?
    @api_key = data['api_key']
    @user_key = data['user_key']
  end
end

#load_keys!Object

load keys using the known keyfile



41
42
43
# File 'lib/hellotxt/keyloader.rb', line 41

def load_keys!
  load_keys(@keyfile)
end

#saveObject

save key data to keyfile



52
53
54
55
56
# File 'lib/hellotxt/keyloader.rb', line 52

def save
  File.open( @keyfile, 'w+' ) do |out|
    YAML::dump( {'api_key' => @api_key, 'user_key' => @user_key}, out )
  end
end