Module: ApiKeys::Helpers::ViewHelpers

Defined in:
lib/api_keys/helpers/view_helpers.rb

Overview

View helpers for displaying API key information.

These helpers provide formatted data without HTML opinions, allowing integrators to build their own UI while using consistent data formatting.

Examples:

Include in your ApplicationHelper

module ApplicationHelper
  include ApiKeys::Helpers::ViewHelpers
end

Or include in a specific controller

class Settings::ApiKeysController < ApplicationController
  helper ApiKeys::Helpers::ViewHelpers
end

Instance Method Summary collapse

Instance Method Details

#api_key_environment_from_token(token) ⇒ Symbol?

Detects the environment from a token string by parsing its prefix. Useful for displaying environment info when you only have the token.

Examples:

api_key_environment_from_token("sk_test_abc123") # => :test
api_key_environment_from_token("pk_live_xyz789") # => :live
api_key_environment_from_token("ak_abc123")      # => nil

Parameters:

  • token (String)

    The full token string (e.g., "sk_test_abc123...")

Returns:

  • (Symbol, nil)

    The detected environment (:test, :live, etc.) or nil



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/api_keys/helpers/view_helpers.rb', line 120

def api_key_environment_from_token(token)
  return nil if token.blank?
  return nil if token.length > 500 # Reasonable max length for tokens

  config = ApiKeys.configuration
  return nil unless config.environments.present?

  # Check each configured environment's prefix segment
  config.environments.each do |env_name, env_config|
    segment = env_config[:prefix_segment]
    next if segment.blank?

    # Match pattern like _test_ or _live_ in the token
    if token.include?("_#{segment}_")
      return env_name
    end
  end

  nil
end

#api_key_environment_label(api_key) ⇒ String

Returns the environment label for an API key.

Examples:

api_key_environment_label(@key) # => "Live"

Parameters:

Returns:

  • (String)

    "Test", "Live", or "Default" (if no environment set)



61
62
63
64
65
# File 'lib/api_keys/helpers/view_helpers.rb', line 61

def api_key_environment_label(api_key)
  return "Default" if api_key.environment.blank?

  api_key.environment.to_s.capitalize
end

#api_key_environment_label_from_token(token) ⇒ String

Returns a human-readable environment label from a token string. Convenience wrapper around api_key_environment_from_token.

Examples:

api_key_environment_label_from_token("sk_test_abc") # => "Test mode"
api_key_environment_label_from_token("sk_live_xyz") # => "Live mode"

Parameters:

  • token (String)

    The full token string

Returns:

  • (String)

    "Test mode", "Live mode", or "Default" if unknown



151
152
153
154
155
156
# File 'lib/api_keys/helpers/view_helpers.rb', line 151

def api_key_environment_label_from_token(token)
  env = api_key_environment_from_token(token)
  return "Default" if env.nil?

  "#{env.to_s.capitalize} mode"
end

#api_key_publishable?(api_key) ⇒ Boolean

Returns whether the key is a publishable (public) key type. Useful for conditional rendering (e.g., showing/hiding copy button).

Examples:

<% if api_key_publishable?(@key) %>
  <%= button_tag "Copy", data: { token: @key.viewable_token } %>
<% end %>

Parameters:

Returns:

  • (Boolean)

    true if the key is publishable



96
97
98
# File 'lib/api_keys/helpers/view_helpers.rb', line 96

def api_key_publishable?(api_key)
  api_key.key_type.to_s == "publishable"
end

#api_key_secret?(api_key) ⇒ Boolean

Returns whether the key is a secret key type.

Parameters:

Returns:

  • (Boolean)

    true if the key is secret (or legacy with no type)



105
106
107
# File 'lib/api_keys/helpers/view_helpers.rb', line 105

def api_key_secret?(api_key)
  api_key.key_type.blank? || api_key.key_type.to_s == "secret"
end

#api_key_status(api_key) ⇒ Symbol

Returns the status of an API key as a symbol.

Examples:

api_key_status(@key) # => :active

Parameters:

Returns:

  • (Symbol)

    :active, :expired, or :revoked



30
31
32
33
34
35
# File 'lib/api_keys/helpers/view_helpers.rb', line 30

def api_key_status(api_key)
  return :revoked if api_key.revoked?
  return :expired if api_key.expired?

  :active
end

#api_key_status_info(api_key) ⇒ Hash

Returns a hash of status information for an API key. Useful for building custom status badges.

Examples:

info = api_key_status_info(@key)
# => { status: :active, label: "Active", color: :green }

Parameters:

Returns:

  • (Hash)

    Hash with :status, :label, and :color keys



168
169
170
171
172
173
174
175
# File 'lib/api_keys/helpers/view_helpers.rb', line 168

def api_key_status_info(api_key)
  status = api_key_status(api_key)
  {
    status: status,
    label: api_key_status_label(api_key),
    color: status_color(status)
  }
end

#api_key_status_label(api_key) ⇒ String

Returns a human-readable status label for an API key.

Examples:

api_key_status_label(@key) # => "Active"

Parameters:

Returns:

  • (String)

    "Active", "Expired", or "Revoked"



45
46
47
48
49
50
51
# File 'lib/api_keys/helpers/view_helpers.rb', line 45

def api_key_status_label(api_key)
  case api_key_status(api_key)
  when :active then "Active"
  when :expired then "Expired"
  when :revoked then "Revoked"
  end
end

#api_key_type_info(api_key) ⇒ Hash

Returns a hash of type information for an API key. Useful for building custom type badges.

Examples:

info = api_key_type_info(@key)
# => { type: :publishable, label: "Publishable", color: :green }

Parameters:

Returns:

  • (Hash)

    Hash with :type, :label, and :color keys



187
188
189
190
191
192
193
194
# File 'lib/api_keys/helpers/view_helpers.rb', line 187

def api_key_type_info(api_key)
  type = api_key.key_type.presence&.to_sym || :secret
  {
    type: type,
    label: api_key_type_label(api_key),
    color: type_color(type)
  }
end

#api_key_type_label(api_key) ⇒ String

Returns the key type label for an API key.

Examples:

api_key_type_label(@key) # => "Secret"

Parameters:

Returns:

  • (String)

    "Publishable", "Secret", or the key_type capitalized



75
76
77
78
79
80
81
82
83
# File 'lib/api_keys/helpers/view_helpers.rb', line 75

def api_key_type_label(api_key)
  return "Secret" if api_key.key_type.blank?

  case api_key.key_type.to_s
  when "publishable" then "Publishable"
  when "secret" then "Secret"
  else api_key.key_type.to_s.capitalize
  end
end