Class: Karafka::Web::Ui::Controllers::Requests::Params

Inherits:
Object
  • Object
show all
Defined in:
lib/karafka/web/ui/controllers/requests/params.rb

Overview

Internal representation of params with sane sanitization

Instance Method Summary collapse

Constructor Details

#initialize(request_params) ⇒ Params

Returns a new instance of Params.

Parameters:

  • request_params (Hash)

    raw hash with params



29
30
31
# File 'lib/karafka/web/ui/controllers/requests/params.rb', line 29

def initialize(request_params)
  @request_params = request_params
end

Instance Method Details

#[](key) ⇒ Object

Returns value of the requested param.

Parameters:

  • key (String, Symbol)

    params key

Returns:

  • (Object)

    value of the requested param



35
36
37
# File 'lib/karafka/web/ui/controllers/requests/params.rb', line 35

def [](key)
  fetch(key.to_s)
end

#bool(key) ⇒ Boolean

Returns boolean key value.

Parameters:

  • key (String, Symbol)

    params key

Returns:

  • (Boolean)

    boolean key value



54
55
56
# File 'lib/karafka/web/ui/controllers/requests/params.rb', line 54

def bool(key)
  ALLOWED_BOOLEAN_TRUE.include?(self[key])
end

#current_offsetInteger

Returns offset from which we want to start. -1 indicates, that we want to show the first page discovered based on the high watermark offset. If no offset is provided, we go with the high offset first page approach.

Returns:

  • (Integer)

    offset from which we want to start. -1 indicates, that we want to show the first page discovered based on the high watermark offset. If no offset is provided, we go with the high offset first page approach



98
99
100
101
102
103
# File 'lib/karafka/web/ui/controllers/requests/params.rb', line 98

def current_offset
  @current_offset ||= begin
    offset = @request_params.fetch("offset", -1).to_i
    [offset, -1].max
  end
end

#current_pageInteger

Note:

It does basic sanitization

Returns current page for paginated views.

Returns:

  • (Integer)

    current page for paginated views



80
81
82
83
84
85
86
# File 'lib/karafka/web/ui/controllers/requests/params.rb', line 80

def current_page
  @current_page ||= begin
    page = @request_params["page"].to_i

    page.positive? ? page : 1
  end
end

#current_partitionInteger

Returns currently selected partition or -1 if nothing provided.

Returns:

  • (Integer)

    currently selected partition or -1 if nothing provided



106
107
108
# File 'lib/karafka/web/ui/controllers/requests/params.rb', line 106

def current_partition
  @current_partition ||= @request_params.fetch("partition", -1).to_i
end

#current_rangeString

Returns Range type for charts we want to fetch.

Returns:

  • (String)

    Range type for charts we want to fetch



89
90
91
92
93
# File 'lib/karafka/web/ui/controllers/requests/params.rb', line 89

def current_range
  candidate = @request_params.fetch("range", "seconds")
  candidate = ALLOWED_RANGES.first unless ALLOWED_RANGES.include?(candidate)
  candidate.to_sym
end

#current_searchHash

Returns current search or empty if no search query present.

Returns:

  • (Hash)

    current search or empty if no search query present



65
66
67
68
69
70
71
# File 'lib/karafka/web/ui/controllers/requests/params.rb', line 65

def current_search
  return @current_search if @current_search

  search = @request_params["search"]

  @current_search = search.is_a?(Hash) ? search : {}
end

#current_sortString

Returns sort query value.

Returns:

  • (String)

    sort query value



74
75
76
# File 'lib/karafka/web/ui/controllers/requests/params.rb', line 74

def current_sort
  @current_sort ||= @request_params["sort"].to_s.downcase
end

#fetch(*args) ⇒ Object

Returns fetched object.

Parameters:

  • args (Symbol, Object)

    key we want to fetch and other args for Hash#fetch

Returns:

  • (Object)

    fetched object



41
42
43
44
# File 'lib/karafka/web/ui/controllers/requests/params.rb', line 41

def fetch(*args)
  args[0] = args[0].to_s
  @request_params.fetch(*args)
end

#int(key) ⇒ Integer

Returns integer value of the key.

Parameters:

  • key (String, Symbol)

    params key

Returns:

  • (Integer)

    integer value of the key



48
49
50
# File 'lib/karafka/web/ui/controllers/requests/params.rb', line 48

def int(key)
  self[key].to_i
end

#str(key) ⇒ String

Returns stringified key value.

Parameters:

  • key (String, Symbol)

    params key

Returns:

  • (String)

    stringified key value



60
61
62
# File 'lib/karafka/web/ui/controllers/requests/params.rb', line 60

def str(key)
  self[key].to_s
end