Class: ShafClient::Link

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

Direct Known Subclasses

Curie

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(href:, templated: false) ⇒ Link

Returns a new instance of Link.



15
16
17
18
# File 'lib/shaf_client/link.rb', line 15

def initialize(href:, templated: false)
  @href = href
  @templated = !!templated
end

Instance Attribute Details

#templatedObject (readonly) Also known as: templated?

Returns the value of attribute templated.



5
6
7
# File 'lib/shaf_client/link.rb', line 5

def templated
  @templated
end

Class Method Details

.from(data) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/shaf_client/link.rb', line 7

def self.from(data)
  if data.is_a? Array
    data.map { |d| new(href: d['href'], templated: d['templated']) }
  else
    new(href: data['href'], templated: data['templated'])
  end
end

Instance Method Details

#hrefObject



22
23
24
# File 'lib/shaf_client/link.rb', line 22

def href
  @href.dup
end

#resolve_templated(**args) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/shaf_client/link.rb', line 26

def resolve_templated(**args)
  return href unless templated?

  args.inject(href) do |uri, (key, value)|
    value = value.to_s.sub(/.+:/, '')
    uri.sub(/{#{key}}/, value)
  end
end

#to_hObject



35
36
37
38
39
40
# File 'lib/shaf_client/link.rb', line 35

def to_h
  {
    href: href,
    templated: templated?
  }
end