lib/github/kv.rb in github-ds-0.2.8 vs lib/github/kv.rb in github-ds-0.2.9
- old
+ new
@@ -268,9 +268,32 @@
end
nil
end
+ # ttl :: String -> Result<[Time | nil]>
+ #
+ # Returns the expires_at time for the specified key or nil.
+ #
+ # Example:
+ #
+ # kv.ttl("foo")
+ # # => #<Result value: 2018-04-23 11:34:54 +0200>
+ #
+ # kv.ttl("foo")
+ # # => #<Result value: nil>
+ #
+ def ttl(key)
+ validate_key(key)
+
+ Result.new {
+ GitHub::SQL.value(<<-SQL, :key => key, :connection => connection)
+ SELECT expires_at FROM key_values
+ WHERE `key` = :key AND (expires_at IS NULL OR expires_at > NOW())
+ SQL
+ }
+ end
+
private
def validate_key(key, error_message: nil)
unless key.is_a?(String)
raise TypeError, error_message || "key must be a String in #{self.class.name}, but was #{key.class}"
end