Class: Inferno::Repositories::SessionData

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

Defined Under Namespace

Classes: Model

Instance Method Summary collapse

Methods inherited from Repository

#add_non_db_entities, #build_entity, #create, db, #db_params, #entity_class, #find, #handle_non_db_params, #non_db_params, table_name, #update

Instance Method Details

#entity_class_nameObject



56
57
58
# File 'lib/inferno/repositories/session_data.rb', line 56

def entity_class_name
  'SessionData'
end

#get_all_from_session(test_session_id) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/inferno/repositories/session_data.rb', line 43

def get_all_from_session(test_session_id)
  self.class::Model
    .where(test_session_id:)
    .all
    .map! do |session_data_hash|
      build_entity(
        session_data_hash
          .to_json_data
          .deep_symbolize_keys!
      )
    end
end

#load(test_session_id:, name:, type: 'text') ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/inferno/repositories/session_data.rb', line 25

def load(test_session_id:, name:, type: 'text') # rubocop:disable Metrics/CyclomaticComplexity
  raw_value =
    self.class::Model
      .find(test_session_id:, name: name.to_s.downcase)
      &.value

  case type.to_s
  when 'oauth_credentials'
    DSL::OAuthCredentials.new(JSON.parse(raw_value || '{}'))
  when 'checkbox'
    JSON.parse(raw_value || '[]')
  when 'auth_info'
    DSL::AuthInfo.new(JSON.parse(raw_value || '{}'))
  else
    raw_value
  end
end

#save(params) ⇒ Object



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

def save(params)
  name = params[:name].to_s.downcase
  test_session_id = params[:test_session_id]

  value = value_to_persist(params)

  db
    .insert_conflict(
      target: :id,
      update: { value: }
    ).insert(
      id: "#{test_session_id}_#{name}",
      name:,
      value:,
      test_session_id:
    )
end

#serialize_auth_info(params) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/inferno/repositories/session_data.rb', line 111

def serialize_auth_info(params)
  auth =
    if params[:value].is_a? String
      DSL::AuthInfo.new(JSON.parse(params[:value]))
    elsif !params[:value].is_a? DSL::AuthInfo
      raise Exceptions::BadSessionDataType.new(
        params[:name],
        DSL::AuthInfo.name,
        params[:value].class
      )
    else
      params[:value]
    end

  auth.name = params[:name]
  auth.to_s
end

#serialize_checkbox_input(params) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/inferno/repositories/session_data.rb', line 77

def serialize_checkbox_input(params)
  if params[:value].is_a?(String)
    params[:value]
  elsif params[:value].nil?
    '[]'
  elsif params[:value].is_a?(Array)
    params[:value].to_json
  else
    raise Exceptions::BadSessionDataType.new(
      params[:name],
      'JSON String or Array',
      params[:value].class
    )
  end
end

#serialize_oauth_credentials_input(params) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/inferno/repositories/session_data.rb', line 93

def serialize_oauth_credentials_input(params)
  credentials =
    if params[:value].is_a? String
      DSL::OAuthCredentials.new(JSON.parse(params[:value]))
    elsif !params[:value].is_a? DSL::OAuthCredentials
      raise Exceptions::BadSessionDataType.new(
        params[:name],
        DSL::OAuthCredentials.name,
        params[:value].class
      )
    else
      params[:value]
    end

  credentials.name = params[:name]
  credentials.to_s
end

#value_to_persist(params) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/inferno/repositories/session_data.rb', line 60

def value_to_persist(params)
  return nil if params[:value].blank?

  case params[:type]&.to_s
  when 'text', 'textarea', 'radio'
    params[:value].to_s
  when 'checkbox'
    serialize_checkbox_input(params)
  when 'oauth_credentials'
    serialize_oauth_credentials_input(params)
  when 'auth_info'
    serialize_auth_info(params)
  else
    raise Exceptions::UnknownSessionDataType, params
  end
end