Class: Jekyll::WebmentionIO::Config::BadUriPolicy

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll/config.rb

Defined Under Namespace

Classes: BadUriPolicyEntry

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site_config) ⇒ BadUriPolicy

Returns a new instance of BadUriPolicy.



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/jekyll/config.rb', line 171

def initialize(site_config)
  @bad_uri_policy = site_config['bad_uri_policy'] || {}

  @bad_uri_policy['whitelist'] ||= []
  @bad_uri_policy['blacklist'] ||= []

  # We always want to collect webmentions from the configured API host,
  # so we explicitly whitelist it. This way a transient service outage
  # won't get the endpoint banned by the bad-URI policy. Derived from the
  # configured api_url (default webmention.io) so a custom endpoint gets
  # the same protection.
  @bad_uri_policy['whitelist'].insert(-1, api_host_pattern(site_config))

  @whitelist = @bad_uri_policy['whitelist'].map { |expr| Regexp.new(expr) }
  @blacklist = @bad_uri_policy['blacklist'].map { |expr| Regexp.new(expr) }
end

Instance Attribute Details

#blacklistObject (readonly)

Returns the value of attribute blacklist.



169
170
171
# File 'lib/jekyll/config.rb', line 169

def blacklist
  @blacklist
end

#whitelistObject (readonly)

Returns the value of attribute whitelist.



169
170
171
# File 'lib/jekyll/config.rb', line 169

def whitelist
  @whitelist
end

Instance Method Details

#for_state(state) ⇒ Object

Given the provided state value (see WebmentionPolicy::State), retrieve the policy entry. If no entry exists, return a new default entry that indicates unlimited retries.



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/jekyll/config.rb', line 199

def for_state(state)
  default_policy = { 'policy' => UriPolicy::RETRY }

  # Retrieve the policy entry, the default entry, or the canned default
  policy_entry = @bad_uri_policy[state] || @bad_uri_policy['default'] || default_policy

  # Convert shorthand entry to full policy record
  if policy_entry.instance_of? String
    policy_entry = { 'policy' => policy_entry }
  end

  if policy_entry['policy'] == UriPolicy::RETRY && !policy_entry.key?('retry_delay')
    # If this is a retry policy and no delay is set, set up the default
    # delay policy.  This inherits from the legacy cache_bad_uris_for
    # setting to enable backward compatibility with older configurations.
    #
    # We do this here to make the rule enforcement logic a little tidier.

    policy_entry['retry_delay'] = [(@bad_uri_policy['cache_bad_uris_for'] || 1) * 24]
  end

  # Now finally convert into a proper policy entry structure
  BadUriPolicyEntry.new(
    policy_entry['policy'],
    policy_entry['max_attempts'],
    policy_entry['retry_delay']
  )
end

#set_policy(state, policy, max_attempts = nil, retry_delay = nil) ⇒ Object



188
189
190
191
192
193
194
# File 'lib/jekyll/config.rb', line 188

def set_policy(state, policy, max_attempts = nil, retry_delay = nil)
  @bad_uri_policy[state] = {
    'policy' => policy,
    'max_attempts' => max_attempts,
    'retry_delay' => retry_delay
  }
end