Module: Textus::BuiltinActions

Defined in:
lib/textus/builtin_actions.rb

Class Method Summary collapse

Class Method Details

.register_allObject

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/textus/builtin_actions.rb', line 9

def self.register_all
  Textus.action(:json) do |config:, store:, args:|
    _ = store
    _ = args
    data = JSON.parse(config["bytes"].to_s)
    { _meta: {}, body: YAML.dump(data) }
  end

  Textus.action(:csv) do |config:, store:, args:|
    _ = store
    _ = args
    rows = CSV.parse(config["bytes"].to_s, headers: true).map(&:to_h)
    { _meta: {}, body: YAML.dump(rows) }
  end

  Textus.action(:"markdown-links") do |config:, store:, args:|
    _ = store
    _ = args
    links = config["bytes"].to_s.scan(%r{\[([^\]]+)\]\((https?://[^)\s]+)\)}).map do |text, href|
      { "text" => text, "href" => href }
    end
    { _meta: {}, body: YAML.dump(links) }
  end

  Textus.action(:"ical-events") do |config:, store:, args:|
    _ = store
    _ = args
    events = []
    current = nil
    config["bytes"].to_s.each_line do |line|
      line = line.strip
      case line
      when "BEGIN:VEVENT" then current = {}
      when "END:VEVENT"
        events << current if current
        current = nil
      when /\A(SUMMARY|DTSTART|DTEND|UID|LOCATION|DESCRIPTION):(.*)\z/
        current[Regexp.last_match(1).downcase] = Regexp.last_match(2) if current
      end
    end
    { _meta: {}, body: YAML.dump(events) }
  end

  Textus.action(:rss) do |config:, store:, args:|
    _ = store
    _ = args
    doc = REXML::Document.new(config["bytes"].to_s)
    items = doc.elements.to_a("//item").map do |item|
      {
        "title" => item.elements["title"]&.text,
        "link" => item.elements["link"]&.text,
        "pubDate" => item.elements["pubDate"]&.text,
      }
    end
    { _meta: {}, body: YAML.dump(items) }
  end
end