Sha256: b502a1463ebbaef52c5a5b171abc7f3f0c286dd1a3bb851524291945e0fa3f55

Contents?: true

Size: 1.93 KB

Versions: 6

Compression:

Stored size: 1.93 KB

Contents

module Twurl
  class RCFile
    FILE = '.twurlrc'
    @directory ||= ENV['HOME']
    class << self
      attr_accessor :directory

      def file_path
        File.join(directory, FILE)
      end

      def load
        YAML.load_file(file_path)
      rescue Errno::ENOENT
        default_rcfile_structure
      end

      def default_rcfile_structure
        {'profiles' => {}, 'configuration' => {}}
      end
    end

    attr_reader :data
    def initialize
      @data = self.class.load
    end

    def empty?
      data == self.class.default_rcfile_structure
    end

    def save
      File.open(self.class.file_path, 'w') do |rcfile|
        rcfile.write data.to_yaml
      end
    end

    def [](username)
      profiles[username]
    end

    def profiles
      data['profiles']
    end

    def default_profile
      configuration['default_profile']
    end

    def default_profile=(profile)
      configuration['default_profile'] = [profile.username, profile.consumer_key]
    end

    def configuration
      data['configuration']
    end

    def alias(name, path)
      data['aliases'] ||= {}
      data['aliases'][name] = path
      save
    end

    def aliases
      data['aliases']
    end

    def alias_from_options(options)
      options.subcommands.each do |potential_alias|
        if path = alias_from_name(potential_alias)
          break path
        end
      end
    end

    def alias_from_name(name)
      aliases[name]
    end

    def has_oauth_profile_for_username_with_consumer_key?(username, consumer_key)
      user_profiles = self[username]
      !user_profiles.nil? && !user_profiles[consumer_key].nil?
    end

    def <<(oauth_client)
      client_from_file = self[oauth_client.username] || {}
      client_from_file[oauth_client.consumer_key] = oauth_client.to_hash
      (profiles[oauth_client.username] ||= {}).update(client_from_file)
      self.default_profile = oauth_client unless default_profile
      save
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
twurl-0.6.5 lib/twurl/rcfile.rb
twurl-0.6.4 lib/twurl/rcfile.rb
twurl-0.6.3 lib/twurl/rcfile.rb
twurl-0.6.2 lib/twurl/rcfile.rb
twurl-0.6.1 lib/twurl/rcfile.rb
twurl-0.6.0 lib/twurl/rcfile.rb