Class: Inferno::Repositories::Requests

Inherits:
Repository
  • Object
show all
Defined in:
lib/inferno/repositories/requests.rb

Defined Under Namespace

Classes: Model

Instance Method Summary collapse

Methods inherited from Repository

#add_non_db_entities, #build_entity, db, #db_params, #entity_class, #entity_class_name, #handle_non_db_params, #non_db_params, table_name, #update

Instance Method Details

#create(params) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/inferno/repositories/requests.rb', line 9

def create(params)
  request = self.class::Model.create(db_params(params))

  headers = create_headers(request, params)

  params[:tags]&.each do |tag|
    request.add_tag(tag)
  end

  build_entity(
    request.to_hash
      .merge(headers:)
      .merge(non_db_params(params))
  )
end

#create_headers(request, params) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/inferno/repositories/requests.rb', line 25

def create_headers(request, params)
  request_headers = (params[:request_headers] || []).map do |header|
    request.add_header(header.merge(request_id: request.index, type: 'request'))
  end
  response_headers = (params[:response_headers] || []).map do |header|
    request.add_header(header.merge(request_id: request.index, type: 'response'))
  end

  (request_headers + response_headers).map { |header| headers_repo.build_entity(header.to_hash) }
end

#find(id) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/inferno/repositories/requests.rb', line 36

def find(id)
  result =
    self.class::Model
      .where(id:)
      .select(*entity_class::SUMMARY_FIELDS)
      .to_a
  return nil if result.blank?

  build_entity(result.first.to_hash)
end

#find_full_request(id) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/inferno/repositories/requests.rb', line 47

def find_full_request(id)
  result =
    self.class::Model
      .find(id:)
      .to_json_data(json_serializer_options)
      .deep_symbolize_keys!

  build_entity(result)
end

#find_named_request(test_session_id, name) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/inferno/repositories/requests.rb', line 57

def find_named_request(test_session_id, name)
  results =
    self.class::Model
      .where(test_session_id:, name: name.to_s)
      .map { |model| model.to_json_data(json_serializer_options) }
  return nil if results.blank?

  result = results.reduce { |max, current| current['index'] > max['index'] ? current : max }
  result.deep_symbolize_keys!

  build_entity(result)
end

#json_serializer_optionsObject



93
94
95
96
97
# File 'lib/inferno/repositories/requests.rb', line 93

def json_serializer_options
  {
    include: [:headers, :tags]
  }
end

#requests_for_result(result_id) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/inferno/repositories/requests.rb', line 83

def requests_for_result(result_id)
  self.class::Model
    .order(:index)
    .where(result_id:)
    .select(*entity_class::SUMMARY_FIELDS)
    .to_a
    .map(&:to_hash)
    .map! { |result| build_entity(result) }
end

#tagged_requests(test_session_id, tags) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/inferno/repositories/requests.rb', line 70

def tagged_requests(test_session_id, tags)
  self.class::Model
    .tagged_requests(test_session_id, tags)
    .to_a
    .map! do |request|
      build_entity(
        request
          .to_json_data(json_serializer_options)
          .deep_symbolize_keys!
      )
    end
end