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.
Instance Method Summary collapse
-
#api_key_environment_from_token(token) ⇒ Symbol?
Detects the environment from a token string by parsing its prefix.
-
#api_key_environment_label(api_key) ⇒ String
Returns the environment label for an API key.
-
#api_key_environment_label_from_token(token) ⇒ String
Returns a human-readable environment label from a token string.
-
#api_key_publishable?(api_key) ⇒ Boolean
Returns whether the key is a publishable (public) key type.
-
#api_key_secret?(api_key) ⇒ Boolean
Returns whether the key is a secret key type.
-
#api_key_status(api_key) ⇒ Symbol
Returns the status of an API key as a symbol.
-
#api_key_status_info(api_key) ⇒ Hash
Returns a hash of status information for an API key.
-
#api_key_status_label(api_key) ⇒ String
Returns a human-readable status label for an API key.
-
#api_key_type_info(api_key) ⇒ Hash
Returns a hash of type information for an API key.
-
#api_key_type_label(api_key) ⇒ String
Returns the key type label for an API key.
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.
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.
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.
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).
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.
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.
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.
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.
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.
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.
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 |