Skip to content

Commit

Permalink
Merge branch 'main' into chore-add-homepage-to-package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
millotp authored Dec 2, 2024
2 parents 4c7437b + 066dd87 commit ca15e69
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
25 changes: 23 additions & 2 deletions clients/algoliasearch-client-ruby/lib/algolia/api/search_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3288,7 +3288,7 @@ def browse_synonyms(
#
# @return [String]
#
def generate_secured_api_key(parent_api_key, restrictions = {})
def self.generate_secured_api_key(parent_api_key, restrictions = {})
restrictions = restrictions.to_hash
if restrictions.key?(:searchParams)
# merge searchParams with the root of the restrictions
Expand All @@ -3310,13 +3310,24 @@ def generate_secured_api_key(parent_api_key, restrictions = {})
Base64.encode64("#{hmac}#{url_encoded_restrictions}").gsub("\n", "")
end

# Helper: Generates a secured API key based on the given `parent_api_key` and given `restrictions`.
#
# @param parent_api_key [String] Parent API key used the generate the secured key
# @param restrictions [SecuredApiKeyRestrictions] Restrictions to apply on the secured key
#
# @return [String]
#
def generate_secured_api_key(parent_api_key, restrictions = {})
self.class.generate_secured_api_key(parent_api_key, restrictions)
end

# Helper: Retrieves the remaining validity of the previous generated `secured_api_key`, the `validUntil` parameter must have been provided.
#
# @param secured_api_key [String]
#
# @return [Integer]
#
def get_secured_api_key_remaining_validity(secured_api_key)
def self.get_secured_api_key_remaining_validity(secured_api_key)
now = Time.now.to_i
decoded_key = Base64.decode64(secured_api_key)
regex = "validUntil=(\\d+)"
Expand All @@ -3331,6 +3342,16 @@ def get_secured_api_key_remaining_validity(secured_api_key)
valid_until - now
end

# Helper: Retrieves the remaining validity of the previous generated `secured_api_key`, the `validUntil` parameter must have been provided.
#
# @param secured_api_key [String]
#
# @return [Integer]
#
def get_secured_api_key_remaining_validity(secured_api_key)
self.class.get_secured_api_key_remaining_validity(secured_api_key)
end

# Helper: Saves the given array of objects in the given index. The `chunked_batch` helper is used under the hood, which creates a `batch` requests with at most 1000 objects in it.
#
# @param index_name [String]: The `index_name` to save `objects` in.
Expand Down
26 changes: 24 additions & 2 deletions templates/ruby/search_helpers.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ end
#
# @return [String]
#
def generate_secured_api_key(parent_api_key, restrictions = {})
def self.generate_secured_api_key(parent_api_key, restrictions = {})
restrictions = restrictions.to_hash
if restrictions.key?(:searchParams)
# merge searchParams with the root of the restrictions
Expand All @@ -186,13 +186,25 @@ def generate_secured_api_key(parent_api_key, restrictions = {})
Base64.encode64("#{hmac}#{url_encoded_restrictions}").gsub("\n", '')
end

# Helper: Generates a secured API key based on the given `parent_api_key` and given `restrictions`.
#
# @param parent_api_key [String] Parent API key used the generate the secured key
# @param restrictions [SecuredApiKeyRestrictions] Restrictions to apply on the secured key
#
# @return [String]
#
def generate_secured_api_key(parent_api_key, restrictions = {})
self.class.generate_secured_api_key(parent_api_key, restrictions)
end


# Helper: Retrieves the remaining validity of the previous generated `secured_api_key`, the `validUntil` parameter must have been provided.
#
# @param secured_api_key [String]
#
# @return [Integer]
#
def get_secured_api_key_remaining_validity(secured_api_key)
def self.get_secured_api_key_remaining_validity(secured_api_key)
now = Time.now.to_i
decoded_key = Base64.decode64(secured_api_key)
regex = 'validUntil=(\d+)'
Expand All @@ -207,6 +219,16 @@ def get_secured_api_key_remaining_validity(secured_api_key)
valid_until - now
end

# Helper: Retrieves the remaining validity of the previous generated `secured_api_key`, the `validUntil` parameter must have been provided.
#
# @param secured_api_key [String]
#
# @return [Integer]
#
def get_secured_api_key_remaining_validity(secured_api_key)
self.class.get_secured_api_key_remaining_validity(secured_api_key)
end

# Helper: Saves the given array of objects in the given index. The `chunked_batch` helper is used under the hood, which creates a `batch` requests with at most 1000 objects in it.
#
# @param index_name [String]: The `index_name` to save `objects` in.
Expand Down

0 comments on commit ca15e69

Please sign in to comment.