Sha256: f202e9c190c1573169d81b13f8352d8448385078f81771e8a508cab5a3bbeccc

Contents?: true

Size: 1.27 KB

Versions: 17

Compression:

Stored size: 1.27 KB

Contents

require 'osx_keychain'

module CocoaPodsKeys
  class Keyring
    attr_accessor :keys, :path, :name

    def initialize(name, path, keys = [])
      @name = name.to_s
      @path = path.to_s
      @keys = keys
    end

    def self.from_hash(hash)
      new(hash['name'], hash['path'], hash['keys'])
    end

    def to_hash
      { 'keys' => @keys, 'path' => @path, 'name' => @name }
    end

    def code_name
      name.split(/[^a-zA-Z0-9_]/).map { |s| s[0].upcase + s[1..-1] }.join('')
    end

    def self.keychain_prefix
      'cocoapods-keys-'
    end

    def keychain
      @keychain ||= OSXKeychain.new
    end

    def save(key, value)
      keychain[self.class.keychain_prefix + name, key] = value
    end

    def keychain_data
      Hash[
        @keys.map { |key| [key, keychain_value(key)] }
      ]
    end

    def keychain_has_key?(key)
      has_key = !keychain_value(key).nil?

      if has_key && !@keys.include?(key)
        @keys << key
      elsif !has_key && @keys.include?(key)
        @keys.delete(key)
      end

      has_key
    end

    def keychain_value(key)
      ENV[key] || keychain[self.class.keychain_prefix + name, key]
    end

    def camel_cased_keys
      Hash[keychain_data.map { |(key, value)| [key[0].downcase + key[1..-1], value] }]
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
cocoapods-keys-2.2.1 lib/keyring.rb
cocoapods-keys-2.2.0 lib/keyring.rb
cocoapods-keys-2.1.0 lib/keyring.rb
cocoapods-keys-2.0.7 lib/keyring.rb
cocoapods-keys-2.0.6 lib/keyring.rb
cocoapods-keys-2.0.5 lib/keyring.rb
cocoapods-keys-2.0.4 lib/keyring.rb
cocoapods-keys-2.0.3 lib/keyring.rb
cocoapods-keys-2.0.2 lib/keyring.rb
cocoapods-keys-2.0.1 lib/keyring.rb
cocoapods-keys-2.0.0 lib/keyring.rb
cocoapods-keys-1.7.0 lib/keyring.rb
cocoapods-keys-1.6.1 lib/keyring.rb
cocoapods-keys-1.6.0 lib/keyring.rb
cocoapods-keys-1.5.2 lib/keyring.rb
cocoapods-keys-1.5.1 lib/keyring.rb
cocoapods-keys-1.5.0 lib/keyring.rb