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



51
52
53
# File 'lib/inferno/repositories/session_data.rb', line 51

def entity_class_name
  'SessionData'
end

#get_all_from_session(test_session_id) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/inferno/repositories/session_data.rb', line 38

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



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

def load(test_session_id:, name:, type: 'text')
  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 || '{}'))
  else
    raw_value
  end
end

#save(params) ⇒ Object



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

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

#value_to_persist(params) ⇒ Object



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

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 'oauth_credentials'
    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, params[:value].class)
      else
        params[:value]
      end

    credentials.name = params[:name]
    credentials.to_s
  else
    raise Exceptions::UnknownSessionDataType, params
  end
end