Class: Webmention::Client
- Inherits:
-
Object
- Object
- Webmention::Client
- Defined in:
- lib/webmention/client.rb
Class Attribute Summary collapse
- .registered_parsers ⇒ Object readonly private
Instance Attribute Summary collapse
- #source_url ⇒ Url readonly
- #vouch_url ⇒ Url readonly
Class Method Summary collapse
- .register_parser(klass) ⇒ Object private
Instance Method Summary collapse
-
#initialize(source, vouch: nil) ⇒ Client
constructor
Create a new Client.
-
#inspect ⇒ String
:nocov:.
-
#mentioned_urls ⇒ Array<String>
Retrieve unique URLs mentioned by this client’s source URL.
-
#send_webmention(target) ⇒ Response, ErrorResponse
Send a webmention from this client’s source URL to a target URL.
-
#send_webmentions(*targets) ⇒ Array<Response, ErrorResponse>
Send webmentions from this client’s source URL to multiple target URLs.
-
#verify_webmention(target) ⇒ Verification
Verify that this client’s source URL links to a target URL.
Constructor Details
Class Attribute Details
.registered_parsers ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
9 10 11 |
# File 'lib/webmention/client.rb', line 9 def registered_parsers @registered_parsers end |
Instance Attribute Details
#source_url ⇒ Url (readonly)
13 14 15 |
# File 'lib/webmention/client.rb', line 13 def source_url @source_url end |
#vouch_url ⇒ Url (readonly)
16 17 18 |
# File 'lib/webmention/client.rb', line 16 def vouch_url @vouch_url end |
Class Method Details
.register_parser(klass) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
19 20 21 |
# File 'lib/webmention/client.rb', line 19 def self.register_parser(klass) klass.mime_types.each { |mime_type| @registered_parsers[mime_type] = klass } end |
Instance Method Details
#inspect ⇒ String
:nocov:
43 44 45 46 47 |
# File 'lib/webmention/client.rb', line 43 def inspect "#<#{self.class}:#{format("%#0x", object_id)} " \ "source_url: #{source_url} " \ "vouch_url: #{vouch_url}>" end |
#mentioned_urls ⇒ Array<String>
Retrieve unique URLs mentioned by this client’s source URL.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/webmention/client.rb', line 61 def mentioned_urls response = source_url.response urls = Set.new( self.class .registered_parsers[response.mime_type] .new(response.body, response.uri) .results ) urls.reject! { |url| url.match(/^#{response.uri}(?:#.*)?$/) } urls.to_a.sort end |
#send_webmention(target) ⇒ Response, ErrorResponse
Send a webmention from this client’s source URL to a target URL.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/webmention/client.rb', line 86 def send_webmention(target) target_url = Url.new(target) # A Webmention endpoint exists. Send the request and return the response. if target_url.webmention_endpoint? return Request.post(target_url.webmention_endpoint, **(target)) end # An error was encountered fetching the target URL. Return the response. return target_url.response unless target_url.response.ok? # No Webmention endpoint exists. Return a new ErrorResponse. ErrorResponse.new("No webmention endpoint found for target URL #{target}", target_url.response.request) end |
#send_webmentions(*targets) ⇒ Array<Response, ErrorResponse>
Send webmentions from this client’s source URL to multiple target URLs.
112 113 114 |
# File 'lib/webmention/client.rb', line 112 def send_webmentions(*targets) targets.map { |target| send_webmention(target) } end |
#verify_webmention(target) ⇒ Verification
Verify that this client’s source URL links to a target URL.
124 125 126 |
# File 'lib/webmention/client.rb', line 124 def verify_webmention(target) Verification.new(source_url, Url.new(target), vouch_url: vouch_url) end |