Class: Jekyll::WebmentionIO::NetworkClient

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll/network_client.rb

Defined Under Namespace

Modules: HTTPStatus

Constant Summary collapse

EXCEPTIONS =
[
  SocketError, Timeout::Error,
  Errno::EINVAL, Errno::ECONNRESET, Errno::ECONNREFUSED, EOFError,
  Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError,
  OpenSSL::SSL::SSLError,
].freeze

Instance Method Summary collapse

Instance Method Details

#http_get(uri, redirect_limit, original_uri: false) ⇒ Object

A helper function which performs an HTTP GET operate on the target URI while handling 302 redirects (up to a specific limit).

Returns the body if a 200 OK was received or nil if an error occurred (including if the redirect limit was reached).



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/jekyll/network_client.rb', line 47

def http_get(uri, redirect_limit, original_uri: false)
  if !redirect_limit.positive?
    Jekyll::WebmentionIO.log('warn', "too many redirects for #{original_uri}") if original_uri

    return nil
  end

  original_uri ||= uri

  response = perform_http_request(uri)

  case response[:status]
  when HTTPStatus::SUCCESS
    response[:body]

  when HTTPStatus::REDIRECTION
    redirect_to =
      if response[:body].relative?
        "#{original_uri.scheme}://#{original_uri.host}" + response.body.to_s
      else
        response[:body].to_s
      end

    http_get(redirect_to, redirect_limit - 1, original_uri: original_uri)
  end
end

#send_webmention(source, target) ⇒ Object

Send a webmention to the target URL from the source URL indicated.

Returns a response object with a code attribute indicating the numeric HTTP response code, and a body attribute containing the response payload. Webmentions contain no specific spec for what that response must look like, though services like webmention.io return JSON.



29
30
31
# File 'lib/jekyll/network_client.rb', line 29

def send_webmention(source, target)
  Webmention.send_webmention(source, target)
end

#webmention_endpoint(uri) ⇒ Object

For a give URI on a site, returns the specific URI to be used when posting webmentions to that site.

e.g. for “www.site.com” this might return “www.site.com/webmentions



38
39
40
# File 'lib/jekyll/network_client.rb', line 38

def webmention_endpoint(uri)
  IndieWeb::Endpoints.get(uri)[:webmention]
end