Class: Jekyll::WebmentionIO::Config

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

Defined Under Namespace

Modules: HtmlProofer, UriPolicy Classes: BadUriPolicy, JsConfig, SyndicationRule

Constant Summary collapse

TIMEFRAMES =
{
  'last_week' => 'weekly',
  'last_month' => 'monthly',
  'last_year' => 'yearly',
}.freeze
DEFAULT_API_URL =

The default base URL for the Webmention.io API. Exposed as a config key so the endpoint can be pointed elsewhere (e.g. a local stand-in during integration testing) instead of being hard-coded in the network layer.

'https://webmention.io/api'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site = nil) ⇒ Config

Returns a new instance of Config.



59
60
61
62
63
64
65
66
67
# File 'lib/jekyll/config.rb', line 59

def initialize(site = nil)
  @site = site

  if site.nil?
    parse
  else
    parse(@site.config['webmentions'], @site.config['url'].to_s, @site.config['baseurl'].to_s)
  end
end

Instance Attribute Details

#api_hostObject (readonly)

The scheme://host origin and bare host of the configured API, derived from api_url. Used to build the service links emitted into the page head (and the JS) so they track a custom endpoint instead of being hard-coded to webmention.io.



39
40
41
# File 'lib/jekyll/config.rb', line 39

def api_host
  @api_host
end

#api_originObject (readonly)

The scheme://host origin and bare host of the configured API, derived from api_url. Used to build the service links emitted into the page head (and the JS) so they track a custom endpoint instead of being hard-coded to webmention.io.



39
40
41
# File 'lib/jekyll/config.rb', line 39

def api_origin
  @api_origin
end

#api_urlObject

Returns the value of attribute api_url.



30
31
32
# File 'lib/jekyll/config.rb', line 30

def api_url
  @api_url
end

#bad_uri_policyObject

Returns the value of attribute bad_uri_policy.



30
31
32
# File 'lib/jekyll/config.rb', line 30

def bad_uri_policy
  @bad_uri_policy
end

#cache_folderObject

Returns the value of attribute cache_folder.



30
31
32
# File 'lib/jekyll/config.rb', line 30

def cache_folder
  @cache_folder
end

#debugObject

Returns the value of attribute debug.



30
31
32
# File 'lib/jekyll/config.rb', line 30

def debug
  @debug
end

#html_proofer_ignoreObject

Returns the value of attribute html_proofer_ignore.



30
31
32
# File 'lib/jekyll/config.rb', line 30

def html_proofer_ignore
  @html_proofer_ignore
end

#jsObject

Returns the value of attribute js.



30
31
32
# File 'lib/jekyll/config.rb', line 30

def js
  @js
end

#legacy_domainsObject

Returns the value of attribute legacy_domains.



30
31
32
# File 'lib/jekyll/config.rb', line 30

def legacy_domains
  @legacy_domains
end

#max_attemptsObject

Returns the value of attribute max_attempts.



30
31
32
# File 'lib/jekyll/config.rb', line 30

def max_attempts
  @max_attempts
end

#pause_lookupsObject

Returns the value of attribute pause_lookups.



30
31
32
# File 'lib/jekyll/config.rb', line 30

def pause_lookups
  @pause_lookups
end

#site_urlObject

Returns the value of attribute site_url.



30
31
32
# File 'lib/jekyll/config.rb', line 30

def site_url
  @site_url
end

#syndicationObject

Returns the value of attribute syndication.



30
31
32
# File 'lib/jekyll/config.rb', line 30

def syndication
  @syndication
end

#templatesObject

Returns the value of attribute templates.



30
31
32
# File 'lib/jekyll/config.rb', line 30

def templates
  @templates
end

#throttle_lookupsObject

Returns the value of attribute throttle_lookups.



30
31
32
# File 'lib/jekyll/config.rb', line 30

def throttle_lookups
  @throttle_lookups
end

#usernameObject

Returns the value of attribute username.



30
31
32
# File 'lib/jekyll/config.rb', line 30

