Module: Textus::Workflow::StepHelpers

Included in:
Textus::Workflow::StepExecutor::StepScope
Defined in:
lib/textus/workflow/step_helpers.rb

Instance Method Summary collapse

Instance Method Details

#check_naming(entries, pattern:, code:, fix:) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/textus/workflow/step_helpers.rb', line 48

def check_naming(entries, pattern:, code:, fix:)
  entries.filter_map do |entry|
    key = entry_value(entry, :key)
    path = entry_value(entry, :path)
    name = path && !path.to_s.empty? ? File.basename(path.to_s) : key.to_s.split(".").last
    next if pattern.match?(name)

    {
      "code" => code,
      "level" => "warning",
      "subject" => key,
      "message" => "#{key} does not match the required naming pattern #{pattern.inspect}",
      "fix" => fix,
    }
  end
end

#fetch_json(url, headers: {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/textus/workflow/step_helpers.rb', line 10

def fetch_json(url, headers: {})
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == "https"
  http.open_timeout = 5
  http.read_timeout = 30

  request = Net::HTTP::Get.new(uri)
  headers.each { |k, v| request[k] = v }
  request["Accept"] ||= "application/json"

  response = http.request(request)
  raise "HTTP #{response.code} for #{url}" unless response.is_a?(Net::HTTPOK)

  JSON.parse(response.body)
end

#merge_hashes(*hashes) ⇒ Object



40
41
42
# File 'lib/textus/workflow/step_helpers.rb', line 40

def merge_hashes(*hashes)
  hashes.reduce({}) { |acc, h| deep_merge(acc, h) }
end

#pluck(source, *keys) ⇒ Object



44
45
46
# File 'lib/textus/workflow/step_helpers.rb', line 44

def pluck(source, *keys)
  source.slice(*keys)
end

#render_template(name, data) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/textus/workflow/step_helpers.rb', line 27

def render_template(name, data)
  path = @container.layout.template_path(name)
  raise "template not found: #{name}" unless File.exist?(path)

  template = File.read(path)
  ctx = Textus::Protocol::Produce::Render::Context.new(
    container: @container, call: @call,
    key: @ctx&.key, entry: @ctx&.entry
  )
  renderer = Textus::Protocol::Produce::Render.new(template, ctx)
  renderer.bytes_for(data)
end