Class: LinkHeaders::LinkFactory
- Inherits:
-
Object
- Object
- LinkHeaders::LinkFactory
- Defined in:
- lib/linkheaders/link.rb
Instance Attribute Summary collapse
-
#all_links ⇒ Array
retrieve all known LinkHeader::Link objects.
-
#default_anchor ⇒ <String>
The HTTP anchor used by default for implicit Links.
-
#warnings ⇒ Array
An array of strings containing any warnings that were encountered when creating the link (e.g. duplicate cite-as but non-identical URLs).
Instance Method Summary collapse
-
#bodylinks ⇒ Array
Extracts the LinkHeader::Link ojects that originated in the HTML Link Headers.
-
#headlinks ⇒ Array
Extracts the LinkHeader::Link ojects that originated in the HTTP Headers.
-
#initialize(default_anchor: 'https://example.org/') ⇒ LinkFactory
constructor
Create the LinkFacgtory Object.
-
#linksetlinks ⇒ Array
Extracts the LinkHeader::Link ojects that originated from a LinkSet.
-
#linksets ⇒ Array
Extracts Linkset type links from a list of LinkHeader::Link objects.
-
#new_link(responsepart:, href:, relation:, anchor: @default_anchor, **kwargs) ⇒ LinkHeader::Link
Create a new LinkHeader::Link object.
- #sanitycheck(link) ⇒ Object
Constructor Details
#initialize(default_anchor: 'https://example.org/') ⇒ LinkFactory
Create the LinkFacgtory Object
16 17 18 19 20 |
# File 'lib/linkheaders/link.rb', line 16 def initialize(default_anchor: 'https://example.org/') @default_anchor = default_anchor @warnings = Array.new @all_links = Array.new end |
Instance Attribute Details
#all_links ⇒ Array
retrieve all known LinkHeader::Link objects
51 52 53 |
# File 'lib/linkheaders/link.rb', line 51 def all_links @all_links end |
#default_anchor ⇒ <String>
Returns the HTTP anchor used by default for implicit Links.
6 7 8 |
# File 'lib/linkheaders/link.rb', line 6 def default_anchor @default_anchor end |
#warnings ⇒ Array
Returns An array of strings containing any warnings that were encountered when creating the link (e.g. duplicate cite-as but non-identical URLs).
8 9 10 |
# File 'lib/linkheaders/link.rb', line 8 def warnings @warnings end |
Instance Method Details
#bodylinks ⇒ Array
Extracts the LinkHeader::Link ojects that originated in the HTML Link Headers
90 91 92 93 94 95 96 97 98 |
# File 'lib/linkheaders/link.rb', line 90 def bodylinks links = Array.new self.all_links.each do |link| # warn "found #{link.relation}" next unless link.responsepart == :body links << link end links end |
#headlinks ⇒ Array
Extracts the LinkHeader::Link ojects that originated in the HTTP Headers
75 76 77 78 79 80 81 82 83 |
# File 'lib/linkheaders/link.rb', line 75 def headlinks links = Array.new self.all_links.each do |link| # warn "found #{link.relation}" next unless link.responsepart == :header links << link end links end |
#linksetlinks ⇒ Array
Extracts the LinkHeader::Link ojects that originated from a LinkSet
105 106 107 108 109 110 111 112 113 |
# File 'lib/linkheaders/link.rb', line 105 def linksetlinks links = Array.new self.all_links.each do |link| # warn "found #{link.relation}" next unless link.responsepart == :linkset links << link end links end |
#linksets ⇒ Array
Extracts Linkset type links from a list of LinkHeader::Link objects
60 61 62 63 64 65 66 67 68 |
# File 'lib/linkheaders/link.rb', line 60 def linksets links = Array.new self.all_links.each do |link| # warn "found #{link.relation}" next unless link.relation == 'linkset' links << link end links end |
#new_link(responsepart:, href:, relation:, anchor: @default_anchor, **kwargs) ⇒ LinkHeader::Link
Create a new LinkHeader::Link object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/linkheaders/link.rb', line 34 def new_link(responsepart:, href:, relation:, anchor: @default_anchor, **kwargs) # warn "creating new link with kw #{kwargs}" if relation.split(/\s/).length > 1 @warnings |= ['WARN: the link relation contains spaces. This is allowed by the standard to indicate multiple relations for the same link, but this MUST be processed before creating a LinkHeaders::Link object!'] end link = LinkHeaders::Link.new(responsepart: responsepart, factory: self, href: href, anchor: anchor, relation: relation, **kwargs) link = sanitycheck(link) # this will add warnings if the link already exists and has a conflict. returns the original of a duplicate self.all_links |= [link] return link end |
#sanitycheck(link) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/linkheaders/link.rb', line 115 def sanitycheck(link) if link.relation == "describedby" and !(link.respond_to? 'type') @warnings |= ['WARN: A describedby link should include a "type" attribute, to know the MIME type of the addressed description'] end self.all_links.each do |l| if l.relation == "cite-as" and link.relation == "cite-as" if l.href != link.href @warnings |= ['WARN: Found conflicting cite-as relations. This should never happen'] end end if l.href == link.href if l.relation != link.relation @warnings |= ['WARN: Found identical hrefs with different relation types. This may be suspicious. Both have been retained'] else @warnings |= ["WARN: found apparent duplicate #{l.relation} #{l.href} EQUALS#{link.href}. Ignoring and returning known link #{l.relation} #{l.href}"] link = l end end end link end |