Sha256: d88637b7420ba2b10d5ad3a07eaf2af84ce34689a873dd209ecf47ae1fc8c89b

Contents?: true

Size: 1.39 KB

Versions: 4

Compression:

Stored size: 1.39 KB

Contents

require 'yaml'

module Pingfm

  class KeyloadingError < Exception; end

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

    # Path to YAML file containing keys
    attr_accessor :keyfile

    # ping.fm uses this as the key for the registered application
    attr_accessor :api_key

    # ping.fm uses this as the key for the user
    attr_accessor :app_key

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

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

      # load keys on init
      load_keys!
    end

    # load a new set of keys
    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']
        @app_key = data['app_key']
      end
    end

    # load keys using the known keyfile
    def load_keys!
      load_keys(@keyfile)
    end

    # if keys have been loaded successfully
    def has_keys?
      return true unless @api_key.nil? or @app_key.nil?
      return false
    end

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

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
Oshuma-pingfm-1.0.1 lib/pingfm/keyloader.rb
Oshuma-pingfm-1.0.2 lib/pingfm/keyloader.rb
pingfm-1.0.1 lib/pingfm/keyloader.rb
pingfm-1.0.2 lib/pingfm/keyloader.rb