Sha256: 6e914097a4b14d3668559df6e5d70fe2adc544faff8995c53faf7bd02800eebc

Contents?: true

Size: 1.63 KB

Versions: 41

Compression:

Stored size: 1.63 KB

Contents

require 'uuidtools'

module Vault
  module User
    ID_CAPTURE = /^user(\d+)\@[\w\.]+com$/
    # Convert a user ID into a Heroku user ID.
    #
    # @param user_id [Fixnum] A user ID.
    # @return [String] A Heroku ID that uniquely represents the user.
    def self.id_to_hid(user_id)
      "user#{user_id}@heroku.com"
    end

    # Convert a user ID into a v5 UUID.
    #
    # @param user_id [Fixnum] A user ID.
    # @return [String] A v5 UUID that uniquely represents the user.
    def self.id_to_uuid(user_id)
      url = "https://vault.heroku.com/users/#{user_id}"
      UUIDTools::UUID.sha1_create(UUIDTools::UUID_URL_NAMESPACE, url).to_s
    end

    # Convert a Heroku user ID into a core user ID.
    #
    # @param heroku_id [String] A Heroku user ID, such as
    #   `user1234@heroku.com`.
    # @raise [ArgumentError] Raised if a malformed Heroku ID is provided.
    # @return [Fixnum] The core user ID that uniquely represents the user.
    def self.hid_to_id(heroku_id)
      if user_id = heroku_id.slice(ID_CAPTURE, 1)
        user_id.to_i
      else
        raise ArgumentError,"#{heroku_id} is not a valid Heroku user ID."
      end
    end

    # Convert a Heroku user ID into a v5 UUID.
    #
    # @param heroku_id [String] A Heroku user ID, such as
    #   `user1234@heroku.com`.
    # @raise [ArgumentError] Raised if a malformed Heroku ID is provided.
    # @return [String] A v5 UUID that uniquely represents the user.
    def self.hid_to_uuid(heroku_id)
      if user_id = heroku_id.slice(ID_CAPTURE, 1)
        id_to_uuid(user_id)
      else
        raise ArgumentError,"#{heroku_id} is not a valid Heroku user ID."
      end
    end
  end
end

Version data entries

41 entries across 41 versions & 1 rubygems

Version Path
vault-tools-1.0.1 lib/vault-tools/user.rb
vault-tools-1.0.0 lib/vault-tools/user.rb
vault-tools-0.7.1 lib/vault-tools/user.rb
vault-tools-0.7.0 lib/vault-tools/user.rb
vault-tools-0.6.5 lib/vault-tools/user.rb
vault-tools-0.6.4 lib/vault-tools/user.rb
vault-tools-0.6.3 lib/vault-tools/user.rb
vault-tools-0.6.2 lib/vault-tools/user.rb
vault-tools-0.6.1 lib/vault-tools/user.rb
vault-tools-0.6.0 lib/vault-tools/user.rb
vault-tools-0.5.22 lib/vault-tools/user.rb
vault-tools-0.5.21 lib/vault-tools/user.rb
vault-tools-0.5.19 lib/vault-tools/user.rb
vault-tools-0.5.18 lib/vault-tools/user.rb
vault-tools-0.5.17 lib/vault-tools/user.rb
vault-tools-0.5.16 lib/vault-tools/user.rb
vault-tools-0.5.15 lib/vault-tools/user.rb
vault-tools-0.5.14 lib/vault-tools/user.rb
vault-tools-0.5.13 lib/vault-tools/user.rb
vault-tools-0.5.12 lib/vault-tools/user.rb