Module: LightrateRails::ControllerHelper

Extended by:
ActiveSupport::Concern
Defined in:
lib/lightrate_rails/controller_helper.rb

Instance Method Summary collapse

Instance Method Details

#consume_lightrate_token_for_current_path(user_identifier: nil) ⇒ LightrateClient::ConsumeLocalBucketTokenResponse

Consume a token for the current request path

Parameters:

  • user_identifier (String, nil) (defaults to: nil)

    User identifier (defaults to current_user.id)

Returns:

  • (LightrateClient::ConsumeLocalBucketTokenResponse)

    The response object

Raises:



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/lightrate_rails/controller_helper.rb', line 96

def consume_lightrate_token_for_current_path(user_identifier: nil)
  user_id = user_identifier || current_user&.id
  raise ArgumentError, "User identifier is required" unless user_id

  response = lightrate_client.consume_local_bucket_token(
    path: request.path,
    http_method: request.request_method,
    user_identifier: user_id
  )

  unless response.success
    raise LightrateRails::LightRateNoTokensAvailable.new(request.path, user_id, response)
  end

  response
end

#consume_lightrate_token_for_path(path:, method: nil, user_identifier: nil) ⇒ Object

Manually consume a token from the local bucket for a specific path

Parameters:

  • path (String)

    The API path

  • method (String) (defaults to: nil)

    The HTTP method (defaults to current request method)

  • user_identifier (String, nil) (defaults to: nil)

    User identifier (defaults to current_user.id)

Raises:

  • (ArgumentError)


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/lightrate_rails/controller_helper.rb', line 117

def consume_lightrate_token_for_path(path:, method: nil, user_identifier: nil)
  user_id = user_identifier || current_user&.id
  raise ArgumentError, "User identifier is required" unless user_id

  http_method = method || request.request_method

  response = lightrate_client.consume_local_bucket_token(
    path: path,
    http_method: http_method,
    user_identifier: user_id
  )

  unless response.success
    raise LightrateRails::LightRateNoTokensAvailable.new(path, user_id, response)
  end

  response
end

#lightrate_tokens_available?(user_identifier: nil) ⇒ Boolean

Check if tokens are available for the current request path Note: This method actually consumes a token to check availability

Parameters:

  • user_identifier (String, nil) (defaults to: nil)

    User identifier (defaults to current_user.id)

Returns:

  • (Boolean)

    true if tokens are available, false otherwise



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/lightrate_rails/controller_helper.rb', line 80

def lightrate_tokens_available?(user_identifier: nil)
  user_id = user_identifier || current_user&.id
  return false unless user_id

  begin
    consume_lightrate_token_for_current_path(user_identifier: user_id)
    true
  rescue LightrateRails::LightRateNoTokensAvailable
    false
  end
end