Class: Webmention::Verification::Client

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

Constant Summary collapse

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Create a client used to determine whether or not source URI links to target URI.

Examples:

source = 'https://source.example.com/post/100'
target = 'https://target.example.com/post/100'

client = Webmention::Verification::Client.new(source, target)

puts client.verified?
#=> TrueClass or FalseClass

Parameters:

  • source (String)
  • target (String)
  • options (Hash)

Options Hash (**options):

  • :strict (Boolean) — default: true

Raises:



38
39
40
41
42
43
44
45
46
47
# File 'lib/webmention/verification/client.rb', line 38

def initialize(source, target, **options)
  @source = source.to_str
  @target = target.to_str
  @options = options

  message = 'must be an absolute URI (e.g. https://example.com/post/100)'

  raise ArgumentError, "source #{message}" unless absolute?(source_uri)
  raise ArgumentError, "target #{message}" unless absolute?(target_uri)
end

Class Attribute Details

.registered_verifiersObject (readonly)

Returns the value of attribute registered_verifiers.



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

def registered_verifiers
  @registered_verifiers
end

Instance Attribute Details

#sourceObject (readonly)

Returns the value of attribute source.



17
18
19
# File 'lib/webmention/verification/client.rb', line 17

def source
  @source
end

#targetObject (readonly)

Returns the value of attribute target.



17
18
19
# File 'lib/webmention/verification/client.rb', line 17

def target
  @target
end

Class Method Details

.register_verifier(klass) ⇒ Object



19
20
21
# File 'lib/webmention/verification/client.rb', line 19

def self.register_verifier(klass)
  klass.mime_types.each { |mime_type| @registered_verifiers[mime_type] = klass }
end

Instance Method Details

#inspectString

Returns:

  • (String)


50
51
52
53
54
55
# File 'lib/webmention/verification/client.rb', line 50

def inspect
  "#<#{self.class.name}:#{format('%#0x', object_id)} " \
    "source: #{source.inspect} " \
    "target: #{target.inspect} " \
    "options: #{@options.inspect}>"
end

#responseHTTP::Response



59
60
61
62
63
64
65
# File 'lib/webmention/verification/client.rb', line 59

def response
  @response ||= HTTP.follow.headers(HTTP_CLIENT_HEADERS).timeout(connect: 10, read: 10).get(source_uri)
rescue HTTP::Error => e
  raise HttpError, e
rescue OpenSSL::SSL::SSLError => e
  raise SSLError, e
end

#source_uriHTTP::URI

Returns:

  • (HTTP::URI)

Raises:



69
70
71
72
73
# File 'lib/webmention/verification/client.rb', line 69

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

#target_uriHTTP::URI

Returns:

  • (HTTP::URI)

Raises:



77
78
79
80
81
# File 'lib/webmention/verification/client.rb', line 77

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

#verified?Boolean



85
86
87
88
89
90
91
92
# File 'lib/webmention/verification/client.rb', line 85

def verified?
  self.class
      .registered_verifiers[response.mime_type]
      .new(response, target, **@options)
      .verified?
rescue NoMethodError
  raise UnsupportedMimeTypeError, "Unsupported MIME Type: #{response.mime_type}"
end