Class: Memoir::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/memoir/request.rb

Constant Summary collapse

VALID_OPTIONS =
{
  no_annotations: 'noAnnotations',
  global_annotations: 'globalAnnotations',
  ms_resolution: 'msResolution',
  show_tsuids: 'showTSUIDs',
  show_summary: 'showSummary',
  show_query: 'showQuery',
  delete: 'delete'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_time, end_time = nil, **options) ⇒ Request

Returns a new instance of Request.



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

def initialize(start_time, end_time = nil, **options)
  fail Memoir::Errors::ArgumentError, 'start_time cannot be nil' unless start_time

  @start_time = start_time
  @end_time = end_time
  @queries = []
  @options = options
  @arrays = false
end

Instance Attribute Details

#arraysObject

Returns the value of attribute arrays.



2
3
4
# File 'lib/memoir/request.rb', line 2

def arrays
  @arrays
end

#end_timeObject

Returns the value of attribute end_time.



2
3
4
# File 'lib/memoir/request.rb', line 2

def end_time
  @end_time
end

#optionsObject

Returns the value of attribute options.



2
3
4
# File 'lib/memoir/request.rb', line 2

def options
  @options
end

#queriesObject

Returns the value of attribute queries.



2
3
4
# File 'lib/memoir/request.rb', line 2

def queries
  @queries
end

#start_timeObject

Returns the value of attribute start_time.



2
3
4
# File 'lib/memoir/request.rb', line 2

def start_time
  @start_time
end

Instance Method Details

#<<(query) ⇒ Object



24
25
26
# File 'lib/memoir/request.rb', line 24

def <<(query)
  add_query(query)
end

#add_query(query) ⇒ Object



28
29
30
# File 'lib/memoir/request.rb', line 28

def add_query(query)
  queries << query
end

#clear_queriesObject



36
37
38
# File 'lib/memoir/request.rb', line 36

def clear_queries
  queries.clear
end

#remove_query(query) ⇒ Object



32
33
34
# File 'lib/memoir/request.rb', line 32

def remove_query(query)
  queries.delete query
end

#to_hObject



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/memoir/request.rb', line 44

def to_h
  validate!

  hash = {
    'start' => start_time.instance_of?(Memoir::TimePeriod) ? "#{start_time}-ago" : start_time.to_i,
    'queries' => queries.map(&:to_h)
  }

  hash['end'] = end_time.to_i if end_time
  options.each_pair { |key, value| hash[VALID_OPTIONS[key]] = value }

  hash
end

#to_jsonObject



40
41
42
# File 'lib/memoir/request.rb', line 40

def to_json
  JSON.pretty_generate(to_h)
end