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 ⇒ Webmention::Url readonly
- #vouch_url ⇒ Webmention::Url readonly
Class Method Summary collapse
- .register_parser(klass) ⇒ Object private
Instance Method Summary collapse
-
#initialize(source, vouch: nil) ⇒ Webmention::Client
constructor
Create a new Webmention::Client.
-
#inspect ⇒ String
:nocov:.
-
#mentioned_urls ⇒ Array<String>
Retrieve unique URLs mentioned by this client’s source URL.
-
#send_webmention(target) ⇒ Webmention::Response, Webmention::ErrorResponse
Send a webmention from this client’s source URL to a target URL.
-
#send_webmentions(*targets) ⇒ Array<Webmention::Response, Webmention::ErrorResponse>
Send webmentions from this client’s source URL to multiple target URLs.
-
#verify_webmention(target) ⇒ Webmention::Verification
Verify that this client’s source URL links to a target URL.
Constructor Details
#initialize(source, vouch: nil) ⇒ Webmention::Client
Create a new Webmention::Client.
38 39 40 41 |
# File 'lib/webmention/client.rb', line 38 def initialize(source, vouch: nil) @source_url = Url.new(source) @vouch_url = Url.new(vouch) end |
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 ⇒ Webmention::Url (readonly)
13 14 15 |
# File 'lib/webmention/client.rb', line 13 def source_url @source_url end |
#vouch_url ⇒ Webmention::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:
45 46 47 48 49 |
# File 'lib/webmention/client.rb', line 45 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.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/webmention/client.rb', line 63 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) ⇒ Webmention::Response, Webmention::ErrorResponse
Send a webmention from this client’s source URL to a target URL.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/webmention/client.rb', line 88 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<Webmention::Response, Webmention::ErrorResponse>
Send webmentions from this client’s source URL to multiple target URLs.
114 115 116 |
# File 'lib/webmention/client.rb', line 114 def send_webmentions(*targets) targets.map { |target| send_webmention(target) } end |
#verify_webmention(target) ⇒ Webmention::Verification
Verify that this client’s source URL links to a target URL.
126 127 128 |
# File 'lib/webmention/client.rb', line 126 def verify_webmention(target) Verification.new(source_url, Url.new(target), vouch_url: vouch_url) end |