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



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

def create(params)
  request = self.class::Model.create(db_params(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

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

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

#find(id) ⇒ Object



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

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



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

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



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

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



69
70
71
72
73
# File 'lib/inferno/repositories/requests.rb', line 69

def json_serializer_options
  {
    include: :headers
  }
end

#requests_for_result(result_id) ⇒ Object



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

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