Class: Webmention::Verification::Client

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

Constant Summary collapse

HTTP_HEADERS_OPTS =
{
  accept:     '*/*',
  user_agent: 'Webmention Verification Client (https://rubygems.org/gems/webmention-verification)'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, target, **options) ⇒ Client

Returns a new instance of Client.

Raises:



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/webmention/verification/client.rb', line 11

def initialize(source, target, **options)
  raise ArgumentError, "source must be a String (given #{source.class.name})" unless source.is_a?(String)
  raise ArgumentError, "target must be a String (given #{target.class.name})" unless target.is_a?(String)

  @source = source
  @target = target
  @options = options

  raise ArgumentError, 'source must be an absolute URI (e.g. https://example.com/post/100)' unless source_uri.absolute?
  raise ArgumentError, 'target must be an absolute URI (e.g. https://example.com/post/100)' unless target_uri.absolute?
end

Instance Attribute Details

#sourceObject (readonly)

Returns the value of attribute source.



9
10
11
# File 'lib/webmention/verification/client.rb', line 9

def source
  @source
end

#targetObject (readonly)

Returns the value of attribute target.



9
10
11
# File 'lib/webmention/verification/client.rb', line 9

def target
  @target
end

Instance Method Details

#responseObject



23
24
25
26
27
28
29
# File 'lib/webmention/verification/client.rb', line 23

def response
  @response ||= HTTP.follow.headers(HTTP_HEADERS_OPTS).timeout(connect: 10, read: 10).get(source_uri)
rescue HTTP::ConnectionError,
       HTTP::TimeoutError,
       HTTP::Redirector::TooManyRedirectsError => exception
  raise Webmention::Verification.const_get(exception.class.name.split('::').last), exception
end

#source_uriObject



31
32
33
34
35
# File 'lib/webmention/verification/client.rb', line 31

def source_uri
  @source_uri ||= Addressable::URI.parse(source)
rescue Addressable::URI::InvalidURIError => exception
  raise InvalidURIError, exception
end

#target_uriObject



37
38
39
40
41
# File 'lib/webmention/verification/client.rb', line 37

def target_uri
  @target_uri ||= Addressable::URI.parse(target)
rescue Addressable::URI::InvalidURIError => exception
  raise InvalidURIError, exception
end

#verified?Boolean

Returns:

  • (Boolean)

Raises:



43
44
45
46
47
# File 'lib/webmention/verification/client.rb', line 43

def verified?
  raise UnsupportedMimeTypeError, "Unsupported MIME Type: #{response.mime_type}" unless verifier_for_mime_type

  verifier_for_mime_type.new(response, target, @options).verified?
end