Skip to content

Commit fab9ea8

Browse files
committed
Expand and improve documentation
1 parent aa52ce6 commit fab9ea8

9 files changed

Lines changed: 76 additions & 81 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- Perform [endpoint discovery](https://www.w3.org/TR/webmention/#sender-discovers-receiver-webmention-endpoint) on mentioned URLs.
1313
- Send webmentions to one or more mentioned URLs (and optionally include a [vouch](https://indieweb.org/Vouch) URL).
1414
- Verify that a received webmention's source URL links to a target URL (and optionally verify that a vouch URL mentions the source URL's domain).
15+
- Supports Ruby 2.6 and newer.
1516

1617
## Getting Started
1718

USAGE.md

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ target = "https://aaronpk.example/post/100" # A post on someone else's website
1515
response = Webmention.send_webmention(source, target)
1616
```
1717

18-
`Webmention.send_webmention` will return either a `Webmention::Response` or a `Webmention::ErrorResponse`. Instances of both classes respond to `ok?`. Building on the examples above:
19-
20-
A `Webmention::ErrorResponse` may be returned when:
18+
`Webmention.send_webmention` will return either a `Webmention::Response` or a `Webmention::ErrorResponse`. Instances of both classes respond to `ok?`. Building on the examples above, a `Webmention::ErrorResponse` may be returned when:
2119

2220
1. The target URL does not advertise a Webmention endpoint.
2321
2. The request to the target URL raises an `HTTP::Error` or an `OpenSSL::SSL::SSLError`.
@@ -150,7 +148,4 @@ verification.vouch_mentions_source?
150148

151149
webmention-client-ruby avoids raising exceptions when making HTTP requests. As noted above, a `Webmention::ErrorResponse` should be returned in cases where an HTTP request triggers an exception.
152150

153-
When crawling the supplied URL, `Webmention.mentioned_urls` _may_ raise a `NoMethodError` if:
154-
155-
- a `Webmention::ErrorResponse` is returned, or
156-
- the response is of an unsupported MIME type.
151+
When crawling the supplied URL, `Webmention.mentioned_urls` _may_ raise a `NoMethodError` if a `Webmention::ErrorResponse` is returned, or the response is of an unsupported MIME type.

lib/webmention.rb

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ module Webmention
2222
# Retrieve unique URLs mentioned by the provided URL.
2323
#
2424
# @example
25-
# Webmention.mentioned_urls('https://jgarber.example/posts/100')
25+
# Webmention.mentioned_urls("https://jgarber.example/posts/100")
2626
#
2727
# @param url [String, HTTP::URI, #to_s] An absolute URL.
2828
#
2929
# @raise [NoMethodError]
30-
# Raised when response is a Webmention::ErrorResponse or response is of an
30+
# Raised when response is an {ErrorResponse} or response is of an
3131
# unsupported MIME type.
3232
#
3333
# @return [Array<String>]
@@ -38,14 +38,14 @@ def self.mentioned_urls(url)
3838
# Send a webmention from a source URL to a target URL.
3939
#
4040
# @example Send a webmention
41-
# source = 'https://jgarber.example/posts/100'
42-
# target = 'https://aaronpk.example/notes/1'
41+
# source = "https://jgarber.example/posts/100"
42+
# target = "https://aaronpk.example/notes/1"
4343
# Webmention.send_webmention(source, target)
4444
#
4545
# @example Send a webmention with a vouch URL
46-
# source = 'https://jgarber.example/posts/100'
47-
# target = 'https://aaronpk.example/notes/1'
48-
# Webmention.send_webmention(source, target, vouch: 'https://tantek.example/notes/1')
46+
# source = "https://jgarber.example/posts/100"
47+
# target = "https://aaronpk.example/notes/1"
48+
# Webmention.send_webmention(source, target, vouch: "https://tantek.example/notes/1")
4949
#
5050
# @param source [String, HTTP::URI, #to_s]
5151
# An absolute URL representing a source document.
@@ -55,22 +55,22 @@ def self.mentioned_urls(url)
5555
# An absolute URL representing a document vouching for the source document.
5656
# See https://indieweb.org/Vouch for additional details.
5757
#
58-
# @return [Webmention::Response, Webmention::ErrorResponse]
58+
# @return [Response, ErrorResponse]
5959
def self.send_webmention(source, target, vouch: nil)
6060
Client.new(source, vouch: vouch).send_webmention(target)
6161
end
6262

6363
# Send webmentions from a source URL to multiple target URLs.
6464
#
6565
# @example Send multiple webmentions
66-
# source = 'https://jgarber.example/posts/100'
67-
# targets = ['https://aaronpk.example/notes/1', 'https://adactio.example/notes/1']
66+
# source = "https://jgarber.example/posts/100"
67+
# targets = ["https://aaronpk.example/notes/1", "https://adactio.example/notes/1"]
6868
# Webmention.send_webmentions(source, targets)
6969
#
7070
# @example Send multiple webmentions with a vouch URL
71-
# source = 'https://jgarber.example/posts/100'
72-
# targets = ['https://aaronpk.example/notes/1', 'https://adactio.example/notes/1']
73-
# Webmention.send_webmentions(source, targets, vouch: 'https://tantek.example/notes/1')
71+
# source = "https://jgarber.example/posts/100"
72+
# targets = ["https://aaronpk.example/notes/1", "https://adactio.example/notes/1"]
73+
# Webmention.send_webmentions(source, targets, vouch: "https://tantek.example/notes/1")
7474
#
7575
# @param source [String, HTTP::URI, #to_s]
7676
# An absolute URL representing a source document.
@@ -80,7 +80,7 @@ def self.send_webmention(source, target, vouch: nil)
8080
# An absolute URL representing a document vouching for the source document.
8181
# See https://indieweb.org/Vouch for additional details.
8282
#
83-
# @return [Array<Webmention::Response, Webmention::ErrorResponse>]
83+
# @return [Array<Response, ErrorResponse>]
8484
def self.send_webmentions(source, *targets, vouch: nil)
8585
Client.new(source, vouch: vouch).send_webmentions(*targets)
8686
end
@@ -89,7 +89,7 @@ def self.send_webmentions(source, *targets, vouch: nil)
8989
#
9090
# @param (see Webmention.send_webmention)
9191
#
92-
# @raise (see Webmention::Client#mentioned_urls)
92+
# @raise (see Client#mentioned_urls)
9393
#
9494
# @return [Boolean]
9595
def self.verify_webmention(source, target, vouch: nil)

lib/webmention/client.rb

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,30 @@ class << self
99
attr_reader :registered_parsers
1010
end
1111

12-
# @return [Webmention::Url]
12+
# @return [Url]
1313
attr_reader :source_url
1414

15-
# @return [Webmention::Url]
15+
# @return [Url]
1616
attr_reader :vouch_url
1717

1818
# @api private
1919
def self.register_parser(klass)
2020
klass.mime_types.each { |mime_type| @registered_parsers[mime_type] = klass }
2121
end
2222

23-
# Create a new Webmention::Client.
23+
# Create a new {Client}.
2424
#
2525
# @example
26-
# Webmention::Client.new('https://jgarber.example/posts/100')
26+
# Webmention::Client.new("https://jgarber.example/posts/100")
2727
#
2828
# @example
29-
# Webmention::Client.new('https://jgarber.example/posts/100', vouch: 'https://tantek.example/notes/1')
29+
# Webmention::Client.new("https://jgarber.example/posts/100", vouch: "https://tantek.example/notes/1")
3030
#
3131
# @param source [String, HTTP::URI, #to_s]
3232
# An absolute URL representing a source document.
3333
# @param vouch [String, HTTP::URI, #to_s]
3434
# An absolute URL representing a document vouching for the source document.
3535
# See https://indieweb.org/Vouch for additional details.
36-
#
37-
# @return [Webmention::Client]
3836
def initialize(source, vouch: nil)
3937
@source_url = Url.new(source)
4038
@vouch_url = Url.new(vouch)
@@ -52,11 +50,11 @@ def inspect
5250
# Retrieve unique URLs mentioned by this client's source URL.
5351
#
5452
# @example
55-
# client = Webmention::Client.new('https://jgarber.example/posts/100')
53+
# client = Webmention::Client.new("https://jgarber.example/posts/100")
5654
# client.mentioned_urls
5755
#
5856
# @raise [NoMethodError]
59-
# Raised when response is a Webmention::ErrorResponse or response is of an
57+
# Raised when response is an {ErrorResponse} or response is of an
6058
# unsupported MIME type.
6159
#
6260
# @return [Array<String>]
@@ -78,13 +76,13 @@ def mentioned_urls
7876
# Send a webmention from this client's source URL to a target URL.
7977
#
8078
# @example
81-
# client = Webmention::Client.new('https://jgarber.example/posts/100')
82-
# client.send_webmention('https://aaronpk.example/notes/1')
79+
# client = Webmention::Client.new("https://jgarber.example/posts/100")
80+
# client.send_webmention("https://aaronpk.example/notes/1")
8381
#
8482
# @param target [String, HTTP::URI, #to_s]
8583
# An absolute URL representing a target document.
8684
#
87-
# @return [Webmention::Response, Webmention::ErrorResponse]
85+
# @return [Response, ErrorResponse]
8886
def send_webmention(target)
8987
target_url = Url.new(target)
9088

@@ -103,14 +101,14 @@ def send_webmention(target)
103101
# Send webmentions from this client's source URL to multiple target URLs.
104102
#
105103
# @example
106-
# client = Webmention::Client.new('https://jgarber.example/posts/100')
107-
# targets = ['https://aaronpk.example/notes/1', 'https://adactio.example/notes/1']
104+
# client = Webmention::Client.new("https://jgarber.example/posts/100")
105+
# targets = ["https://aaronpk.example/notes/1", "https://adactio.example/notes/1"]
108106
# client.send_webmentions(targets)
109107
#
110108
# @param targets [Array<String, HTTP::URI, #to_s>]
111109
# An array of absolute URLs representing multiple target documents.
112110
#
113-
# @return [Array<Webmention::Response, Webmention::ErrorResponse>]
111+
# @return [Array<Response, ErrorResponse>]
114112
def send_webmentions(*targets)
115113
targets.map { |target| send_webmention(target) }
116114
end
@@ -120,9 +118,9 @@ def send_webmentions(*targets)
120118
# @param target [String, HTTP::URI, #to_s]
121119
# An absolute URL representing a target document.
122120
#
123-
# @raise (see Webmention::Client#mentioned_urls)
121+
# @raise (see Client#mentioned_urls)
124122
#
125-
# @return [Webmention::Verification]
123+
# @return [Verification]
126124
def verify_webmention(target)
127125
Verification.new(source_url, Url.new(target), vouch_url: vouch_url)
128126
end

lib/webmention/error_response.rb

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,18 @@ class ErrorResponse
55
# @return [String]
66
attr_reader :message
77

8-
# @return [Webmention::Request]
8+
# @return [Request]
99
attr_reader :request
1010

11-
# Create a new Webmention::ErrorResponse.
11+
# Create a new {ErrorResponse}.
1212
#
1313
# Instances of this class represent HTTP requests that generated errors
1414
# (e.g. connection error, SSL error) or that could not locate a Webmention
15-
# endpoint. The nature of the error is captured in the <code>#message</code>
16-
# instance method.
15+
# endpoint. The nature of the error is captured in the {#message} instance
16+
# method.
1717
#
1818
# @param message [String]
19-
# @param request [Webmention::Request]
20-
#
21-
# @return [Webmention::ErrorResponse]
19+
# @param request [Request]
2220
def initialize(message, request)
2321
@message = message
2422
@request = request

lib/webmention/request.rb

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
module Webmention
44
class Request
5-
# Defaults derived from Webmention specification examples.
5+
# Set defaults derived from Webmention specification examples.
6+
#
67
# @see https://www.w3.org/TR/webmention/#limits-on-get-requests
8+
# W3C Webmention Recommendation § 4.2 Limits on GET requests
79
HTTP_CLIENT_OPTS = {
810
follow: {
911
max_hops: 20,
@@ -32,23 +34,23 @@ class Request
3234
# Send an HTTP GET request to the supplied URL.
3335
#
3436
# @example
35-
# Request.get('https://jgarber.example/posts/100')
37+
# Webmention::Request.get("https://jgarber.example/posts/100")
3638
#
3739
# @param url [String]
3840
#
39-
# @return [Webmention::Response, Webmention::ErrorResponse]
41+
# @return [Response, ErrorResponse]
4042
def self.get(url)
4143
new(:get, url).perform
4244
end
4345

4446
# Send an HTTP POST request with form-encoded data to the supplied URL.
4547
#
4648
# @example
47-
# Request.post(
48-
# 'https://aaronpk.example/webmention',
49-
# source: 'https://jgarber.examples/posts/100',
50-
# target: 'https://aaronpk.example/notes/1',
51-
# vouch: 'https://tantek.example/notes/1'
49+
# Webmention::Request.post(
50+
# "https://aaronpk.example/webmention",
51+
# source: "https://jgarber.examples/posts/100",
52+
# target: "https://aaronpk.example/notes/1",
53+
# vouch: "https://tantek.example/notes/1"
5254
# )
5355
#
5456
# @param url [String]
@@ -61,18 +63,16 @@ def self.get(url)
6163
# An absolute URL representing a document vouching for the source document.
6264
# See https://indieweb.org/Vouch for additional details.
6365
#
64-
# @return [Webmention::Response, Webmention::ErrorResponse]
66+
# @return [Response, ErrorResponse]
6567
def self.post(url, **options)
6668
new(:post, url, form: options.slice(:source, :target, :vouch)).perform
6769
end
6870

69-
# Create a new Webmention::Request.
71+
# Create a new {Request}.
7072
#
7173
# @param method [Symbol]
7274
# @param url [String]
7375
# @param options [Hash{Symbol => String}]
74-
#
75-
# @return [Webmention::Request]
7676
def initialize(method, url, **options)
7777
@method = method.to_sym
7878
@uri = HTTP::URI.parse(url.to_s)
@@ -88,9 +88,9 @@ def inspect
8888
end
8989
# :nocov:
9090

91-
# Submit the Webmention::Request.
91+
# Submit the {Request}.
9292
#
93-
# @return [Webmention::Response, Webmention::ErrorResponse]
93+
# @return [Response, ErrorResponse]
9494
def perform
9595
Response.new(client.request(method, uri, options), self)
9696
rescue HTTP::Error, OpenSSL::SSL::SSLError => e
@@ -99,6 +99,7 @@ def perform
9999

100100
private
101101

102+
# @return [HTTP::Client]
102103
def client
103104
@client ||= HTTP::Client.new(HTTP_CLIENT_OPTS)
104105
end

lib/webmention/response.rb

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Webmention
44
class Response
55
extend Forwardable
66

7-
# @return [Webmention::Request]
7+
# @return [Request]
88
attr_reader :request
99

1010
# @!method
@@ -31,16 +31,14 @@ class Response
3131
# @return [HTTP::URI]
3232
def_delegator :@response, :uri
3333

34-
# Create a new Webmention::Response.
34+
# Create a new {Response}.
3535
#
3636
# Instances of this class represent completed HTTP requests, the details
37-
# of which may be accessed using the delegated <code>#code</code> and
38-
# <code>#reason</code>) instance methods.
37+
# of which may be accessed using the delegated {#code} and {#reason}
38+
# instance methods.
3939
#
4040
# @param response [HTTP::Response]
41-
# @param request [Webmention::Request]
42-
#
43-
# @return [Webmention::Response]
41+
# @param request [Request]
4442
def initialize(response, request)
4543
@response = response
4644
@request = request

lib/webmention/url.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@ class Url
1111
# @return [String]
1212
def_delegator :uri, :to_s
1313

14-
# Create a new Webmention::Url.
14+
# Create a new {Url}.
1515
#
1616
# @param url [String, HTTP::URI, #to_s] An absolute URL.
17-
#
18-
# @return [Webmention::Url]
1917
def initialize(url)
2018
@uri = HTTP::URI.parse(url.to_s)
2119
end
@@ -28,7 +26,7 @@ def inspect
2826
end
2927
# :nocov:
3028

31-
# @return [Webmention::Response, Webmention::ErrorResponse]
29+
# @return [Response, ErrorResponse]
3230
def response
3331
@response ||= Request.get(uri)
3432
end

0 commit comments

Comments
 (0)