Class: OpenGraphPlus::APIKey
- Inherits:
-
Object
- Object
- OpenGraphPlus::APIKey
- Defined in:
- lib/opengraphplus/api_key.rb
Constant Summary collapse
- NAMESPACE =
"ogp"- PUBLIC_KEY_BYTES =
22 characters when base64 encoded
16- SECRET_KEY_BYTES =
43 characters when base64 encoded
32
Instance Attribute Summary collapse
-
#environment ⇒ Object
readonly
Returns the value of attribute environment.
-
#public_key ⇒ Object
readonly
Returns the value of attribute public_key.
-
#secret_key ⇒ Object
readonly
Returns the value of attribute secret_key.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(public_key:, secret_key:, environment: :live) ⇒ APIKey
constructor
A new instance of APIKey.
- #live? ⇒ Boolean
- #test? ⇒ Boolean
- #to_s ⇒ Object
Constructor Details
#initialize(public_key:, secret_key:, environment: :live) ⇒ APIKey
Returns a new instance of APIKey.
15 16 17 18 19 |
# File 'lib/opengraphplus/api_key.rb', line 15 def initialize(public_key:, secret_key:, environment: :live) @public_key = public_key @secret_key = secret_key @environment = environment.to_sym end |
Instance Attribute Details
#environment ⇒ Object (readonly)
Returns the value of attribute environment.
13 14 15 |
# File 'lib/opengraphplus/api_key.rb', line 13 def environment @environment end |
#public_key ⇒ Object (readonly)
Returns the value of attribute public_key.
13 14 15 |
# File 'lib/opengraphplus/api_key.rb', line 13 def public_key @public_key end |
#secret_key ⇒ Object (readonly)
Returns the value of attribute secret_key.
13 14 15 |
# File 'lib/opengraphplus/api_key.rb', line 13 def secret_key @secret_key end |
Class Method Details
.generate(environment: :live) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/opengraphplus/api_key.rb', line 37 def generate(environment: :live) new( public_key: SecureRandom.urlsafe_base64(PUBLIC_KEY_BYTES), secret_key: SecureRandom.urlsafe_base64(SECRET_KEY_BYTES), environment: environment ) end |
.parse(encoded_key) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/opengraphplus/api_key.rb', line 45 def parse(encoded_key) return nil unless encoded_key.is_a?(String) case encoded_key.split("_", 3) in [NAMESPACE, env, payload] payload .then { |p| Base64.urlsafe_decode64(p) } .then { |json| JSON.parse(json) } .then { |data| new(public_key: data["pk"], secret_key: data["sk"], environment: env) } else nil end rescue ArgumentError, JSON::ParserError nil end |
Instance Method Details
#live? ⇒ Boolean
28 29 30 |
# File 'lib/opengraphplus/api_key.rb', line 28 def live? environment == :live end |
#test? ⇒ Boolean
32 33 34 |
# File 'lib/opengraphplus/api_key.rb', line 32 def test? environment == :test end |
#to_s ⇒ Object
21 22 23 24 25 26 |
# File 'lib/opengraphplus/api_key.rb', line 21 def to_s { pk: public_key, sk: secret_key } .to_json .then { |json| Base64.urlsafe_encode64(json, padding: false) } .then { |payload| [NAMESPACE, environment, payload].join("_") } end |