Class: LinkHeaders::LinkFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/linkheaders/link.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(default_anchor: 'https://example.org/') ⇒ LinkFactory

Create the LinkFacgtory Object

Parameters:

  • default_anchor (String) (defaults to: 'https://example.org/')

    The URL to be used as the default anchor for a link when it isn’t specified



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

retrieve all known LinkHeader::Link objects

Returns:

  • (Array)

    Array of all LinkHeader::Link objects created by the factory so far



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.

Returns:

  • (<String>)

    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

#warningsArray

Returns An array of strings containing any warnings that were encountered when creating the link (e.g. duplicate cite-as but non-identical URLs).

Returns:

  • (Array)

    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

Extracts the LinkHeader::Link ojects that originated in the HTML Link Headers

Returns:

  • (Array)

    Array of LinkHeader::Link objects



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

Extracts the LinkHeader::Link ojects that originated in the HTTP Headers

Returns:

  • (Array)

    Array of LinkHeader::Link objects



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

Extracts the LinkHeader::Link ojects that originated from a LinkSet

Returns:

  • (Array)

    Array of LinkHeader::Link objects



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

#linksetsArray

Extracts Linkset type links from a list of LinkHeader::Link objects

Returns:

  • (Array)

    Array of LinkHeader::Link objects that represent URLs of LinkSets.



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

Create a new LinkHeader::Link object

Parameters:

  • responsepart (Symbol)

    either :header, :body, or :linkset as the original location of this Link

  • href (String)

    the URL of the link

  • relation (String)

    the string of the relation type (e.g. “cite-as” or “described-by”)

  • anchor (String) (defaults to: @default_anchor)

    The URL of the anchor. Defaults to the default anchor of the LinkHeader factory

  • **kwargs (Hash)

    All other facets of the link. e.g. ‘type’ => ‘text/html’,…

Returns:

  • (LinkHeader::Link)

    The Link object just created



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