Class: Restfully::Link

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Link

Returns a new instance of Link.



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

def initialize(attributes = {})
  attributes = attributes.symbolize_keys
  @rel = attributes[:rel]
  @title = attributes[:title] || @rel
  @href = URI.parse(attributes[:href].to_s)
  @type = attributes[:type]
  @id = attributes[:id]
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



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

def errors
  @errors
end

#hrefObject (readonly)

Returns the value of attribute href.



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

def href
  @href
end

#relObject (readonly)

Returns the value of attribute rel.



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

def rel
  @rel
end

#titleObject (readonly)

Returns the value of attribute title.



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

def title
  @title
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#idObject



34
35
36
# File 'lib/restfully/link.rb', line 34

def id
  title.to_s.downcase.gsub(/[^a-z]/,'_').squeeze('_').to_sym
end

#media_typeObject



30
31
32
# File 'lib/restfully/link.rb', line 30

def media_type
  @media_type ||= MediaType.find(type)
end

#self?Boolean

Returns:

  • (Boolean)


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

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

#typesObject



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

def types
  (type || "").split(";")
end

#valid?Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
28
# File 'lib/restfully/link.rb', line 22

def valid?
  @errors = []
  if href.nil?
    errors << "href cannot be nil"
  end
  errors.empty?
end