Class: Webmention::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/webmention/client.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, vouch: nil) ⇒ Webmention::Client

Create a new Webmention::Client.

Examples:

Webmention::Client.new('https://jgarber.example/posts/100')
Webmention::Client.new('https://jgarber.example/posts/100', vouch: 'https://tantek.example/notes/1')

Parameters:

  • source (String, HTTP::URI, #to_s)

    An absolute URL representing a source document.

  • vouch (String, HTTP::URI, #to_s) (defaults to: nil)

    An absolute URL representing a document vouching for the source document. See indieweb.org/Vouch for additional details.



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_parsersObject (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_urlWebmention::Url (readonly)

Returns:



13
14
15
# File 'lib/webmention/client.rb', line 13

def source_url
  @source_url
end

#vouch_urlWebmention::Url (readonly)

Returns:



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

#inspectString

:nocov:

Returns:

  • (String)


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_urlsArray<String>

Retrieve unique URLs mentioned by this client’s source URL.

Examples:

client = Webmention::Client.new('https://jgarber.example/posts/100')
client.mentioned_urls

Returns:

  • (Array<String>)

Raises:

  • (NoMethodError)

    Raised when response is a Webmention::ErrorResponse or response is of an unsupported MIME type.



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.

Examples:

client = Webmention::Client.new('https://jgarber.example/posts/100')
client.send_webmention('https://aaronpk.example/notes/1')

Parameters:

  • target (String, HTTP::URI, #to_s)

    An absolute URL representing a target document.

Returns:



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, **request_options_for(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.

Examples:

client = Webmention::Client.new('https://jgarber.example/posts/100')
targets = ['https://aaronpk.example/notes/1', 'https://adactio.example/notes/1']
client.send_webmentions(targets)

Parameters:

  • targets (Array<String, HTTP::URI, #to_s>)

    An array of absolute URLs representing multiple target documents.

Returns:



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.

Parameters:

  • target (String, HTTP::URI, #to_s)

    An absolute URL representing a target document.

Returns:

Raises:

  • (NoMethodError)

    Raised when response is a Webmention::ErrorResponse or response is of an unsupported MIME type.



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