Sha256: 1a4408755c24e755cc685b295f3dd85d6758ac7d8063e5dd80cebb1b24c9b412

Contents?: true

Size: 1.27 KB

Versions: 2

Compression:

Stored size: 1.27 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

    def initialize(keyfile = File.expand_path('~/.pingfm_keys.yml'))
      @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

2 entries across 2 versions & 2 rubygems

Version Path
Oshuma-pingfm-1.0.0 lib/pingfm/keyloader.rb
pingfm-1.0.0 lib/pingfm/keyloader.rb