Class: Cased::Query

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/cased/query.rb

Instance Method Summary collapse

Constructor Details

#initialize(client, phrase: nil, variables: {}) ⇒ Query

Returns a new instance of Query.

Parameters:

  • client (Cased::HTTP::Client)

    the HTTP client authorized to query an audit trail policy

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

    the phrase to search for audit trail events

  • variables (Hash) (defaults to: {})

    the query variables

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
21
22
# File 'lib/cased/query.rb', line 14

def initialize(client, phrase: nil, variables: {})
  raise ArgumentError, 'variables must be a Hash' unless variables.is_a?(Hash)

  @client = client
  @phrase = phrase
  @page = 1
  @limit = 25
  @variables = variables
end

Instance Method Details

#limit(new_limit) ⇒ Cased::Query

Parameters:

  • new_limit (Integer)

    The number of audit trail events to return.

Returns:



34
35
36
37
38
# File 'lib/cased/query.rb', line 34

def limit(new_limit)
  @limit = [[new_limit, 100].min, 1].max

  self
end

#page(new_page) ⇒ Cased::Query

Parameters:

  • new_page (Integer)

    The page of audit trail events to request.

Returns:



42
43
44
45
46
# File 'lib/cased/query.rb', line 42

def page(new_page)
  @page = [1, new_page.to_i].max

  self
end

#phrase(new_phrase) ⇒ Cased::Query

Parameters:

  • new_phrase (String)

    The audit trail policy search phrase.

Returns:



26
27
28
29
30
# File 'lib/cased/query.rb', line 26

def phrase(new_phrase)
  @phrase = new_phrase

  self
end

#responseObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/cased/query.rb', line 73

def response
  return @response if defined?(@response)

  @response = begin
    resp = @client.get('events') do |req|
      req.params['phrase'] = @phrase unless @phrase.nil?
      req.params['per_page'] = @limit unless @limit.nil?
      req.params['page'] = @page unless @page.nil?
      req.params['variables'] = @variables unless @variables.nil?
    end

    CollectionResponse.new(response: resp)
  rescue Cased::HTTP::Error, Faraday::Error => e
    CollectionResponse.new(exception: e)
  end
end