Class: Vox::HTTP::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/vox/http/route.rb

Overview

Route that contains information about a request path, intended for use with Client#request.

Constant Summary collapse

MAJOR_PARAMS =

Major parameters that are significant when forming a rate limit key.

%i[guild_id channel_id webhook_id].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(verb, key, **params) ⇒ Route

Create a new route to be used with Client#request

Parameters:

  • verb (#to_sym)

    The HTTP verb to be used when accessing the API path.

  • key (String)

    The unformatted route using Kernel.format syntax to incorporate the data provided in ‘params`.

  • params (Hash<String, #to_s>)

    Parameters passed when formatting ‘key`.



32
33
34
35
36
37
# File 'lib/vox/http/route.rb', line 32

def initialize(verb, key, **params)
  @verb = verb.downcase.to_sym
  @key = key
  @params = params
  @rl_key = "#{@verb}:#{@key}:#{major_param}"
end

Instance Attribute Details

#keyString (readonly)

Returns Unformatted API path, using Kernel.format syntax referencing keys in #params.

Returns:

  • (String)

    Unformatted API path, using Kernel.format syntax referencing keys in #params.



17
18
19
# File 'lib/vox/http/route.rb', line 17

def key
  @key
end

#paramsHash (readonly)

Returns Parameters that are passed to be used when formatting the API path.

Returns:

  • (Hash)

    Parameters that are passed to be used when formatting the API path.



25
26
27
# File 'lib/vox/http/route.rb', line 25

def params
  @params
end

#rl_keyString (readonly)

Returns String that defines an endpoint based on HTTP verb, API path, and major parameter if any.

Returns:

  • (String)

    String that defines an endpoint based on HTTP verb, API path, and major parameter if any.



21
22
23
# File 'lib/vox/http/route.rb', line 21

def rl_key
  @rl_key
end

#verbSymbol, String (readonly)

Returns HTTP verb to be used when accessing the API path.

Returns:

  • (Symbol, String)

    HTTP verb to be used when accessing the API path.



13
14
15
# File 'lib/vox/http/route.rb', line 13

def verb
  @verb
end

Instance Method Details

#==(other) ⇒ true, false

Compare a Vox::HTTP::Route or Vox::HTTP::Route like object (responds to ‘#verb`, `#key`, and `#params`).

Parameters:

Returns:

  • (true, false)


55
56
57
# File 'lib/vox/http/route.rb', line 55

def ==(other)
  @verb == other.verb && @key == other.key && @params == other.params
end

#formatString

Format the route with the given params

Returns:

  • (String)

    Formatted API path.



41
42
43
44
45
# File 'lib/vox/http/route.rb', line 41

def format
  return @key if @params.empty?

  Kernel.format(@key, @params) if @params.any?
end

#major_paramString, ...

Returns The major param value of the route key if any.

Returns:

  • (String, Integer, nil)

    The major param value of the route key if any



48
49
50
# File 'lib/vox/http/route.rb', line 48

def major_param
  params.slice(*MAJOR_PARAMS).values.first
end