def username
  @username
end

Class Method Details

.api_uri(url) ⇒ Object

Resolves a webmention API URL to a URI, falling back to the default endpoint when the configured value has no host.



48
49
50
51
# File 'lib/jekyll/config.rb', line 48

def self.api_uri(url)
  uri = URI.parse(url)
  uri.host ? uri : URI.parse(DEFAULT_API_URL)
end

.authority(uri) ⇒ Object

The host of a parsed API URI, plus the port when it isn’t the scheme’s default (so custom/local endpoints still match the full URL).



55
56
57
# File 'lib/jekyll/config.rb', line 55

def self.authority(uri)
  uri.port == uri.default_port ? uri.host : "#{uri.host}:#{uri.port}"
end

Instance Method Details

#collectionsObject



162
163
164
# File 'lib/jekyll/config.rb', line 162

def collections
  @site.collections
end

#documentsObject

Based on the specified configuration, return the list of documents for the site that should be processed.



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/jekyll/config.rb', line 138

def documents
  documents = @site.posts.docs.clone

  if @pages == true
    WebmentionIO.log 'info', 'Including site pages.'
    documents.concat @site.pages.clone
  end

  if @collections.empty?
    WebmentionIO.log 'info', 'Adding collections.'

    @site.collections.each do |name, collection|
      # skip _posts
      next if name == 'posts'

      if collections.include?(name)
        documents.concat collection.docs.clone
      end
    end
  end

  documents
end

#last_lookup_threshold(date) ⇒ Object

The next lookup date has to be before this date to be allowed to request webmentions again.



121
122
123
124
125
126
127
# File 'lib/jekyll/config.rb', line 121

def last_lookup_threshold(date)
  age = get_timeframe_from_date(date)

  throttle = @throttle_lookups[age]

  throttle.nil? ? nil : get_date_from_string(throttle)
end

#parse(config = nil, site_url = '', base_url = '') ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/jekyll/config.rb', line 69

def parse(config = nil, site_url = '', base_url = '')
  config ||= {}

  @site_url = site_url
  @username = config['username']
  @debug = config['debug']
  @api_url = config['api_url'] || DEFAULT_API_URL
  api_uri = self.class.api_uri(@api_url)
  @api_host = api_uri.host
  @api_origin = "#{api_uri.scheme}://#{self.class.authority(api_uri)}"

  @pause_lookups =
    if !@site.nil? && @site.config['serving']
      WebmentionIO.log 'msg', 'Webmentions won’t be gathered when running `jekyll serve`.'

      true
    elsif !@site.nil? && @site_url.include?('localhost')
      WebmentionIO.log 'msg', 'Webmentions won’t be gathered on localhost.'

      true
    else
      config['pause_lookups']
    end

  @cache_folder = config['cache_folder'] || '.jekyll-cache'
  @cache_folder = @site.in_source_dir(@cache_folder) if !@site.nil?

  @pages = config['pages']
  @collections = config['collections'] || {}
  @templates = config['templates'] || {}

  @js = JsConfig.new(base_url, config['js'] || false)

  @html_proofer_ignore = HtmlProofer.get_const(
    config['html_proofer_ignore'] ||
    (config['html_proofer'] ? 'templates' : nil) ||
    'none'
  )

  @max_attempts = config['max_attempts']

  @bad_uri_policy = BadUriPolicy.new(config)

  @throttle_lookups = config['throttle_lookups'] || {}

  @legacy_domains = config['legacy_domains'] || []

  @syndication = (config['syndication'] || {}).transform_values { |entry| SyndicationRule.new(entry) }
end

#syndication_rule_for_uri(uri) ⇒ Object

Given a webmention endpoint, find the corresponding syndication rule Yes, this is a kind of reverse lookup so we can figure out of a given queued webmention was a result of a syndication rule.



132
133
134
# File 'lib/jekyll/config.rb', line 132

def syndication_rule_for_uri(uri)
  @syndication.values.detect { |rule| rule.endpoint == uri }
end