Module: Card::Set::Right::ApiKey

Extended by:
Card::Set
Defined in:
card/tmpsets/set/mod011-card-mod-account/right/api_key.rb,
card/tmpsets/set/mod019-card-mod-api_key/right/api_key.rb

Overview

Set: All “+ApiKey” cards

Defined Under Namespace

Modules: HtmlFormat, JsonFormat

Constant Summary

Constants included from Helpers

Helpers::SET_PATTERN_TEST_REGEXP

Constants included from Event::Api

Event::Api::OPTIONS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Card::Set

reset_modules

Methods included from I18nScope

#mod_name, #scope

Methods included from Loader

#clean_empty_module_from_hash, #clean_empty_modules, #extended, #process_base_modules, #register_set

Methods included from Helpers

#method_missing, #num_set_parts, #pattern_code, #respond_to_missing?, #set_name_parts, #shortname, #underscore

Methods included from AdvancedApi

#attachment, #define_set_from_error, #ensure_set, #setting_opts, #stage_method

Methods included from Format

#before, #format, layout_method_name, #view, view_method_name, view_setting_method_name, wrapper_method_name

Methods included from Inheritance

#include_set, #include_set_formats

Methods included from Basket

#abstract_basket, #add_to_basket, #basket, #unshift_basket

Methods included from Trait

#card_accessor, #card_reader, #card_writer, #require_field

Methods included from Event::Api

#event

Class Method Details

.source_locationObject



7
# File 'card/tmpsets/set/mod011-card-mod-account/right/api_key.rb', line 7

def self.source_location; "/Users/ethan/dev/decko/gem/card-mod-account/set/right/api_key.rb"; end

Instance Method Details

#api_key_taken?Boolean

checks availability of API key

Returns:

  • (Boolean)


21
22
23
24
25
# File 'card/tmpsets/set/mod019-card-mod-api_key/right/api_key.rb', line 21

def api_key_taken?
  return false unless (acct = Card::Auth. content)

  acct.id != left_id
end

#authenticate_api_key(api_key) ⇒ True/False

Returns checks whether key matches content.

Returns:

  • (True/False)

    checks whether key matches content



40
41
42
43
44
45
# File 'card/tmpsets/set/mod019-card-mod-api_key/right/api_key.rb', line 40

def authenticate_api_key api_key
  return true unless (error = api_key_validation_error api_key)

  errors.add error, t(error)
  false
end

#event: generate_api_keyObject

triggerable event to generate new API Key



11
12
13
# File 'card/tmpsets/set/mod019-card-mod-api_key/right/api_key.rb', line 11

event :generate_api_key, :prepare_to_validate, trigger: :required do
  generate
end

#event: validate_api_keyObject



15
16
17
18
# File 'card/tmpsets/set/mod019-card-mod-api_key/right/api_key.rb', line 15

event :validate_api_key, :validate, on: :save, changed: :content do
  errors.add :content, t(:api_key_invalid) unless content.match?(/^\w{20,}$/)
  errors.add :content, t(:api_key_taken) if api_key_taken?
end

#generateObject



47
48
49
# File 'card/tmpsets/set/mod019-card-mod-api_key/right/api_key.rb', line 47

def generate
  self.content = SecureRandom.base64.tr "+/=", "Qrt"
end

#generate!Object



51
52
53
# File 'card/tmpsets/set/mod019-card-mod-api_key/right/api_key.rb', line 51

def generate!
  generate.tap { save! }
end

#history?Boolean

DURATIONS = “second minute hour day week month year”.freeze

Returns:

  • (Boolean)


12
13
14
# File 'card/tmpsets/set/mod011-card-mod-account/right/api_key.rb', line 12

def history?
  false
end

#ok_to_createObject



35
36
37
# File 'card/tmpsets/set/mod019-card-mod-api_key/right/api_key.rb', line 35

def ok_to_create
  own_account? || super
end

#ok_to_readObject



31
32
33
# File 'card/tmpsets/set/mod019-card-mod-api_key/right/api_key.rb', line 31

def ok_to_read
  own_account? || super
end

#simple_tokenObject



55
56
57
# File 'card/tmpsets/set/mod019-card-mod-api_key/right/api_key.rb', line 55

def simple_token
  Card::Auth::Token.encode accounted.id
end

#validate!(api_key) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'card/tmpsets/set/mod011-card-mod-account/right/api_key.rb', line 20

def validate! api_key
  error =
    case
    when !real?             then [:token_not_found, tr(:error_token_not_found)]
    # when expired?           then [:token_expired, tr(:error_token_expired)]
    when content != api_key then [:incorrect_token, tr(:error_incorrect_token)]
    end
  errors.add(*error) if error
  error.nil?
end

#view: rawObject



16
17
18
# File 'card/tmpsets/set/mod011-card-mod-account/right/api_key.rb', line 16

view :raw do
  tr :private_data
end