Class: Restfully::Link

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

Constant Summary collapse

VALID_RELATIONSHIPS =
%w{member parent collection self alternate next}
RELATIONSHIPS_REQUIRING_TITLE =
%w{collection member}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Link

Returns a new instance of Link.



10
11
12
13
14
15
16
# File 'lib/restfully/link.rb', line 10

def initialize(attributes = {})
  @rel = attributes['rel']
  @title = attributes['title']
  @href = URI.parse(attributes['href'].to_s)
  @resolvable = attributes['resolvable'] || false
  @resolved = attributes['resolved'] || false
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



8
9
10
# File 'lib/restfully/link.rb', line 8

def errors
  @errors
end

#hrefObject (readonly)

Returns the value of attribute href.



8
9
10
# File 'lib/restfully/link.rb', line 8

def href
  @href
end

#relObject (readonly)

Returns the value of attribute rel.



8
9
10
# File 'lib/restfully/link.rb', line 8

def rel
  @rel
end

#titleObject (readonly)

Returns the value of attribute title.



8
9
10
# File 'lib/restfully/link.rb', line 8

def title
  @title
end

Instance Method Details

#resolvable?Boolean

Returns:

  • (Boolean)


18
# File 'lib/restfully/link.rb', line 18

def resolvable?; @resolvable == true; end

#resolved?Boolean

Returns:

  • (Boolean)


19
# File 'lib/restfully/link.rb', line 19

def resolved?; @resolved == true; end

#self?Boolean

Returns:

  • (Boolean)


20
# File 'lib/restfully/link.rb', line 20

def self?; @rel == 'self'; end

#valid?Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/restfully/link.rb', line 22

def valid?
  @errors = []
  if href.nil?
    errors << "href cannot be nil."
  end
  unless VALID_RELATIONSHIPS.include?(rel)
    errors << "#{rel} is not a valid link relationship."
  end
  if (!title || title.empty?) && RELATIONSHIPS_REQUIRING_TITLE.include?(rel)
    errors << "#{rel} #{href} has no title."
  end
  errors.empty?
end