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
# File 'lib/jekyll/commands/webmention.rb', line 18

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

  WebmentionIO.bootstrap(site)

  send_webmentions
end

.send_webmentionsObject



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
81
# File 'lib/jekyll/commands/webmention.rb', line 27

def self.send_webmentions
  WebmentionIO.log 'msg', 'Getting ready to send webmentions (this may take a while).'

  count = 0
  max_attempts = WebmentionIO.config.max_attempts
  outgoing = WebmentionIO.caches.outgoing_webmentions

  return if outgoing.empty?

  outgoing.each do |source, targets|
    targets.each do |target, response|
      # skip ones we’ve handled
      next unless response == false || response.instance_of?(Integer)

      # skip protocol-less links, we'll need to revisit this again later
      idx = target.index('//')
      next if idx.nil? || idx.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.policy.uri_ok?(escaped)

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

      if !max_attempts.nil? && 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 response
      response = WebmentionIO.webmentions.send_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

  outgoing.write
  WebmentionIO.log 'msg', "#{count} webmentions sent."
end