Class: RiddlerAdmin::PreviewContext

Inherits:
ApplicationRecord show all
Defined in:
app/models/riddler_admin/preview_context.rb

Constant Summary collapse

MODEL_KEY =
"pctx".freeze
ID_LENGTH =

916_132_832 per second

5

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.convert_headers(input_headers) ⇒ Object



8
9
10
11
12
13
14
# File 'app/models/riddler_admin/preview_context.rb', line 8

def self.convert_headers input_headers
  original_headers = input_headers.to_h.
    select{|k,v| k.starts_with? "HTTP_"}.
    map{|k,v| [k.downcase.gsub(/^http_/, ""), v] }

  Hash[original_headers]
end

Instance Method Details

#dataObject



16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/models/riddler_admin/preview_context.rb', line 16

def data
  yaml_string = yaml

  if ::RiddlerAdmin.configuration.encrypt_preview_contexts? &&
    encrypted_yaml.present?

    yaml_string = decrypt encrypted_yaml
  end

  return {} if yaml_string.to_s.strip == ""
  YAML.safe_load yaml_string
end

#headers_hashObject



47
48
49
50
51
52
53
# File 'app/models/riddler_admin/preview_context.rb', line 47

def headers_hash
  headers_array = (headers || "").split("\n").map do |line|
    key, val = *line.split(":").map(&:strip)
    [key.downcase.gsub(/[^0-9a-z]/i, "_"), val]
  end
  Hash[headers_array]
end

#merge_headers(input_headers) ⇒ Object



55
56
57
58
# File 'app/models/riddler_admin/preview_context.rb', line 55

def merge_headers input_headers
  request_headers = self.class.convert_headers input_headers
  request_headers.merge headers_hash
end

#params_hashObject



40
41
42
43
44
45
# File 'app/models/riddler_admin/preview_context.rb', line 40

def params_hash
  params_array = (params || "").split("\n").map do |line|
    line.split(":").map &:strip
  end
  Hash[params_array]
end

#refresh_data(input_headers: {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'app/models/riddler_admin/preview_context.rb', line 29

def refresh_data input_headers: {}
  use_case_headers = merge_headers input_headers

  use_case = ::Riddler::UseCases::PreviewContext.new \
    params: params_hash,
    headers: use_case_headers

  hash = use_case.process
  update_data hash
end

#update_data(hash) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'app/models/riddler_admin/preview_context.rb', line 60

def update_data hash
  yaml = hash.to_yaml

  if ::RiddlerAdmin.configuration.encrypt_preview_contexts?
    encrypted_yaml = encrypt yaml
    update_attribute :encrypted_yaml, encrypted_yaml
  else
    update_attribute :yaml, yaml
  end
end