webmention-verification 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 11b5a1f75c0730ed30ff111a03426ab50f695c51
4
+ data.tar.gz: 11488a2681721ec61fdae2c8bbd6b39a3f75e0a1
5
+ SHA512:
6
+ metadata.gz: b5bf6d821e0c880c554d7e8b3bbcd7f58b662aba139174f507bfdd1d92ac39371001bcea1b5cd133c9931e69be795d2605b1eed9367eb7073f1d32989c750d24
7
+ data.tar.gz: d534370598cdb1d17e91121300543a85f3524b839f32b3a961d0a5451a2bb806f09d6a792dfb3fe7f705a5c84d27fdb3532f7bde90d00eee9b89de40f860f4f7
data/.editorconfig ADDED
@@ -0,0 +1,14 @@
1
+ # EditorConfig is awesome: http://EditorConfig.org
2
+ root = true
3
+
4
+ [*]
5
+ charset = utf-8
6
+ end_of_line = lf
7
+ insert_final_newline = true
8
+ indent_size = 2
9
+ indent_style = space
10
+ trim_trailing_whitespace = true
11
+
12
+ [*.md]
13
+ indent_size = 4
14
+ indent_style = tab
data/.gitignore ADDED
@@ -0,0 +1,34 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ # Documentation cache and generated files:
17
+ /.yardoc/
18
+ /_yardoc/
19
+ /doc/
20
+ /rdoc/
21
+
22
+ # Environment normalization:
23
+ /.bundle/
24
+ /vendor/bundle
25
+ /lib/bundler/man/
26
+
27
+ # for a library or gem, you might want to ignore these files since the code is
28
+ # intended to run in multiple environments; otherwise, check them in:
29
+ Gemfile.lock
30
+ # .ruby-version
31
+ # .ruby-gemset
32
+
33
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
34
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --order random
2
+ --require spec_helper
data/.rubocop ADDED
@@ -0,0 +1,3 @@
1
+ --display-style-guide
2
+ --extra-details
3
+ --parallel
data/.rubocop.yml ADDED
@@ -0,0 +1,18 @@
1
+ require:
2
+ - rubocop-rspec
3
+
4
+ Metrics/BlockLength:
5
+ Exclude:
6
+ - spec/**/*.rb
7
+
8
+ Metrics/LineLength:
9
+ Enabled: false
10
+
11
+ RSpec/NestedGroups:
12
+ Max: 4
13
+
14
+ Style/Documentation:
15
+ Enabled: false
16
+
17
+ Style/FrozenStringLiteralComment:
18
+ Enabled: false
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.4.4
data/.simplecov ADDED
@@ -0,0 +1,11 @@
1
+ require 'simplecov-console'
2
+
3
+ formatters = [SimpleCov::Formatter::HTMLFormatter]
4
+
5
+ if RSpec.configuration.files_to_run.length > 1
6
+ formatters << SimpleCov::Formatter::Console
7
+ end
8
+
9
+ SimpleCov.start do
10
+ formatter SimpleCov::Formatter::MultiFormatter.new(formatters)
11
+ end
data/.travis.yml ADDED
@@ -0,0 +1,19 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.4.4
4
+ - 2.5.1
5
+ cache: bundler
6
+ before_script:
7
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
8
+ - chmod +x ./cc-test-reporter
9
+ - ./cc-test-reporter before-build
10
+ script:
11
+ - bundle exec rubocop
12
+ - bundle exec rake
13
+ after_script:
14
+ - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
15
+ notifications:
16
+ email: false
17
+ slack:
18
+ rooms:
19
+ secure: sZ7DfokMwpZOjZ1jHD4RF58PtNGu+knqrv194+WLmfqV+MVPU38L04EGP1c/Kj7rIOtTQnbkDC10TjJNfl+OcRbUKYT19SBN3OqJJbAe5H46nhvNCfISKG54fEpbEMTcOsljSLcKB6FR3o/RiJUorY2BwS3aMCsf8NDhm08Nnxub5abpFvpgiXCfGSRklxt/dfuJmHB0kt3B8EEH3UzuAeaJoFOiV5RYnCUG0z5GXgHeZqZBs5sxmsmQhrDh5o/Yr0FSd9vcHDLD8EgArw//KyV10cF84NSEWtvPpFCcTnMpcfIuzkN/vgLoIbgYbyhczktCXiySHZDOzYBsCngzLN6DjD+5SdQgrrvtUF/Qsi9JUc6ij1EVHhcZjis7x6kGGtPL35nsM3D0KYwTXzlDIfj/RybQpLCaLL78lrkTiL2OnR1CfKXPwS58+sv0raUJOlWJP50WIL6Fe3J4L+3NWNVfpHdU2SV1cP1/UIs+Pwn4LUOwhSUn+rGsG1UKB/ZDezUMLes9PNclOmDVs8GgT7Lkn2N43mIqVQOyeHrUAVvWiI6M8xOTiAbNGP9sjOj4yJzhOwOToP5Id3ts9GvMoayHd8d8cyECQ9dotNh3VlX73p+Yjkz6rEvo/bsXfzx8gmsl0QQ6P7b4afFRwm1GC+nIofm6BNUS6cfg0xPrmMs=
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,37 @@
1
+ # Contributing to webmention-verification-ruby
2
+
3
+ There are a couple ways you can help improve webmention-verification-ruby:
4
+
5
+ 1. Fix an existing [issue][issues] and submit a [pull request][pulls].
6
+ 1. Review open [pull requests][pulls].
7
+ 1. Report a new [issue][issues]. _Only do this after you've made sure the behavior or problem you're observing isn't already documented in an open issue._
8
+
9
+ ## Getting Started
10
+
11
+ webmention-verification-ruby is developed using Ruby 2.4.4 and is additionally tested against Ruby 2.5.1 using [Travis CI](https://travis-ci.com/jgarber623/webmention-verification-ruby).
12
+
13
+ Before making changes to webmention-verification-ruby, you'll want to install Ruby 2.4.4. It's recommended that you use a Ruby version managment tool like [rbenv](https://github.com/rbenv/rbenv), [chruby](https://github.com/postmodern/chruby), or [rvm](https://github.com/rvm/rvm). Once you've installed Ruby 2.4.4 using your method of choice, install the project's gems by running:
14
+
15
+ ```sh
16
+ bundle install
17
+ ```
18
+
19
+ ## Making Changes
20
+
21
+ 1. Fork and clone the project's repo.
22
+ 1. Install development dependencies as outlined above.
23
+ 1. Create a feature branch for the code changes you're looking to make: `git checkout -b my-new-feature`.
24
+ 1. _Write some code!_
25
+ 1. If your changes would benefit from testing, add the necessary tests and verify everything passes by running `bundle exec rspec`.
26
+ 1. Commit your changes: `git commit -am 'Add some new feature or fix some issue'`. _(See [this excellent article](https://chris.beams.io/posts/git-commit/) for tips on writing useful Git commit messages.)_
27
+ 1. Push the branch to your fork: `git push -u origin my-new-feature`.
28
+ 1. Create a new [pull request][pulls] and we'll review your changes.
29
+
30
+ ## Code Style
31
+
32
+ Code formatting conventions are defined in the `.editorconfig` file which uses the [EditorConfig](http://editorconfig.org) syntax. There are [plugins for a variety of editors](http://editorconfig.org/#download) that utilize the settings in the `.editorconfig` file. We recommended you install the EditorConfig plugin for your editor of choice.
33
+
34
+ Your bug fix or feature addition won't be rejected if it runs afoul of any (or all) of these guidelines, but following the guidelines will definitely make everyone's lives a little easier.
35
+
36
+ [issues]: https://github.com/jgarber623/webmention-verification-ruby/issues
37
+ [pulls]: https://github.com/jgarber623/webmention-verification-ruby/pulls
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in webmention-verification.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 Jason Garber
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,170 @@
1
+ # webmention-verification-ruby
2
+
3
+ **A Ruby gem for verifying a received [webmention](https://indieweb.org/Webmention).**
4
+
5
+ [![Gem](https://img.shields.io/gem/v/webmention-verification.svg?style=for-the-badge)](https://rubygems.org/gems/webmention-verification)
6
+ [![Downloads](https://img.shields.io/gem/dt/webmention-verification.svg?style=for-the-badge)](https://rubygems.org/gems/webmention-verification)
7
+ [![Build](https://img.shields.io/travis/com/jgarber623/webmention-verification-ruby/master.svg?style=for-the-badge)](https://travis-ci.com/jgarber623/webmention-verification-ruby)
8
+ [![Dependencies](https://img.shields.io/depfu/jgarber623/webmention-verification-ruby.svg?style=for-the-badge)](https://depfu.com/github/jgarber623/webmention-verification-ruby)
9
+ [![Maintainability](https://img.shields.io/codeclimate/maintainability/jgarber623/webmention-verification-ruby.svg?style=for-the-badge)](https://codeclimate.com/github/jgarber623/webmention-verification-ruby)
10
+ [![Coverage](https://img.shields.io/codeclimate/c/jgarber623/webmention-verification-ruby.svg?style=for-the-badge)](https://codeclimate.com/github/jgarber623/webmention-verification-ruby/code)
11
+
12
+ ## Key Features
13
+
14
+ - Compliant with [Section 3.2.2](https://www.w3.org/TR/webmention/#webmention-verification) of [the W3C's Webmention spcification](https://www.w3.org/TR/webmention/).
15
+ - Supports Ruby 2.4 and newer.
16
+
17
+ ## Getting Started
18
+
19
+ Before installing and using webmention-verification-ruby, you'll want to have [Ruby](https://www.ruby-lang.org) 2.4 (or newer) installed. It's recommended that you use a Ruby version managment tool like [rbenv](https://github.com/rbenv/rbenv), [chruby](https://github.com/postmodern/chruby), or [rvm](https://github.com/rvm/rvm).
20
+
21
+ webmention-verification-ruby is developed using Ruby 2.4.4 and is additionally tested against Ruby 2.5.1 using [Travis CI](https://travis-ci.com/jgarber623/webmention-verification-ruby).
22
+
23
+ ## Installation
24
+
25
+ If you're using [Bundler](https://bundler.io), add webmention-verification-ruby to your project's `Gemfile`:
26
+
27
+ ```ruby
28
+ source 'https://rubygems.org'
29
+
30
+ gem 'webmention-verification'
31
+ ```
32
+
33
+ …and hop over to your command prompt and run…
34
+
35
+ ```sh
36
+ $ bundle install
37
+ ```
38
+
39
+ ## Usage
40
+
41
+ ### Basic Usage
42
+
43
+ With webmention-verification-ruby added to your project's `Gemfile` and installed, you may verify a received webmention by doing:
44
+
45
+ ```ruby
46
+ require 'webmention/verification'
47
+
48
+ source = 'https://source.example.com/post/100'
49
+ target = 'https://target.example.com/post/100'
50
+
51
+ verified = Webmention::Verification.verified?(source, target)
52
+
53
+ puts verified # returns Boolean
54
+ ```
55
+
56
+ This example assumes that you've received a webmention from `https://source.example.com/post/100` (the "source") to your URL, `https://target.example.com/post/100` (the "target"). The above code will return `true` if the source URL links to your target URL and `false` if it doesn't.
57
+
58
+ ### Advanced Usage
59
+
60
+ Should the need arise, you may work directly with the `Webmention::Verification::Client` class:
61
+
62
+ ```ruby
63
+ require 'webmention/verification'
64
+
65
+ source = 'https://source.example.com/post/100'
66
+ target = 'https://target.example.com/post/100'
67
+
68
+ client = Webmention::Verification::Client.new(source, target)
69
+
70
+ puts client.source # returns String: 'https://source.example.com/post/100'
71
+ puts client.target # returns String: 'https://target.example.com/post/100'
72
+
73
+ puts client.source_uri # returns Addressable::URI
74
+ puts client.target_uri # returns Addressable::URI
75
+
76
+ puts client.response # returns HTTP::Response
77
+
78
+ puts client.verified? # Returns Boolean
79
+ ```
80
+
81
+ **By default, webmention-verification-ruby will strictly match URLs.** You may disable strict matching which allows webmention-verification-ruby to match both `http://` and `https://` URLs. This is useful for matching webmentions your website may have received before it was available exclusively via HTTPS.
82
+
83
+ To disable strict mode, pass `strict: false` when instantiating a `Webmention::Verification::Client`:
84
+
85
+ ```ruby
86
+ require 'webmention/verification'
87
+
88
+ source = 'https://source.example.com/post/100'
89
+ target = 'https://target.example.com/post/100'
90
+
91
+ client = Webmention::Verification::Client.new(source, target, strict: false)
92
+
93
+ puts client.verified? # Returns Boolean
94
+ ```
95
+
96
+ The above example will match either `https://source.example.com/post/100` _or_ `http://source.example.com/post/100` in the target URL.
97
+
98
+ ### Verifiers
99
+
100
+ webmention-verification-ruby verifies [HTML](https://www.w3.org/TR/html/), [JSON](https://json.org), and plaintext files in accordance with [Section 3.2.2](https://www.w3.org/TR/webmention/#webmention-verification) of [the W3C's Webmention specification](https://www.w3.org/TR/webmention/):
101
+
102
+ > The receiver **should** use per-media-type rules to determine whether the source document mentions the target URL.
103
+
104
+ In plaintext documents, webmention-verification-ruby will search the source URL for exact matches of the target URL. If the source URL is a JSON document, key/value pairs whose value equals the target URL are matched.
105
+
106
+ HTML documents are searched for a variety of elements and attributes whose values may be (or include) URLs:
107
+
108
+ | Element | Attributes |
109
+ |:-------------|:----------------|
110
+ | `a` | `href` |
111
+ | `area` | `href` |
112
+ | `audio` | `src` |
113
+ | `blockquote` | `cite` |
114
+ | `del` | `cite` |
115
+ | `embed` | `src` |
116
+ | `img` | `src`, `srcset` |
117
+ | `ins` | `cite` |
118
+ | `object` | `data` |
119
+ | `q` | `cite` |
120
+ | `source` | `src`, `srcset` |
121
+ | `track` | `src` |
122
+ | `video` | `src` |
123
+
124
+ You may work directly with webmention-verification-ruby's verifiers by doing:
125
+
126
+ ```ruby
127
+ require 'webmention/verification'
128
+
129
+ response = HTTP.get('https://source.example.com/post/100')
130
+ target = 'https://target.example.com/post/100'
131
+
132
+ verifier = Webmention::Verification::HtmlVerifier.new(response, target)
133
+
134
+ verifier.verified? # returns Boolean
135
+ verifier.results # returns Array
136
+ ```
137
+
138
+ In the example above, `verifier.results` will return an array of HTML elements that link to the provided target URL. An empty array will be returned if no elements linking to the target URL are found in the source URL.
139
+
140
+ ### Exception Handling
141
+
142
+ There are several exceptions that may be raised by webmention-verification-ruby's underlying dependencies. These errors are raised as subclasses of `Webmention::Verification::Error` (which itself is a subclass of `StandardError`).
143
+
144
+ From [sporkmonger/addressable](https://github.com/sporkmonger/addressable):
145
+
146
+ - `Webmention::Verification::InvalidURIError`
147
+
148
+ From [httprb/http](https://github.com/httprb/http):
149
+
150
+ - `Webmention::Verification::ConnectionError`
151
+ - `Webmention::Verification::TimeoutError`
152
+ - `Webmention::Verification::TooManyRedirectsError`
153
+
154
+ webmention-verification-ruby will also raise a `Webmention::Verification::UnsupportedMimeTypeError` when encountering an `HTTP::Response` instance with an unsupported MIME type.
155
+
156
+ ## Contributing
157
+
158
+ Interested in helping improve webmention-verification-ruby? Awesome! Your help is greatly appreciated. See [CONTRIBUTING.md](https://github.com/jgarber623/webmention-verification-ruby/blob/master/CONTRIBUTING.md) for details.
159
+
160
+ ## Acknowledgments
161
+
162
+ webmention-verification-ruby wouldn't exist without Webmention and the hard work put in by everyone involved in the [IndieWeb](https://indieweb.org) movement.
163
+
164
+ The [`LinkExtractor` class](https://github.com/Zegnat/php-linkextractor/blob/master/src/LinkExtractor.php#L32-L51) in [Zegnat](https://github.com/Zegnat)'s [php-linkextractor](https://github.com/Zegnat/php-linkextractor) was invaluable in the devleopment of the `HtmlVerifier` class. Intridea's [Hashie](https://rubygems.org/gems/hashie) gem (specifically the [`DeepLocate` extension](https://github.com/intridea/hashie/blob/master/lib/hashie/extensions/deep_locate.rb)) also provided inspiration for the `JsonVerifier` class.
165
+
166
+ webmention-verification-ruby is written and maintained by [Jason Garber](https://sixtwothree.org).
167
+
168
+ ## License
169
+
170
+ webmention-verification-ruby is freely available under the [MIT License](https://opensource.org/licenses/MIT). Use it, learn from it, fork it, improve it, change it, tailor it to your needs.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task default: :spec
@@ -0,0 +1,21 @@
1
+ require 'addressable/uri'
2
+ require 'http'
3
+ require 'json'
4
+ require 'nokogiri'
5
+
6
+ require 'webmention/verification/version'
7
+ require 'webmention/verification/error'
8
+ require 'webmention/verification/client'
9
+ require 'webmention/verification/verifier'
10
+
11
+ require 'webmention/verification/verifiers/html_verifier'
12
+ require 'webmention/verification/verifiers/json_verifier'
13
+ require 'webmention/verification/verifiers/plaintext_verifier'
14
+
15
+ module Webmention
16
+ module Verification
17
+ def self.verified?(source, target)
18
+ Client.new(source, target).verified?
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,61 @@
1
+ module Webmention
2
+ module Verification
3
+ class Client
4
+ HTTP_HEADERS_OPTS = {
5
+ accept: '*/*',
6
+ user_agent: 'Webmention Verification Client (https://rubygems.org/gems/webmention-verification)'
7
+ }.freeze
8
+
9
+ attr_reader :source, :target
10
+
11
+ def initialize(source, target, strict: true)
12
+ raise ArgumentError, "source must be a String (given #{source.class.name})" unless source.is_a?(String)
13
+ raise ArgumentError, "target must be a String (given #{target.class.name})" unless target.is_a?(String)
14
+
15
+ @source = source
16
+ @target = target
17
+ @strict = strict
18
+
19
+ raise ArgumentError, 'source must be an absolute URI (e.g. https://example.com/post/100)' unless source_uri.absolute?
20
+ raise ArgumentError, 'target must be an absolute URI (e.g. https://example.com/post/100)' unless target_uri.absolute?
21
+ end
22
+
23
+ def response
24
+ @response ||= HTTP.follow.headers(HTTP_HEADERS_OPTS).timeout(
25
+ connect: 10,
26
+ read: 10
27
+ ).get(source_uri)
28
+ rescue HTTP::ConnectionError => error
29
+ raise ConnectionError, error
30
+ rescue HTTP::TimeoutError => error
31
+ raise TimeoutError, error
32
+ rescue HTTP::Redirector::TooManyRedirectsError => error
33
+ raise TooManyRedirectsError, error
34
+ end
35
+
36
+ def source_uri
37
+ @source_uri ||= Addressable::URI.parse(source)
38
+ rescue Addressable::URI::InvalidURIError => error
39
+ raise InvalidURIError, error
40
+ end
41
+
42
+ def target_uri
43
+ @target_uri ||= Addressable::URI.parse(target)
44
+ rescue Addressable::URI::InvalidURIError => error
45
+ raise InvalidURIError, error
46
+ end
47
+
48
+ def verified?
49
+ raise UnsupportedMimeTypeError, "Unsupported MIME Type: #{response.mime_type}" unless verifier_for_mime_type
50
+
51
+ verifier_for_mime_type.new(response, target, strict: @strict).verified?
52
+ end
53
+
54
+ private
55
+
56
+ def verifier_for_mime_type
57
+ @verifier_for_mime_type ||= Verifier.subclasses.find { |verifier| verifier.mime_types.include?(response.mime_type) }
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,17 @@
1
+ module Webmention
2
+ module Verification
3
+ class Error < StandardError; end
4
+
5
+ class ArgumentError < Error; end
6
+
7
+ class ConnectionError < Error; end
8
+
9
+ class InvalidURIError < Error; end
10
+
11
+ class TimeoutError < Error; end
12
+
13
+ class TooManyRedirectsError < Error; end
14
+
15
+ class UnsupportedMimeTypeError < Error; end
16
+ end
17
+ end
@@ -0,0 +1,58 @@
1
+ module Webmention
2
+ module Verification
3
+ class Verifier
4
+ attr_reader :response, :target
5
+
6
+ def initialize(response, target, strict: true)
7
+ raise ArgumentError, "response must be an HTTP::Response (given #{response.class.name})" unless response.is_a?(HTTP::Response)
8
+ raise ArgumentError, "target must be a String (given #{target.class.name})" unless target.is_a?(String)
9
+
10
+ @response = response
11
+ @target = target
12
+ @strict = strict
13
+
14
+ raise UnsupportedMimeTypeError, "Unsupported MIME Type: #{response.mime_type}" unless self.class.mime_types.include?(response.mime_type)
15
+ end
16
+
17
+ def results
18
+ @results ||= parse_response_body
19
+ end
20
+
21
+ def verified?
22
+ results.any?
23
+ end
24
+
25
+ class << self
26
+ def inherited(base)
27
+ subclasses << base
28
+
29
+ super(base)
30
+ end
31
+
32
+ def mime_types
33
+ mime_types = []
34
+
35
+ subclasses.each { |subclass| mime_types << subclass.mime_types }
36
+
37
+ mime_types.flatten.sort
38
+ end
39
+
40
+ def subclasses
41
+ @subclasses ||= []
42
+ end
43
+ end
44
+
45
+ private
46
+
47
+ def response_body
48
+ @response_body ||= response.body.to_s
49
+ end
50
+
51
+ def target_regexp_str
52
+ return target if @strict
53
+
54
+ target.sub(%r{https?://}, 'https?://')
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,48 @@
1
+ module Webmention
2
+ module Verification
3
+ class HtmlVerifier < Verifier
4
+ HTML_ATTRIBUTE_MAPPINGS = {
5
+ cite: %w[blockquote del ins q],
6
+ data: %w[object],
7
+ href: %w[a area],
8
+ poster: %w[video],
9
+ src: %w[audio embed img source track video],
10
+ srcset: %w[img source]
11
+ }.freeze
12
+
13
+ def self.mime_types
14
+ ['text/html']
15
+ end
16
+
17
+ private
18
+
19
+ def doc
20
+ @doc ||= Nokogiri::HTML(response_body)
21
+ end
22
+
23
+ def parse_response_body
24
+ matches = []
25
+
26
+ HTML_ATTRIBUTE_MAPPINGS.each do |attribute, elements|
27
+ elements.each { |element| matches << search_doc(element, attribute) }
28
+ end
29
+
30
+ matches.flatten
31
+ end
32
+
33
+ def search_doc(element, attribute)
34
+ regexp = attribute == :srcset ? srcset_attribute_regexp : target_regexp
35
+
36
+ doc.css("#{element}[#{attribute}]").find_all { |node| node[attribute].match?(regexp) }
37
+ end
38
+
39
+ def srcset_attribute_regexp
40
+ @srcset_attribute_regexp ||= /(?:^|\b)#{target_regexp_str}\s/
41
+ end
42
+
43
+ def target_regexp
44
+ @target_regexp ||= /^#{target_regexp_str}$/
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,35 @@
1
+ module Webmention
2
+ module Verification
3
+ class JsonVerifier < Verifier
4
+ def self.mime_types
5
+ ['application/json']
6
+ end
7
+
8
+ private
9
+
10
+ def comparator(value)
11
+ value.is_a?(String) && value.match?(target_regexp)
12
+ end
13
+
14
+ def locate(object, matches = [])
15
+ if object.is_a?(Enumerable)
16
+ matches << object if object.any? { |_, value| comparator(value) }
17
+
18
+ (object.respond_to?(:values) ? object.values : object.entries).each do |obj|
19
+ locate(obj, matches)
20
+ end
21
+ end
22
+
23
+ matches
24
+ end
25
+
26
+ def parse_response_body
27
+ locate(JSON.parse(response_body))
28
+ end
29
+
30
+ def target_regexp
31
+ @target_regexp ||= /^#{target_regexp_str}$/
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,19 @@
1
+ module Webmention
2
+ module Verification
3
+ class PlaintextVerifier < Verifier
4
+ def self.mime_types
5
+ ['text/plain']
6
+ end
7
+
8
+ private
9
+
10
+ def parse_response_body
11
+ response_body.scan(target_regexp)
12
+ end
13
+
14
+ def target_regexp
15
+ @target_regexp ||= /(?:^|\s)#{target_regexp_str}(?:\s|$)/
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,5 @@
1
+ module Webmention
2
+ module Verification
3
+ VERSION = '0.1.0'.freeze
4
+ end
5
+ end
@@ -0,0 +1,35 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ require 'webmention/verification/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.required_ruby_version = ['>= 2.4', '< 2.6']
8
+
9
+ spec.name = 'webmention-verification'
10
+ spec.version = Webmention::Verification::VERSION
11
+ spec.authors = ['Jason Garber']
12
+ spec.email = ['jason@sixtwothree.org']
13
+
14
+ spec.summary = 'Verify a received Webmention.'
15
+ spec.description = spec.summary
16
+ spec.homepage = 'https://github.com/jgarber623/webmention-verification-ruby'
17
+ spec.license = 'MIT'
18
+
19
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(bin|spec)/}) }
20
+
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.16', '>= 1.16.2'
24
+ spec.add_development_dependency 'rake', '~> 12.3', '>= 12.3.1'
25
+ spec.add_development_dependency 'rspec', '~> 3.7'
26
+ spec.add_development_dependency 'rubocop', '~> 0.58.0'
27
+ spec.add_development_dependency 'rubocop-rspec', '~> 1.27'
28
+ spec.add_development_dependency 'simplecov', '~> 0.16.1'
29
+ spec.add_development_dependency 'simplecov-console', '~> 0.4.2'
30
+ spec.add_development_dependency 'webmock', '~> 3.4', '>= 3.4.2'
31
+
32
+ spec.add_runtime_dependency 'addressable', '~> 2.5', '>= 2.5.2'
33
+ spec.add_runtime_dependency 'http', '~> 3.3'
34
+ spec.add_runtime_dependency 'nokogiri', '~> 1.8', '>= 1.8.4'
35
+ end
metadata ADDED
@@ -0,0 +1,253 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: webmention-verification
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jason Garber
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-07-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.16.2
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '1.16'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.16.2
33
+ - !ruby/object:Gem::Dependency
34
+ name: rake
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '12.3'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 12.3.1
43
+ type: :development
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '12.3'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 12.3.1
53
+ - !ruby/object:Gem::Dependency
54
+ name: rspec
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '3.7'
60
+ type: :development
61
+ prerelease: false
62
+ version_requirements: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - "~>"
65
+ - !ruby/object:Gem::Version
66
+ version: '3.7'
67
+ - !ruby/object:Gem::Dependency
68
+ name: rubocop
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - "~>"
72
+ - !ruby/object:Gem::Version
73
+ version: 0.58.0
74
+ type: :development
75
+ prerelease: false
76
+ version_requirements: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - "~>"
79
+ - !ruby/object:Gem::Version
80
+ version: 0.58.0
81
+ - !ruby/object:Gem::Dependency
82
+ name: rubocop-rspec
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - "~>"
86
+ - !ruby/object:Gem::Version
87
+ version: '1.27'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - "~>"
93
+ - !ruby/object:Gem::Version
94
+ version: '1.27'
95
+ - !ruby/object:Gem::Dependency
96
+ name: simplecov
97
+ requirement: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - "~>"
100
+ - !ruby/object:Gem::Version
101
+ version: 0.16.1
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - "~>"
107
+ - !ruby/object:Gem::Version
108
+ version: 0.16.1
109
+ - !ruby/object:Gem::Dependency
110
+ name: simplecov-console
111
+ requirement: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - "~>"
114
+ - !ruby/object:Gem::Version
115
+ version: 0.4.2
116
+ type: :development
117
+ prerelease: false
118
+ version_requirements: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - "~>"
121
+ - !ruby/object:Gem::Version
122
+ version: 0.4.2
123
+ - !ruby/object:Gem::Dependency
124
+ name: webmock
125
+ requirement: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - "~>"
128
+ - !ruby/object:Gem::Version
129
+ version: '3.4'
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: 3.4.2
133
+ type: :development
134
+ prerelease: false
135
+ version_requirements: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - "~>"
138
+ - !ruby/object:Gem::Version
139
+ version: '3.4'
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: 3.4.2
143
+ - !ruby/object:Gem::Dependency
144
+ name: addressable
145
+ requirement: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - "~>"
148
+ - !ruby/object:Gem::Version
149
+ version: '2.5'
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: 2.5.2
153
+ type: :runtime
154
+ prerelease: false
155
+ version_requirements: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '2.5'
160
+ - - ">="
161
+ - !ruby/object:Gem::Version
162
+ version: 2.5.2
163
+ - !ruby/object:Gem::Dependency
164
+ name: http
165
+ requirement: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - "~>"
168
+ - !ruby/object:Gem::Version
169
+ version: '3.3'
170
+ type: :runtime
171
+ prerelease: false
172
+ version_requirements: !ruby/object:Gem::Requirement
173
+ requirements:
174
+ - - "~>"
175
+ - !ruby/object:Gem::Version
176
+ version: '3.3'
177
+ - !ruby/object:Gem::Dependency
178
+ name: nokogiri
179
+ requirement: !ruby/object:Gem::Requirement
180
+ requirements:
181
+ - - "~>"
182
+ - !ruby/object:Gem::Version
183
+ version: '1.8'
184
+ - - ">="
185
+ - !ruby/object:Gem::Version
186
+ version: 1.8.4
187
+ type: :runtime
188
+ prerelease: false
189
+ version_requirements: !ruby/object:Gem::Requirement
190
+ requirements:
191
+ - - "~>"
192
+ - !ruby/object:Gem::Version
193
+ version: '1.8'
194
+ - - ">="
195
+ - !ruby/object:Gem::Version
196
+ version: 1.8.4
197
+ description: Verify a received Webmention.
198
+ email:
199
+ - jason@sixtwothree.org
200
+ executables: []
201
+ extensions: []
202
+ extra_rdoc_files: []
203
+ files:
204
+ - ".editorconfig"
205
+ - ".gitignore"
206
+ - ".rspec"
207
+ - ".rubocop"
208
+ - ".rubocop.yml"
209
+ - ".ruby-version"
210
+ - ".simplecov"
211
+ - ".travis.yml"
212
+ - CONTRIBUTING.md
213
+ - Gemfile
214
+ - LICENSE
215
+ - README.md
216
+ - Rakefile
217
+ - lib/webmention/verification.rb
218
+ - lib/webmention/verification/client.rb
219
+ - lib/webmention/verification/error.rb
220
+ - lib/webmention/verification/verifier.rb
221
+ - lib/webmention/verification/verifiers/html_verifier.rb
222
+ - lib/webmention/verification/verifiers/json_verifier.rb
223
+ - lib/webmention/verification/verifiers/plaintext_verifier.rb
224
+ - lib/webmention/verification/version.rb
225
+ - webmention-verification.gemspec
226
+ homepage: https://github.com/jgarber623/webmention-verification-ruby
227
+ licenses:
228
+ - MIT
229
+ metadata: {}
230
+ post_install_message:
231
+ rdoc_options: []
232
+ require_paths:
233
+ - lib
234
+ required_ruby_version: !ruby/object:Gem::Requirement
235
+ requirements:
236
+ - - ">="
237
+ - !ruby/object:Gem::Version
238
+ version: '2.4'
239
+ - - "<"
240
+ - !ruby/object:Gem::Version
241
+ version: '2.6'
242
+ required_rubygems_version: !ruby/object:Gem::Requirement
243
+ requirements:
244
+ - - ">="
245
+ - !ruby/object:Gem::Version
246
+ version: '0'
247
+ requirements: []
248
+ rubyforge_project:
249
+ rubygems_version: 2.6.14.1
250
+ signing_key:
251
+ specification_version: 4
252
+ summary: Verify a received Webmention.
253
+ test_files: []