Module: Textus::Hooks::Builtin

Defined in:
lib/textus/hooks/builtin.rb

Class Method Summary collapse

Class Method Details

.register_allObject

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



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
66
# File 'lib/textus/hooks/builtin.rb', line 10

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

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

  Textus.hook(:fetch, :"markdown-links") do |store:, config:, 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.hook(:fetch, :"ical-events") do |store:, config:, 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.hook(:fetch, :rss) do |store:, config:, 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