Class: Jekyll::WebmentionIO::Commands::WebmentionCommand

Inherits:
Command
  • Object
show all
Defined in:
lib/jekyll/commands/webmention.rb

Class Method Summary collapse

Class Method Details

.init_with_program(prog) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/jekyll/commands/webmention.rb', line 9

def self.init_with_program(prog)
  prog.command(:webmention) do |c|
    c.syntax "webmention"
    c.description "Sends queued webmentions"

    c.action { |args, options| process args, options }
  end
end

.process(_args = [], options = {}) ⇒ Object



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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/jekyll/commands/webmention.rb', line 18

def self.process(_args = [], options = {})
  options = configuration_from_options(options)
  WebmentionIO.bootstrap(Jekyll::Site.new(options))

  if File.exist? WebmentionIO.cache_file("sent.yml")
    WebmentionIO.log "error", "Your outgoing webmentions queue needs to be upgraded. Please re-build your project."
  end

  WebmentionIO.log "msg", "Getting ready to send webmentions (this may take a while)."

  count = 0
  max_attempts = WebmentionIO.max_attempts()
  cached_outgoing = WebmentionIO.get_cache_file_path "outgoing"
  if File.exist?(cached_outgoing)
    outgoing = WebmentionIO.load_yaml(cached_outgoing)
    outgoing.each do |source, targets|
      targets.each do |target, response|
        # skip ones we’ve handled
        next unless response == false or response.instance_of? Integer

        # skip protocol-less links, we'll need to revisit this again later
        next if target.index("//").zero?

        # produce an escaped version of the target (in case of special
        # characters, etc).
        escaped = URI::Parser.new.escape(target);

        # skip bad URLs
        next unless WebmentionIO.uri_ok?(escaped)

        # give up if we've attempted this too many times
        response = (response || 0) + 1

        if ! max_attempts.nil? and response > max_attempts
          outgoing[source][target] = ""
          WebmentionIO.log "msg", "Giving up sending from #{source} to #{target}."
          next
        else
          outgoing[source][target] = response
        end

        # get the endpoint
        endpoint = WebmentionIO.get_webmention_endpoint(escaped)
        next unless endpoint

        # get the response
        response = WebmentionIO.webmention(source, target)
        next unless response

        # capture JSON responses in case site wants to do anything with them
        begin
          response = JSON.parse response
        rescue JSON::ParserError
          response = ""
        end
        outgoing[source][target] = response
        count += 1
      end
    end
    WebmentionIO.dump_yaml(cached_outgoing, outgoing)
    WebmentionIO.log "msg", "#{count} webmentions sent."
  end # file exists (outgoing)
end