Class: LinkHeaders::Link

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

Overview

LinkHeader::Link represnts an HTTP Link Header, an HTML LinkHeader, or a LinkSet Link.

#anchor, #href, and #relation are all guaranteed to return a value. Other methods are dynamically created based on what key/value pairs exist in the link for example, if “‘type’: ‘text/html’” exists in the link description, then the method #type will be available on the Link object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(responsepart:, factory:, href:, anchor:, relation:, **kwargs) ⇒ Link

Create the Link object

Parameters:

  • responsepart (Symbol)

    :header, :body, :linkset

  • factory (LinkHeader::LinkFactory)

    the factory that made the link

  • href (String)

    The URL of the Link

  • anchor (String)

    The URL of the anchor

  • relation (String)

    the Link relation (e.g. “cite-as”)

  • **kwargs (hash)

    The remaining facets of the link (e.g. type => ‘text/html’)



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/linkheaders/link.rb', line 170

def initialize(responsepart:, factory:, href:, anchor:, relation:, **kwargs)
  # warn "incoming kw args #{kwargs}"
  @href = href
  @anchor = anchor
  @relation = relation
  @factory = factory
  @responsepart = responsepart
  @linkmethods = Array.new

  kwargs.each do |k, v|
    # warn "key #{k} val #{v}"

    @linkmethods << k
    define_singleton_method(k.to_sym) {
      value = instance_variable_get("@#{k}")
      return value
    } 
    define_singleton_method "#{k}=".to_sym do |val|
      instance_variable_set("@#{k}", val)
      return "@#{k}".to_sym
    end
    # warn "methods:  #{self.methods - Object.new.methods}"
    self.send("#{k}=", v)
  end
end

Instance Attribute Details

#anchorString

Returns URL of the Link anchor.

Returns:

  • (String)

    URL of the Link anchor



147
148
149
# File 'lib/linkheaders/link.rb', line 147

def anchor
  @anchor
end

#factoryLinkHeader::LinkFactory

Returns The factory that made the Link.

Returns:

  • (LinkHeader::LinkFactory)

    The factory that made the Link



153
154
155
# File 'lib/linkheaders/link.rb', line 153

def factory
  @factory
end

#hrefString

Returns URL of the Link.

Returns:

  • (String)

    URL of the Link



149
150
151
# File 'lib/linkheaders/link.rb', line 149

def href
  @href
end

#linkmethodsString

Returns the list of instance method names auto-generated by the various key/value pairs in the link header. e.g. “type”.

Returns:

  • (String)

    the list of instance method names auto-generated by the various key/value pairs in the link header. e.g. “type”



157
158
159
# File 'lib/linkheaders/link.rb', line 157

def linkmethods
  @linkmethods
end

#relationString

Returns What is the relation? (e.g. “cite-as”).

Returns:

  • (String)

    What is the relation? (e.g. “cite-as”)



151
152
153
# File 'lib/linkheaders/link.rb', line 151

def relation
  @relation
end

#responsepartSymbol

Returns :header, :body, or :linkset indicating the place the Link object originated.

Returns:

  • (Symbol)

    :header, :body, or :linkset indicating the place the Link object originated



155
156
157
# File 'lib/linkheaders/link.rb', line 155

def responsepart
  @responsepart
end

Instance Method Details

#to_htmlString

Create an HTML version of the link

Returns:

  • (String)

    HTML version of the Link object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/linkheaders/link.rb', line 200

def to_html
  methods = self.linkmethods
  href = self.href
  rel = self.relation
  anchor = self.anchor
  properties = []
  methods.each do |method|
    value = self.send(method)
    properties << [method, value]
  end
  properties << ["rel", rel]
  properties << ["anchor", anchor]
  LinkHeader::Link.new(href, properties).to_html
end