indieweb-endpoints 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
+ SHA256:
3
+ metadata.gz: 8ea000fd8ce1b67e86979bf4e7b15a92fdadbf6c06930c1592a9bc9004a709f8
4
+ data.tar.gz: 61065c74af761a2284a4df12ca9ace1eff6e0d73880139ecc88a7c5ab0d60c22
5
+ SHA512:
6
+ metadata.gz: a168817ddd7650e4e7bf54e2029427d70e682027f72658607c9c710bbc4646e7438f6b92f36b4a2b758ab1882220b0ea38a36d59e0ac80a3b595e016d0c0218a
7
+ data.tar.gz: 97ca5850e60dbb0a1509809cd2982ec8eea9d3158e15dcfaaf3279eb92509eb738e76bbbdd007a9d925a3879b05ef3b4801932bb284c2d288894a20e00776ad1
data/.editorconfig ADDED
@@ -0,0 +1,14 @@
1
+ # EditorConfig is awesome: https://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
@@ -0,0 +1 @@
1
+ * jgarber623
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/.reek.yml ADDED
@@ -0,0 +1,12 @@
1
+ detectors:
2
+ IrresponsibleModule:
3
+ enabled: false
4
+ TooManyStatements:
5
+ exclude:
6
+ - IndieWeb::Endpoints::Client#response
7
+ UtilityFunction:
8
+ exclude:
9
+ - FixtureHelpers#read_fixture
10
+
11
+ exclude_paths:
12
+ - vendor/
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,26 @@
1
+ require:
2
+ - rubocop-performance
3
+ - rubocop-rspec
4
+
5
+ Metrics/BlockLength:
6
+ Exclude:
7
+ - spec/**/*.rb
8
+
9
+ Metrics/LineLength:
10
+ Enabled: false
11
+
12
+ Naming/RescuedExceptionsVariableName:
13
+ PreferredName: exception
14
+
15
+ RSpec/FilePath:
16
+ Exclude:
17
+ - spec/lib/indieweb/**/*_spec.rb
18
+
19
+ Style/Documentation:
20
+ Enabled: false
21
+
22
+ Style/FrozenStringLiteralComment:
23
+ Enabled: false
24
+
25
+ Style/SymbolArray:
26
+ Enabled: false
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.4.6
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,16 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.4.6
4
+ - 2.5.5
5
+ - 2.6.2
6
+ cache:
7
+ - bundler
8
+ before_install:
9
+ - gem update --system
10
+ - gem install bundler
11
+ before_script:
12
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
13
+ - chmod +x ./cc-test-reporter
14
+ - ./cc-test-reporter before-build
15
+ after_script:
16
+ - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # Changelog
2
+
3
+ ## 0.1.0 / 2019-04-24
4
+
5
+ - Initial release!
@@ -0,0 +1,3 @@
1
+ # Code of Conduct
2
+
3
+ By contributing to and participating in the development of indieweb-endpoints-ruby, you acknowledge that you have read and agree to the [IndieWeb Code of Conduct](https://indieweb.org/code-of-conduct).
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,37 @@
1
+ # Contributing to indieweb-endpoints-ruby
2
+
3
+ There are a couple ways you can help improve indieweb-endpoints-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
+ indieweb-endpoints-ruby is developed using Ruby 2.4.6 and is additionally tested against Ruby 2.5.5 and 2.6.2 using [Travis CI](https://travis-ci.com/indieweb/indieweb-endpoints-ruby).
12
+
13
+ Before making changes to indieweb-endpoints-ruby, you'll want to install Ruby 2.4.6. 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.6 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/indieweb/indieweb-endpoints-ruby/issues
37
+ [pulls]: https://github.com/indieweb/indieweb-endpoints-ruby/pulls
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in indieweb-endpoints.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 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,108 @@
1
+ # indieweb-endpoints-ruby
2
+
3
+ **A Ruby gem for discovering a URL's [IndieAuth](https://indieweb.org/IndieAuth), [Micropub](https://indieweb.org/Micropub), and [Webmention](https://indieweb.org/Webmention) endpoints.**
4
+
5
+ [![Gem](https://img.shields.io/gem/v/indieweb-endpoints.svg?style=for-the-badge)](https://rubygems.org/gems/indieweb-endpoints)
6
+ [![Downloads](https://img.shields.io/gem/dt/indieweb-endpoints.svg?style=for-the-badge)](https://rubygems.org/gems/indieweb-endpoints)
7
+ [![Build](https://img.shields.io/travis/com/indieweb/indieweb-endpoints-ruby/master.svg?style=for-the-badge)](https://travis-ci.com/indieweb/indieweb-endpoints-ruby)
8
+ [![Maintainability](https://img.shields.io/codeclimate/maintainability/indieweb/indieweb-endpoints-ruby.svg?style=for-the-badge)](https://codeclimate.com/github/indieweb/indieweb-endpoints-ruby)
9
+ [![Coverage](https://img.shields.io/codeclimate/c/indieweb/indieweb-endpoints-ruby.svg?style=for-the-badge)](https://codeclimate.com/github/indieweb/indieweb-endpoints-ruby/code)
10
+
11
+ ## Key Features
12
+
13
+ - Compliant with [Section 4.1](https://www.w3.org/TR/indieauth/#discovery-by-clients) and [Section 4.2.2](https://www.w3.org/TR/indieauth/#redirect-url) of [the W3C's IndieAuth Working Group Note](https://www.w3.org/TR/indieauth/), [Section 5.3](https://www.w3.org/TR/micropub/#endpoint-discovery) of [the W3C's Micropub Recommendation](https://www.w3.org/TR/micropub/), and [Section 3.1.2](https://www.w3.org/TR/webmention/#sender-discovers-receiver-webmention-endpoint) of [the W3C's Webmention Recommendation](https://www.w3.org/TR/webmention/).
14
+ - Passes all Endpoint Discovery tests on [webmention.rocks](https://webmention.rocks).
15
+ - Supports Ruby 2.4 and newer.
16
+
17
+ ## Getting Started
18
+
19
+ Before installing and using indieweb-endpoints-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
+ indieweb-endpoints-ruby is developed using Ruby 2.4.6 and is additionally tested against Ruby 2.5.5 and 2.6.2 using [Travis CI](https://travis-ci.com/indieweb/indieweb-endpoints-ruby).
22
+
23
+ ## Installation
24
+
25
+ If you're using [Bundler](https://bundler.io), add indieweb-endpoints-ruby to your project's `Gemfile`:
26
+
27
+ ```ruby
28
+ source 'https://rubygems.org'
29
+
30
+ gem 'indieweb-endpoints'
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 indieweb-endpoints-ruby added to your project's `Gemfile` and installed, you may discover a URL's IndieWeb-relevant endpoints by doing:
44
+
45
+ ```ruby
46
+ require 'indieweb/endpoints'
47
+
48
+ endpoints = IndieWeb::Endpoints.get('https://aaronparecki.com')
49
+
50
+ puts endpoints
51
+ ```
52
+
53
+ This example will search `https://aaronparecki.com` for valid IndieAuth, Micropub, and Webmention endpoints. In this case, the program returns a `Hash`:
54
+
55
+ ```ruby
56
+ {
57
+ authorization_endpoint: 'https://aaronparecki.com/auth',
58
+ micropub: 'https://aaronparecki.com/micropub',
59
+ redirect_uri: nil,
60
+ token_endpoint: 'https://aaronparecki.com/auth/token',
61
+ webmention: 'https://webmention.io/aaronpk/webmention'
62
+ }
63
+ ```
64
+
65
+ Each key in the resulting `Hash` will return either a `String` representing a URL or `nil`. The `redirect_uri` key will return either an `Array` or `nil` since a given URL may register multiple callback URLs.
66
+
67
+ ### Advanced Usage
68
+
69
+ Should the need arise, you may work directly with the `IndieWeb::Endpoints::Client` class:
70
+
71
+ ```ruby
72
+ require 'indieweb/endpoints'
73
+
74
+ client = IndieWeb::Endpoints::Client.new('https://aaronparecki.com')
75
+
76
+ puts client.response # => HTTP::Response
77
+ puts client.endpoints # => Hash
78
+ ```
79
+
80
+ ### Exception Handling
81
+
82
+ There are several exceptions that may be raised by indieweb-endpoints-ruby's underlying dependencies. These errors are raised as subclasses of `StandardError`.
83
+
84
+ From [jgarber623/absolutely](https://github.com/jgarber623/absolutely) and [sporkmonger/addressable](https://github.com/sporkmonger/addressable):
85
+
86
+ - `IndieWeb::Endpoints::InvalidURIError`
87
+
88
+ From [httprb/http](https://github.com/httprb/http):
89
+
90
+ - `IndieWeb::Endpoints::ConnectionError`
91
+ - `IndieWeb::Endpoints::TimeoutError`
92
+ - `IndieWeb::Endpoints::TooManyRedirectsError`
93
+
94
+ ## Contributing
95
+
96
+ Interested in helping improve indieweb-endpoints-ruby? Awesome! Your help is greatly appreciated. See [CONTRIBUTING.md](https://github.com/indieweb/indieweb-endpoints-ruby/blob/master/CONTRIBUTING.md) for details.
97
+
98
+ By contributing to and participating in the development of indieweb-endpoints-ruby, you acknowledge that you have read and agree to the [IndieWeb Code of Conduct](https://indieweb.org/code-of-conduct).
99
+
100
+ ## Acknowledgments
101
+
102
+ indieweb-endpoints-ruby wouldn't exist without IndieAuth, Micropub, and Webmention and the hard work put in by everyone involved in the [IndieWeb](https://indieweb.org) movement. Additionally, the comprehensive Webmention Endpoint Discovery test suite at [webmention.rocks](https://webmention.rocks) was invaluable in the development of this Ruby gem.
103
+
104
+ indieweb-endpoints-ruby is written and maintained by [Jason Garber](https://sixtwothree.org).
105
+
106
+ ## License
107
+
108
+ indieweb-endpoints-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,17 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'reek/rake/task'
4
+ require 'rspec/core/rake_task'
5
+ require 'rubocop/rake_task'
6
+
7
+ Reek::Rake::Task.new do |task|
8
+ task.fail_on_error = false
9
+ end
10
+
11
+ RSpec::Core::RakeTask.new
12
+
13
+ RuboCop::RakeTask.new do |task|
14
+ task.fail_on_error = false
15
+ end
16
+
17
+ task default: [:rubocop, :reek, :spec]
@@ -0,0 +1,37 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ require 'indieweb/endpoints/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.required_ruby_version = ['>= 2.4', '< 2.7']
8
+
9
+ spec.name = 'indieweb-endpoints'
10
+ spec.version = IndieWeb::Endpoints::VERSION
11
+ spec.authors = ['Jason Garber']
12
+ spec.email = ['jason@sixtwothree.org']
13
+
14
+ spec.summary = 'Discover a URL’s IndieAuth, Micropub, and Webmention endpoints.'
15
+ spec.description = spec.summary
16
+ spec.homepage = 'https://github.com/indieweb/indieweb-endpoints-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 'rake', '~> 12.3'
24
+ spec.add_development_dependency 'reek', '~> 5.3'
25
+ spec.add_development_dependency 'rspec', '~> 3.8'
26
+ spec.add_development_dependency 'rubocop', '~> 0.67.2'
27
+ spec.add_development_dependency 'rubocop-performance', '~> 1.1'
28
+ spec.add_development_dependency 'rubocop-rspec', '~> 1.32'
29
+ spec.add_development_dependency 'simplecov', '~> 0.16.1'
30
+ spec.add_development_dependency 'simplecov-console', '~> 0.4.2'
31
+ spec.add_development_dependency 'webmock', '~> 3.5'
32
+
33
+ spec.add_runtime_dependency 'absolutely', '~> 1.1'
34
+ spec.add_runtime_dependency 'addressable', '~> 2.6'
35
+ spec.add_runtime_dependency 'http', '~> 5.0.0.pre'
36
+ spec.add_runtime_dependency 'nokogiri', '~> 1.10'
37
+ end
@@ -0,0 +1,37 @@
1
+ module IndieWeb
2
+ module Endpoints
3
+ class Client
4
+ HTTP_HEADERS_OPTS = {
5
+ accept: '*/*',
6
+ user_agent: 'IndieAuth, Micropub, and Webmention Endpoint Discovery (https://rubygems.org/gems/indieweb-endpoints)'
7
+ }.freeze
8
+
9
+ def initialize(url)
10
+ raise ArgumentError, "url must be a String (given #{url.class.name})" unless url.is_a?(String)
11
+
12
+ @url = Addressable::URI.parse(url)
13
+
14
+ raise ArgumentError, 'url must be an absolute URL (e.g. https://example.com)' unless @url.absolute?
15
+ rescue Addressable::URI::InvalidURIError => exception
16
+ raise InvalidURIError, exception
17
+ end
18
+
19
+ def endpoints
20
+ @endpoints ||= Parsers.registered.transform_values { |parser| parser.new(response).results }
21
+ end
22
+
23
+ def response
24
+ @response ||= HTTP.follow.headers(HTTP_HEADERS_OPTS).timeout(
25
+ connect: 10,
26
+ read: 10
27
+ ).get(@url)
28
+ rescue HTTP::ConnectionError => exception
29
+ raise ConnectionError, exception
30
+ rescue HTTP::TimeoutError => exception
31
+ raise TimeoutError, exception
32
+ rescue HTTP::Redirector::TooManyRedirectsError => exception
33
+ raise TooManyRedirectsError, exception
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,13 @@
1
+ module IndieWeb
2
+ module Endpoints
3
+ class ArgumentError < StandardError; end
4
+
5
+ class ConnectionError < StandardError; end
6
+
7
+ class InvalidURIError < StandardError; end
8
+
9
+ class TimeoutError < StandardError; end
10
+
11
+ class TooManyRedirectsError < StandardError; end
12
+ end
13
+ end
@@ -0,0 +1,15 @@
1
+ module IndieWeb
2
+ module Endpoints
3
+ module Parsers
4
+ class AuthorizationEndpointParser < BaseParser
5
+ Parsers.register(:authorization_endpoint, self)
6
+
7
+ private
8
+
9
+ def identifier
10
+ @identifier ||= :authorization_endpoint
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module IndieWeb
2
+ module Endpoints
3
+ module Parsers
4
+ class MicropubParser < BaseParser
5
+ Parsers.register(:micropub, self)
6
+
7
+ private
8
+
9
+ def identifier
10
+ @identifier ||= :micropub
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,41 @@
1
+ module IndieWeb
2
+ module Endpoints
3
+ module Parsers
4
+ class RedirectUriParser < BaseParser
5
+ Parsers.register(:redirect_uri, self)
6
+
7
+ def results
8
+ return unless results_from_http_request.any?
9
+
10
+ @results ||= results_from_http_request.map { |endpoint| Absolutely.to_absolute_uri(base: @response.uri.to_s, relative: endpoint) }.uniq.sort
11
+ rescue Absolutely::InvalidURIError => exception
12
+ raise InvalidURIError, exception
13
+ end
14
+
15
+ private
16
+
17
+ def identifier
18
+ @identifier ||= :redirect_uri
19
+ end
20
+
21
+ def results_from_body
22
+ link_elements.map { |element| element['href'] } if response_is_html && link_elements.any?
23
+ end
24
+
25
+ def results_from_headers
26
+ return unless link_headers.any?
27
+
28
+ link_headers.map do |header|
29
+ endpoint_match_data = header.match(REGEXP_TARGET_URI_PATTERN)
30
+
31
+ endpoint_match_data[1] if endpoint_match_data
32
+ end
33
+ end
34
+
35
+ def results_from_http_request
36
+ @results_from_http_request ||= [results_from_headers, results_from_body].flatten.compact
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,15 @@
1
+ module IndieWeb
2
+ module Endpoints
3
+ module Parsers
4
+ class TokenEndpointParser < BaseParser
5
+ Parsers.register(:token_endpoint, self)
6
+
7
+ private
8
+
9
+ def identifier
10
+ @identifier ||= :token_endpoint
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,25 @@
1
+ module IndieWeb
2
+ module Endpoints
3
+ module Parsers
4
+ class WebmentionParser < BaseParser
5
+ Parsers.register(:webmention, self)
6
+
7
+ private
8
+
9
+ def identifier
10
+ @identifier ||= :webmention
11
+ end
12
+
13
+ def link_element
14
+ # Return first `a` or `link` element with valid `rel` attribute
15
+ # https://www.w3.org/TR/webmention/#sender-discovers-receiver-webmention-endpoint
16
+ @link_element ||= link_elements.find { |element| %w[a link].include?(element.name) }
17
+ end
18
+
19
+ def link_elements_css_selector
20
+ @link_elements_css_selector ||= %([rel~="#{identifier}"][href])
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,93 @@
1
+ module IndieWeb
2
+ module Endpoints
3
+ module Parsers
4
+ extend Registerable
5
+
6
+ class BaseParser
7
+ # Ultra-orthodox pattern matching allowed values in Link header `rel` parameter
8
+ # https://tools.ietf.org/html/rfc8288#section-3.3
9
+ REGEXP_REG_REL_TYPE_PATTERN = '[a-z\d][a-z\d\-\.]*'.freeze
10
+
11
+ # Liberal pattern matching a string of text between angle brackets
12
+ # https://tools.ietf.org/html/rfc5988#section-5.1
13
+ REGEXP_TARGET_URI_PATTERN = /^<(.*)>;/.freeze
14
+
15
+ def initialize(response)
16
+ raise ArgumentError, "response must be an HTTP::Response (given #{response.class.name})" unless response.is_a?(HTTP::Response)
17
+
18
+ @response = response
19
+ end
20
+
21
+ def results
22
+ return unless results_from_http_request
23
+
24
+ @results ||= Absolutely.to_absolute_uri(base: @response.uri.to_s, relative: results_from_http_request)
25
+ rescue Absolutely::InvalidURIError => exception
26
+ raise InvalidURIError, exception
27
+ end
28
+
29
+ private
30
+
31
+ def discrete_link_headers
32
+ # Split Link headers with multiple values, flatten the resulting array, and strip whitespace
33
+ # https://webmention.rocks/test/19
34
+ @discrete_link_headers ||= @response.headers.get('link').map { |header| header.split(',') }.flatten.map(&:strip)
35
+ end
36
+
37
+ def doc
38
+ @doc ||= Nokogiri::HTML(@response.body.to_s)
39
+ end
40
+
41
+ def link_element
42
+ # Return first `link` element with valid `rel` attribute
43
+ # https://www.w3.org/TR/indieauth/#discovery-1
44
+ # https://www.w3.org/TR/micropub/#endpoint-discovery
45
+ @link_element ||= link_elements.shift
46
+ end
47
+
48
+ def link_elements
49
+ @link_elements ||= doc.css(link_elements_css_selector)
50
+ end
51
+
52
+ def link_elements_css_selector
53
+ @link_elements_css_selector ||= %(link[rel~="#{identifier}"][href])
54
+ end
55
+
56
+ def link_header
57
+ @link_header ||= link_headers.shift
58
+ end
59
+
60
+ def link_headers
61
+ # Reduce Link headers to those with valid `rel` attribute
62
+ @link_headers ||= discrete_link_headers.find_all { |header| header.match?(regexp_rel_paramater_pattern) }
63
+ end
64
+
65
+ def regexp_rel_paramater_pattern
66
+ # Ultra-orthodox pattern matching Link header `rel` parameter including a matching identifier value
67
+ # https://www.w3.org/TR/webmention/#sender-discovers-receiver-webmention-endpoint
68
+ @regexp_rel_paramater_pattern ||= /(?:;|\s)rel="?(?:#{REGEXP_REG_REL_TYPE_PATTERN}+\s)?#{identifier}(?:\s#{REGEXP_REG_REL_TYPE_PATTERN})?"?/
69
+ end
70
+
71
+ def results_from_body
72
+ link_element['href'] if response_is_html && link_element
73
+ end
74
+
75
+ def results_from_headers
76
+ return unless link_header
77
+
78
+ endpoint_match_data = link_header.match(REGEXP_TARGET_URI_PATTERN)
79
+
80
+ return endpoint_match_data[1] if endpoint_match_data
81
+ end
82
+
83
+ def response_is_html
84
+ @response_is_html ||= @response.mime_type == 'text/html'
85
+ end
86
+
87
+ def results_from_http_request
88
+ @results_from_http_request ||= results_from_headers || results_from_body || nil
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,13 @@
1
+ module IndieWeb
2
+ module Endpoints
3
+ module Registerable
4
+ def register(identifier, klass)
5
+ registered[identifier] = klass
6
+ end
7
+
8
+ def registered
9
+ @registered ||= {}
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ module IndieWeb
2
+ module Endpoints
3
+ VERSION = '0.1.0'.freeze
4
+ end
5
+ end
@@ -0,0 +1,25 @@
1
+ require 'absolutely'
2
+ require 'addressable/uri'
3
+ require 'http'
4
+ require 'nokogiri'
5
+
6
+ require 'indieweb/endpoints/version'
7
+ require 'indieweb/endpoints/exceptions'
8
+
9
+ require 'indieweb/endpoints/client'
10
+ require 'indieweb/endpoints/registerable'
11
+
12
+ require 'indieweb/endpoints/parsers'
13
+ require 'indieweb/endpoints/parsers/authorization_endpoint_parser'
14
+ require 'indieweb/endpoints/parsers/micropub_parser'
15
+ require 'indieweb/endpoints/parsers/redirect_uri_parser'
16
+ require 'indieweb/endpoints/parsers/token_endpoint_parser'
17
+ require 'indieweb/endpoints/parsers/webmention_parser'
18
+
19
+ module IndieWeb
20
+ module Endpoints
21
+ def self.get(url)
22
+ Client.new(url).endpoints
23
+ end
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,257 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: indieweb-endpoints
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: 2019-04-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '12.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '12.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: reek
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.8'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.8'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.67.2
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.67.2
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop-performance
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.1'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.1'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop-rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.32'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.32'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.16.1
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 0.16.1
111
+ - !ruby/object:Gem::Dependency
112
+ name: simplecov-console
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 0.4.2
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 0.4.2
125
+ - !ruby/object:Gem::Dependency
126
+ name: webmock
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '3.5'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '3.5'
139
+ - !ruby/object:Gem::Dependency
140
+ name: absolutely
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '1.1'
146
+ type: :runtime
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '1.1'
153
+ - !ruby/object:Gem::Dependency
154
+ name: addressable
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '2.6'
160
+ type: :runtime
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '2.6'
167
+ - !ruby/object:Gem::Dependency
168
+ name: http
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - "~>"
172
+ - !ruby/object:Gem::Version
173
+ version: 5.0.0.pre
174
+ type: :runtime
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - "~>"
179
+ - !ruby/object:Gem::Version
180
+ version: 5.0.0.pre
181
+ - !ruby/object:Gem::Dependency
182
+ name: nokogiri
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - "~>"
186
+ - !ruby/object:Gem::Version
187
+ version: '1.10'
188
+ type: :runtime
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - "~>"
193
+ - !ruby/object:Gem::Version
194
+ version: '1.10'
195
+ description: Discover a URL’s IndieAuth, Micropub, and Webmention endpoints.
196
+ email:
197
+ - jason@sixtwothree.org
198
+ executables: []
199
+ extensions: []
200
+ extra_rdoc_files: []
201
+ files:
202
+ - ".editorconfig"
203
+ - ".github/CODEOWNERS"
204
+ - ".gitignore"
205
+ - ".reek.yml"
206
+ - ".rspec"
207
+ - ".rubocop"
208
+ - ".rubocop.yml"
209
+ - ".ruby-version"
210
+ - ".simplecov"
211
+ - ".travis.yml"
212
+ - CHANGELOG.md
213
+ - CODE_OF_CONDUCT.md
214
+ - CONTRIBUTING.md
215
+ - Gemfile
216
+ - LICENSE
217
+ - README.md
218
+ - Rakefile
219
+ - indieweb-endpoints.gemspec
220
+ - lib/indieweb/endpoints.rb
221
+ - lib/indieweb/endpoints/client.rb
222
+ - lib/indieweb/endpoints/exceptions.rb
223
+ - lib/indieweb/endpoints/parsers.rb
224
+ - lib/indieweb/endpoints/parsers/authorization_endpoint_parser.rb
225
+ - lib/indieweb/endpoints/parsers/micropub_parser.rb
226
+ - lib/indieweb/endpoints/parsers/redirect_uri_parser.rb
227
+ - lib/indieweb/endpoints/parsers/token_endpoint_parser.rb
228
+ - lib/indieweb/endpoints/parsers/webmention_parser.rb
229
+ - lib/indieweb/endpoints/registerable.rb
230
+ - lib/indieweb/endpoints/version.rb
231
+ homepage: https://github.com/indieweb/indieweb-endpoints-ruby
232
+ licenses:
233
+ - MIT
234
+ metadata: {}
235
+ post_install_message:
236
+ rdoc_options: []
237
+ require_paths:
238
+ - lib
239
+ required_ruby_version: !ruby/object:Gem::Requirement
240
+ requirements:
241
+ - - ">="
242
+ - !ruby/object:Gem::Version
243
+ version: '2.4'
244
+ - - "<"
245
+ - !ruby/object:Gem::Version
246
+ version: '2.7'
247
+ required_rubygems_version: !ruby/object:Gem::Requirement
248
+ requirements:
249
+ - - ">="
250
+ - !ruby/object:Gem::Version
251
+ version: '0'
252
+ requirements: []
253
+ rubygems_version: 3.0.3
254
+ signing_key:
255
+ specification_version: 4
256
+ summary: Discover a URL’s IndieAuth, Micropub, and Webmention endpoints.
257
+ test_files: []