Class: Generator::RateLimitParser

Inherits:
Object
  • Object
show all
Defined in:
lib/generator/parsers/rate_limit_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(operation) ⇒ RateLimitParser

Returns a new instance of RateLimitParser.



7
8
9
# File 'lib/generator/parsers/rate_limit_parser.rb', line 7

def initialize(operation)
  @operation = operation
end

Instance Attribute Details

#operationObject (readonly)

Returns the value of attribute operation.



5
6
7
# File 'lib/generator/parsers/rate_limit_parser.rb', line 5

def operation
  @operation
end

Instance Method Details

#parseObject

CAUTION: This method parses rate limits from human-readable documentation. This is inherently fragile. Amazon could change their documentation format at any time without considering it a breaking change. However, this is the only source for rate limit data in their OpenAPI specifications.

Extracts the rate value (requests per second) from the usage plan table Example table:

Rate (requests per second) Burst
0.0167 20

Returns: 0.0167 (rate value in requests per second)



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/generator/parsers/rate_limit_parser.rb', line 22

def parse
  pattern = %r{
    Burst\s*\|                      # Find "Burst |" at end of table header
    (?:\\n|\n)                      # Either literal \n string or actual newline
    \|(?:\s*-+\s*\|){2,3}           # Separator line (2-3 columns of dashes)
    (?:\\n|\n)                      # Either literal \n string or actual newline
    (?:\|[^|]*){0,1}                # Optional first column (e.g., "Default" in 3-column format)
    \|\s*(\S+)\s*\|                 # Capture rate value (always before burst value)
    [^|]*\|                         # Skip to burst column
  }mx

  match = operation["description"].match(pattern)
  if match
    rate_value = match[1]
    # Handle "n" as a special case - indicates variable/dynamic rate limits
    # We return :unknown to signal that rate limiting exists but no default is provided
    # See: https://developer-docs.amazon.com/sp-api/docs/fulfillment-inbound-api-rate-limits
    return :unknown if rate_value.downcase == "n"

    rate_value.to_f
  elsif operation["description"].match?(/Usage\s+[Pp]lans?:/)
    # Fail when we can't extract rate limit but Usage Plan exists. This likely means Amazon changed their
    # documentation format.
    raise "Failed to extract rate limit for #{operation["operationId"]}. Usage Plan found in description but " \
      "regex failed to match. This usually means Amazon changed their documentation format."
  end
end