Module: Leadlight::Hyperlinkable

Defined in:
lib/leadlight/hyperlinkable.rb

Defined Under Namespace

Classes: LinkSet

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(representation) ⇒ Object



11
12
13
14
15
16
# File 'lib/leadlight/hyperlinkable.rb', line 11

def self.extended(representation)
  super(representation)
  representation.add_link(representation.__location__,
                          'self', 'self', rev: 'self')
  representation.add_links_from_headers
end

Instance Method Details



31
32
33
34
35
36
37
38
39
# File 'lib/leadlight/hyperlinkable.rb', line 31

def add_link(url, rel=nil, title=rel, options={})
  absolute_url = base_url + url
  template     = LinkTemplate.new(__service__, absolute_url, rel,
                                  title,       options)
  link         = template.expand(captures_for_variables(__captures__,
                                                        template.variables))
  define_link_helper(rel) if rel
  links << link
end


49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/leadlight/hyperlinkable.rb', line 49

def add_link_set(rel=nil, helper_name=rel)
  default_link_attributes = {
    rel: rel
  }
  yield.each do |link_attributes|
    attributes = default_link_attributes.merge(link_attributes)
    define_link_set_helper(rel, helper_name) if helper_name
    links << Link.new(__service__,
                      attributes[:href],
                      attributes[:rel],
                      attributes[:title],
                      attributes)
  end
end


41
42
43
44
45
46
47
# File 'lib/leadlight/hyperlinkable.rb', line 41

def add_link_template(template, rel=nil, title=rel, options={})
  absolute_url = base_url + template
  link = LinkTemplate.new(__service__, absolute_url.to_s, rel,
                          title,       options)
  define_link_helper(rel) if rel
  links << link
end


64
65
66
67
68
69
70
# File 'lib/leadlight/hyperlinkable.rb', line 64

def add_links_from_headers
  raw_link_header = __response__.env[:response_headers]['Link']
  link_header = LinkHeader.parse(raw_link_header.to_s)
  link_header.links.each do |link|
    add_link(link.href, link['rel'], link['rel'])
  end
end


26
27
28
29
# File 'lib/leadlight/hyperlinkable.rb', line 26

def link(key, *expand_args, &fallback)
  link = links.at(key, &fallback)
  link && link.expand(*expand_args)
end


18
19
20
21
22
23
24
# File 'lib/leadlight/hyperlinkable.rb', line 18

def links(key=:none)
  if :none == key
    @__links__ ||= LinkSet.new
  else
    links[key]
  end
end