micropub-endpoint 0.2.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ea41f10e540c64355824dca9221efb99ac2710b5
4
- data.tar.gz: e087dd656a9941c707c35fdf71b75a709c36ce4e
3
+ metadata.gz: d168f8f8420066caefc1659a7100715a30295d42
4
+ data.tar.gz: a70f780105503fe3e2fe1c0a3b4e8628ce8fa618
5
5
  SHA512:
6
- metadata.gz: ffaba208bddb2101b840d2d33d5d3014181899829b3ec995c7ab938837ae47efb6ecb3762b2fea77ed2898ddb64683229e3ab2fe84244bcbd2929a14aafb05e8
7
- data.tar.gz: 9e82e30e261a900ca4496b764d17ea4ee251b6484257b85da878f548337c458f0c6ad1dc8fa26d15366f859746b7bbc4175c87cdca1a9f8e4f84d1fd60845f57
6
+ metadata.gz: '058327ba6c3f67336619a8c9ff516d42b3b0e71946c6892a954af5880e9e0e08a8dc6a0cf68e9ae29908c5b34c315c8f107a1fb3765c53d7f43ef1ac0c186028'
7
+ data.tar.gz: 010edbc4bc7119a9bc69fbe77c7ba12c66c3a559612cd96eaba8d843915e025e6a173313be6ad37d6858d630fd7d10cae64667ee4aaa167d605d6774a0e57922
@@ -1,5 +1,10 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.0.0 / 2018-07-17
4
+
5
+ - Adds new `Micropub::Endpoint::Client` and `Micropub::Endpoint::Response` classes and refactors `Micorpub::Endpoint::Discover` API ([77f9865](https://github.com/jgarber623/micropub-endpoint-ruby/commit/77f9865)).
6
+ - Updates specs ([78d15d0](https://github.com/jgarber623/micropub-endpoint-ruby/commit/78d15d0)) and documentation ([383e6b9](https://github.com/jgarber623/micropub-endpoint-ruby/commit/383e6b9)).
7
+
3
8
  ## 0.2.1 / 2018-07-05
4
9
 
5
10
  - Updates [Absolutely](https://github.com/jgarber623/absolutely) to v1.1.0 ([f6027c3](https://github.com/jgarber623/micropub-endpoint-ruby/commit/f6027c3)).
data/README.md CHANGED
@@ -11,7 +11,7 @@
11
11
 
12
12
  ## Key Features
13
13
 
14
- - Compliant with [Section 5.3](https://www.w3.org/TR/micropub/#endpoint-discovery) of [the W3C's Micropub specification](https://www.w3.org/TR/micropub/).
14
+ - Compliant with [Section 5.3](https://www.w3.org/TR/micropub/#endpoint-discovery) of [the W3C's Micropub Recommendation](https://www.w3.org/TR/micropub/).
15
15
  - Supports Ruby 2.4 and newer.
16
16
 
17
17
  ## Getting Started
@@ -50,7 +50,7 @@ endpoint = Micropub::Endpoint.discover('https://adactio.com')
50
50
  puts endpoint # returns String: 'https://adactio.com/micropub'
51
51
  ```
52
52
 
53
- This example will search `https://adactio.com` for a valid Micropub endpoint in accordance with the rules described in [the W3C's Micropub specification](https://www.w3.org/TR/micropub/#endpoint-discovery). In this case, the program returns a string: `https://adactio.com/micropub`.
53
+ This example will search `https://adactio.com` for a valid Micropub endpoint in accordance with the rules described in [the W3C's Micropub Recommendation](https://www.w3.org/TR/micropub/#endpoint-discovery). In this case, the program returns a string: `https://adactio.com/micropub`.
54
54
 
55
55
  If no endpoint is discovered at the provided URL, the program will return `nil`:
56
56
 
@@ -64,18 +64,15 @@ puts endpoint.nil? # returns Boolean: true
64
64
 
65
65
  ### Advanced Usage
66
66
 
67
- Should the need arise, you may work directly with the `Micropub::Endpoint::Discover` class:
67
+ Should the need arise, you may work directly with the `Micropub::Endpoint::Client` class:
68
68
 
69
69
  ```ruby
70
70
  require 'micropub/endpoint'
71
71
 
72
- discoverer = Micropub::Endpoint::Discover.new('https://adactio.com')
72
+ client = Micropub::Endpoint::Client.new('https://adactio.com')
73
73
 
74
- puts discoverer.url # returns String: 'https://adactio.com'
75
- puts discoverer.endpoint # returns String: 'https://adactio.com/micropub'
76
-
77
- puts discoverer.uri # returns Addressable::URI
78
- puts discoverer.response # returns HTTP::Response
74
+ puts client.response # returns HTTP::Response
75
+ puts client.endpoint # returns String: 'https://adactio.com/micropub'
79
76
  ```
80
77
 
81
78
  ### Exception Handling
@@ -5,12 +5,14 @@ require 'nokogiri'
5
5
 
6
6
  require 'micropub/endpoint/version'
7
7
  require 'micropub/endpoint/error'
8
+ require 'micropub/endpoint/client'
8
9
  require 'micropub/endpoint/discover'
10
+ require 'micropub/endpoint/response'
9
11
 
10
12
  module Micropub
11
13
  module Endpoint
12
14
  def self.discover(url)
13
- Discover.new(url).endpoint
15
+ Client.new(url).endpoint
14
16
  end
15
17
  end
16
18
  end
@@ -0,0 +1,21 @@
1
+ module Micropub
2
+ module Endpoint
3
+ class Client
4
+ def initialize(url)
5
+ raise ArgumentError, "url must be a String (given #{url.class.name})" unless url.is_a?(String)
6
+
7
+ @uri = Addressable::URI.parse(url)
8
+ rescue Addressable::URI::InvalidURIError => error
9
+ raise InvalidURIError, error
10
+ end
11
+
12
+ def endpoint
13
+ @endpoint ||= Discover.new(response).endpoint
14
+ end
15
+
16
+ def response
17
+ @response ||= Response.new(@uri).response
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,11 +1,6 @@
1
1
  module Micropub
2
2
  module Endpoint
3
3
  class Discover
4
- HTTP_HEADERS_OPTS = {
5
- accept: '*/*',
6
- user_agent: 'Micropub Endpoint Discovery (https://rubygems.org/gems/micropub-endpoint)'
7
- }.freeze
8
-
9
4
  # Ultra-orthodox pattern matching allowed values in HTTP Link header `rel` parameter
10
5
  # https://tools.ietf.org/html/rfc8288#section-3.3
11
6
  REGEXP_REG_REL_TYPE_PATTERN = '[a-z\d][a-z\d\-\.]*'.freeze
@@ -18,44 +13,26 @@ module Micropub
18
13
  # https://www.w3.org/TR/micropub/#endpoint-discovery
19
14
  REGEXP_MICROPUB_REL_PATTERN = /(?:;|\s)rel="?(?:#{REGEXP_REG_REL_TYPE_PATTERN}+\s)?micropub(?:\s#{REGEXP_REG_REL_TYPE_PATTERN})?"?/
20
15
 
21
- attr_reader :uri, :url
22
-
23
- def initialize(url)
24
- raise ArgumentError, "url must be a String (given #{url.class.name})" unless url.is_a?(String)
16
+ def initialize(response)
17
+ raise ArgumentError, "response must be an HTTP::Response (given #{response.class.name})" unless response.is_a?(HTTP::Response)
25
18
 
26
- @url = url
27
- @uri = Addressable::URI.parse(url)
28
- rescue Addressable::URI::InvalidURIError => error
29
- raise InvalidURIError, error
19
+ @response = response
30
20
  end
31
21
 
32
22
  def endpoint
33
23
  return unless endpoint_from_http_request
34
24
 
35
- @endpoint ||= Absolutely.to_absolute_uri(base: url, relative: endpoint_from_http_request)
25
+ @endpoint ||= Absolutely.to_absolute_uri(base: @response.uri.to_s, relative: endpoint_from_http_request)
36
26
  rescue Absolutely::InvalidURIError => error
37
27
  raise InvalidURIError, error
38
28
  end
39
29
 
40
- def response
41
- @response ||= HTTP.follow.headers(HTTP_HEADERS_OPTS).timeout(
42
- connect: 10,
43
- read: 10
44
- ).get(uri)
45
- rescue HTTP::ConnectionError => error
46
- raise ConnectionError, error
47
- rescue HTTP::TimeoutError => error
48
- raise TimeoutError, error
49
- rescue HTTP::Redirector::TooManyRedirectsError => error
50
- raise TooManyRedirectsError, error
51
- end
52
-
53
30
  private
54
31
 
55
32
  def endpoint_from_body
56
- return unless response.mime_type == 'text/html'
33
+ return unless @response.mime_type == 'text/html'
57
34
 
58
- doc = Nokogiri::HTML(response.body.to_s)
35
+ doc = Nokogiri::HTML(@response.body.to_s)
59
36
 
60
37
  # Search response body for first `link` element with valid `rel` attribute
61
38
  link_element = doc.css('link[rel~="micropub"][href]').shift
@@ -64,7 +41,7 @@ module Micropub
64
41
  end
65
42
 
66
43
  def endpoint_from_headers
67
- link_headers = response.headers.get('link')
44
+ link_headers = @response.headers.get('link')
68
45
 
69
46
  return unless link_headers
70
47
 
@@ -0,0 +1,29 @@
1
+ module Micropub
2
+ module Endpoint
3
+ class Response
4
+ HTTP_HEADERS_OPTS = {
5
+ accept: '*/*',
6
+ user_agent: 'Micropub Endpoint Discovery (https://rubygems.org/gems/micropub-endpoint)'
7
+ }.freeze
8
+
9
+ def initialize(uri)
10
+ raise ArgumentError, "uri must be an Addressable::URI (given #{uri.class.name})" unless uri.is_a?(Addressable::URI)
11
+
12
+ @uri = uri
13
+ end
14
+
15
+ def response
16
+ @response ||= HTTP.follow.headers(HTTP_HEADERS_OPTS).timeout(
17
+ connect: 10,
18
+ read: 10
19
+ ).get(@uri)
20
+ rescue HTTP::ConnectionError => error
21
+ raise ConnectionError, error
22
+ rescue HTTP::TimeoutError => error
23
+ raise TimeoutError, error
24
+ rescue HTTP::Redirector::TooManyRedirectsError => error
25
+ raise TooManyRedirectsError, error
26
+ end
27
+ end
28
+ end
29
+ end
@@ -1,5 +1,5 @@
1
1
  module Micropub
2
2
  module Endpoint
3
- VERSION = '0.2.1'.freeze
3
+ VERSION = '1.0.0'.freeze
4
4
  end
5
5
  end
@@ -23,7 +23,7 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency 'bundler', '~> 1.16', '>= 1.16.2'
24
24
  spec.add_development_dependency 'rake', '~> 12.3', '>= 12.3.1'
25
25
  spec.add_development_dependency 'rspec', '~> 3.7'
26
- spec.add_development_dependency 'rubocop', '~> 0.57.2'
26
+ spec.add_development_dependency 'rubocop', '~> 0.58.0'
27
27
  spec.add_development_dependency 'rubocop-rspec', '~> 1.27'
28
28
  spec.add_development_dependency 'simplecov', '~> 0.16.1'
29
29
  spec.add_development_dependency 'simplecov-console', '~> 0.4.2'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: micropub-endpoint
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Garber
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-07-05 00:00:00.000000000 Z
11
+ date: 2018-07-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -70,14 +70,14 @@ dependencies:
70
70
  requirements:
71
71
  - - "~>"
72
72
  - !ruby/object:Gem::Version
73
- version: 0.57.2
73
+ version: 0.58.0
74
74
  type: :development
75
75
  prerelease: false
76
76
  version_requirements: !ruby/object:Gem::Requirement
77
77
  requirements:
78
78
  - - "~>"
79
79
  - !ruby/object:Gem::Version
80
- version: 0.57.2
80
+ version: 0.58.0
81
81
  - !ruby/object:Gem::Dependency
82
82
  name: rubocop-rspec
83
83
  requirement: !ruby/object:Gem::Requirement
@@ -230,8 +230,10 @@ files:
230
230
  - README.md
231
231
  - Rakefile
232
232
  - lib/micropub/endpoint.rb
233
+ - lib/micropub/endpoint/client.rb
233
234
  - lib/micropub/endpoint/discover.rb
234
235
  - lib/micropub/endpoint/error.rb
236
+ - lib/micropub/endpoint/response.rb
235
237
  - lib/micropub/endpoint/version.rb
236
238
  - micropub-endpoint.gemspec
237
239
  homepage: https://github.com/jgarber623/micropub-endpoint-ruby