Module: Pickpoint::Auth

Defined in:
lib/pickpoint/auth.rb

Defined Under Namespace

Classes: ClientAuthSession, State, StaticSession

Class Method Summary collapse

Class Method Details

.present?(s) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/pickpoint/auth.rb', line 32

def present?(s)
  !s.nil? && !s.to_s.empty?
end

.resolve(cfg, base_url, http) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/pickpoint/auth.rb', line 10

def resolve(cfg, base_url, http)
  n = 0
  n += 1 if present?(cfg.api_key)
  n += 1 if cfg.client_auth
  n += 1 if present?(cfg.access_token)
  if n > 1
    raise InvalidConfigError, "provide only one of: api_key | client_auth | access_token"
  end
  if n.zero?
    raise InvalidConfigError, "auth required: api_key, client_auth, or access_token"
  end

  if present?(cfg.api_key)
    return State.new(api_key: cfg.api_key)
  end
  if cfg.client_auth
    return State.new(session: ClientAuthSession.new(cfg.client_auth, base_url, http))
  end

  State.new(session: StaticSession.new(cfg.access_token))
end