Class: BBC::Redux::Key

Inherits:
Object
  • Object
show all
Defined in:
lib/bbc/redux/key.rb

Overview

Redux API Asset Key Object

Each asset is served with an associated key that is needed to access it's associated media. Generally these keys have a lifetime of 24 hours

Examples:

Properties of the key object


key = redux_client.asset('5966413090093319525').key

key.expired?   #=> Boolean
key.expires_at #=> DateTime
key.live?      #=> Boolean
key.ttl        #=> Integer
key.value      #=> String

Author:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Key

Returns a new instance of Key

Parameters:

  • value (String)

    the access keys value


31
32
33
34
# File 'lib/bbc/redux/key.rb', line 31

def initialize(value)
  @value      = value
  @expires_at = Time.at( value.split('-')[1].to_i ).to_datetime
end

Instance Attribute Details

#expires_atDateTime (readonly)

Returns the access key's expiry time

Returns:

  • (DateTime)

    the access key's expiry time


24
25
26
# File 'lib/bbc/redux/key.rb', line 24

def expires_at
  @expires_at
end

#valueString (readonly)

Returns the access key's value

Returns:

  • (String)

    the access key's value


28
29
30
# File 'lib/bbc/redux/key.rb', line 28

def value
  @value
end

Instance Method Details

#==(other_key) ⇒ Boolean Also known as: eql?

Returns true if other_key is a redux key with the same value

Returns:

  • (Boolean)

    true if other_key is a redux key with the same value


64
65
66
# File 'lib/bbc/redux/key.rb', line 64

def ==(other_key)
  self.class == other_key.class && self.value == other_key.value
end

#expired?Boolean

Returns true if ttl <= 0, false otherwise

Returns:

  • (Boolean)

    true if ttl <= 0, false otherwise

See Also:


39
40
41
# File 'lib/bbc/redux/key.rb', line 39

def expired?
  ttl <= 0
end

#live?Boolean

Returns true if ttl > 0, false otherwise

Returns:

  • (Boolean)

    true if ttl > 0, false otherwise

See Also:


46
47
48
# File 'lib/bbc/redux/key.rb', line 46

def live?
  ttl > 0
end

#to_sObject

Returns the key's value as a string

Returns:

  • the key's value as a string


51
52
53
# File 'lib/bbc/redux/key.rb', line 51

def to_s
  value
end

#ttlInteger

Returns key's Time To Live in seconds

Returns:

  • (Integer)

    key's Time To Live in seconds

See Also:


59
60
61
# File 'lib/bbc/redux/key.rb', line 59

def ttl
  expires_at.to_time.to_i - Time.now.to_i
end