Module: Monk::Id

Defined in:
lib/monk/id.rb,
lib/monk/id/version.rb

Overview

Integrate Monk ID on the server-side by accessing payloads from the client-side JavaScript.

Author:

  • Monk Development, Inc.

Constant Summary

CONFIG_FILE =

Expected path of config file in Rails and Sinatra relative to the app's root directory.

'config/monk_id.yml'.freeze
'_monkIdPayload'.freeze
VERSION =

Current version of the library.

'1.2.0'.freeze

Class Method Summary collapse

Class Method Details

.config(key) ⇒ *

Get a config value. Attempts to load the config if it hasn't already been loaded.

Parameters:

  • key (String)

    Name of config value.

Returns:

  • (*)

    Config value.

Raises:

  • (StandardError)

    If the config can't be loaded.



52
53
54
55
56
# File 'lib/monk/id.rb', line 52

def config(key)
  load_config unless @config

  @config[key]
end

.load_config(path = nil, environment = nil) ⇒ Hash<String>

Load a YAML config file for a specific environment. Rails and Sinatra apps don't need to call this method if the config file is stored at CONFIG_FILE, as it's loaded automatically.

Parameters:

  • path (String) (defaults to: nil)

    Path of YAML config file to load. Leave `nil` to read from environment (`MONK_ID_CONFIG` variable, Rails, Sinatra).

  • environment (String) (defaults to: nil)

    Environment section to use. Leave `nil` to read from environment (`MONK_ID_ENV` variable, Rails, Sinatra). Defaults to `development`.

Returns:

  • (Hash<String>)

    Loaded config values.

Raises:

  • (StandardError)

    If the file doesn't exist or can't be read.



35
36
37
38
39
40
41
42
43
44
# File 'lib/monk/id.rb', line 35

def load_config(path = nil, environment = nil)
  path ||= config_path_from_environment
  environment ||= config_environment

  config = YAML.load_file(path)[environment]

  valid_config?(config)

  @config = config
end

.load_payload(encoded_payload = nil) ⇒ Hash<Symbol>

Load a payload from the client-side.

Parameters:

  • encoded_payload (String, #[]) (defaults to: nil)

    Encoded payload or Hash-like cookies object to automatically load the payload from.

Returns:

  • (Hash<Symbol>)

    Decoded and validate payload. Empty if there's no payload or it fails validation.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/monk/id.rb', line 64

def load_payload(encoded_payload = nil)
  payload = select_payload(encoded_payload)

  return @payload = {} unless payload

  begin
    payload = decode_payload(payload)
    valid = valid_payload?(payload)
  rescue
    valid = false
  end

  @payload = valid ? payload : {}
end

.logged_in?Boolean Also known as: signed_in?

Check whether there's a logged in user.

Returns:

  • (Boolean)

    Whether there's a logged in user.



98
99
100
# File 'lib/monk/id.rb', line 98

def logged_in?
  !user_id.nil?
end

.user_emailString?

Get the logged in user's email address.

Returns:

  • (String)

    If logged in user.

  • (nil)

    If no logged in user.



91
92
93
# File 'lib/monk/id.rb', line 91

def user_email
  payload_user(:email)
end

.user_idString?

Get the logged in user's UUID.

Returns:

  • (String)

    If logged in user.

  • (nil)

    If no logged in user.



83
84
85
# File 'lib/monk/id.rb', line 83

def user_id
  payload_user(:id)
end