Sha256: 522bc595f85875168a457aaecc13516431e5c64be182cadec063c9a8fe84c883

Contents?: true

Size: 1.14 KB

Versions: 6

Compression:

Stored size: 1.14 KB

Contents

module GetToWork
  KEYCHAIN_PREFIX = "GetToWork".freeze

  class Keychain
    def update(opts = {})
      relative_service_name = opts[:service]
      @absolute_service_name =
        self.class.absolute_service_name(relative_service_name)

      update_or_create_keychain_item(opts)
    end

    def self.find(service:)
      ::Keychain.generic_passwords.where(
        service: absolute_service_name(service)
      ).all
    end

    def self.absolute_service_name(relative_name)
      "#{KEYCHAIN_PREFIX}::#{relative_name}"
    end

    private

    def update_or_create_keychain_item(opts = {})
      keychain_items = self.class.find(service: opts[:service])
      ::Keychain.generic_passwords.where(
        service: @absolute_service_name
      )

      if item = keychain_items.first
        item.account = opts[:account]
        item.password = opts[:password]
      else
        create_keychain_item(opts)
      end
    end

    def create_keychain_item(opts = {})
      relative_service_name = opts[:service]
      opts[:service] = self.class.absolute_service_name(relative_service_name)

      ::Keychain.generic_passwords.create(opts)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
get_to_work-0.1.5 lib/get_to_work/keychain.rb
get_to_work-0.1.4 lib/get_to_work/keychain.rb
get_to_work-0.1.3 lib/get_to_work/keychain.rb
get_to_work-0.1.2 lib/get_to_work/keychain.rb
get_to_work-0.1.1 lib/get_to_work/keychain.rb
get_to_work-0.1.0 lib/get_to_work/keychain.rb