Class: Xcflushd::Credentials

Inherits:
Object
  • Object
show all
Defined in:
lib/xcflushd/credentials.rb

Overview

Credentials contains all the fields required to authenticate an app. In 3scale there are 3 authentication modes:

* App ID: app_id (required), app_key, referrer, user_id
* API key: user_key (required), referrer, user_id
* Oauth: access_token (required), app_id, referrer, user_id

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(creds) ⇒ Credentials

Initializes a credentials object from a ‘creds’ hash. The accepted fields of the hash are:

app_id, app_key, referrer, user_id, user_key, and access_token.

Extra fields are discarded.



19
20
21
# File 'lib/xcflushd/credentials.rb', line 19

def initialize(creds)
  @creds = creds.select { |k, _| FIELDS.include?(k) }
end

Instance Attribute Details

#credsObject (readonly)

Returns the value of attribute creds.



13
14
15
# File 'lib/xcflushd/credentials.rb', line 13

def creds
  @creds
end

Class Method Details

.from(escaped_s) ⇒ Object

Creates a Credentials object from an escaped string. The string has this format: credential1:value1,credential2:value2, etc. ‘,’ and ‘:’ are escaped in the values



44
45
46
47
48
49
50
51
52
# File 'lib/xcflushd/credentials.rb', line 44

def self.from(escaped_s)
  creds_hash = escaped_s.split(/(?<!\\),/)
                        .map { |field_value| field_value.split(/(?<!\\):/) }
                        .map { |split| [unescaped(split[0]).to_sym,
                                        unescaped(split[1])] }
                        .to_h

  new(creds_hash)
end

Instance Method Details

#==(other) ⇒ Object



33
34
35
# File 'lib/xcflushd/credentials.rb', line 33

def ==(other)
  self.class == other.class && creds == other.creds
end

#oauth?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/xcflushd/credentials.rb', line 37

def oauth?
  !creds[:access_token].nil?
end

#to_sorted_escaped_sObject

This method returns all the credentials with this format: credential1:value1,credential2:value2, etc. The delimiters used, ‘,’ and ‘:’, are escaped in the values. Also, the credentials appear in alphabetical order.



27
28
29
30
31
# File 'lib/xcflushd/credentials.rb', line 27

def to_sorted_escaped_s
  creds.sort_by { |cred, _| cred }
       .map { |cred, value| "#{escaped(cred.to_s)}:#{escaped(value)}" }
       .join(',')
end