Class: DistributedPress::V1::Social::Dereferencer

Inherits:
Object
  • Object
show all
Defined in:
lib/distributed_press/v1/social/dereferencer.rb

Overview

Fetches ActivityStreams from different instances by instantiating clients.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client:) ⇒ Dereferencer

Returns a new instance of Dereferencer.

Parameters:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/distributed_press/v1/social/dereferencer.rb', line 27

def initialize(client:)
  @client = client
  # @todo Replace with a class
  @parser =
    proc do |body, format|
      next HTTParty::Parser.call(body, :html) if body&.start_with? '<'
      next HTTParty::Parser.call(body, format || :plain) unless body&.start_with? '{'

      object = HTTParty::Parser.call(body, :json)

      # @todo Actually validate objects
      case object['type']
      when 'Collection', 'OrderedCollection', 'CollectionPage', 'OrderedCollectionPage'
        Collection.new(object: object, dereferencer: self)
      else
        ReferencedObject.new(object: object, dereferencer: self)
      end
    end
end

Instance Attribute Details

#clientDistributedPress::V1::Social::Client (readonly)



15
16
17
# File 'lib/distributed_press/v1/social/dereferencer.rb', line 15

def client
  @client
end

Class Method Details

._load(client) ⇒ Object



22
23
24
# File 'lib/distributed_press/v1/social/dereferencer.rb', line 22

def self._load(client)
  new(client: Marshal.load(client))
end

Instance Method Details

#_dump(_) ⇒ Object

We only need the client



18
19
20
# File 'lib/distributed_press/v1/social/dereferencer.rb', line 18

def _dump(_)
  Marshal.dump(client)
end

#clients(uri) ⇒ DistributedPress::V1::Social::Client

Gets a client for a URI

Parameters:

  • :uri (Addressable::URI)

Returns:



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/distributed_press/v1/social/dereferencer.rb', line 90

def clients(uri)
  @@clients ||= {}
  @@clients[uri.origin] ||=
    client.class.new(
      url: uri.origin,
      public_key_url: client.public_key_url,
      private_key_pem: client.private_key.to_s,
      logger: client.logger,
      cache_store: client.class.cache_store
    )
end

#get(uri:) ⇒ HTTParty::Response

TODO:

Raise an error if the content-type is not supported?

Fetch a URI, if it’s an HTML page, look for the alternate version so we always have the correct activity.

Parameters:

  • :uri (String, Addressable::URI)

Returns:

  • (HTTParty::Response)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/distributed_press/v1/social/dereferencer.rb', line 53

def get(uri:)
  response = nil

  loop do
    uri = uris(uri)
    client = clients(uri)
    response = client.get(endpoint: uri.request_uri, parser: @parser)

    # Break the loop if the request failed
    break unless response.success?
    # Or if it's not an HTML document
    break unless response.parsed_response.is_a?(::Nokogiri::HTML5::Document)

    link = response.parsed_response.css('link[rel=alternate]').find do |link|
      next unless link['type']
      next true if client.class::ACCEPT.include?(link['type'])

      client.class::ACCEPT.any? do |accept|
        link['type'].start_with? accept
      end
    end

    break if link.nil?
    break if link['href'].nil?
    break if link['href'].empty?

    # Start loop again with the new URI
    uri = link['href']
  end

  response
end

#references(uri) ⇒ DistributedPress::V1::Social::Reference

Gets a reference for a URI and indexes it by the complete and normalized URI

Parameters:

  • :uri (String, Addressable::URI)

Returns:



107
108
109
110
# File 'lib/distributed_press/v1/social/dereferencer.rb', line 107

def references(uri)
  @@references ||= {}
  @@references[uri.to_s] ||= Reference.new(uri: uri.to_s, dereferencer: self)
end

#uris(uri) ⇒ Addressable::URI

Make sure we’re getting a normalized Addressable::URI

Parameters:

  • :uri (String, Addressable::URI)

Returns:

  • (Addressable::URI)


116
117
118
119
120
121
122
123
124
# File 'lib/distributed_press/v1/social/dereferencer.rb', line 116

def uris(uri)
  @@uris ||= {}
  @@uris[uri.to_s] ||=
    (if uri.is_a? Addressable::URI
       uri
     else
       Addressable::URI.parse(uri)
     end).normalize
end