Class: Hactor::HAL::Resource

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/hactor/hal/resource.rb

Constant Summary collapse

RESERVED_PROPERTIES =
['_links', '_embedded']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(state, options) ⇒ Resource

Returns a new instance of Resource.



17
18
19
20
21
# File 'lib/hactor/hal/resource.rb', line 17

def initialize(state, options)
  @rel = options.fetch(:rel)
  @context = options.fetch(:context)
  @state = state
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



12
13
14
# File 'lib/hactor/hal/resource.rb', line 12

def context
  @context
end

#embedded_collection_classObject

Returns the value of attribute embedded_collection_class.



13
14
15
# File 'lib/hactor/hal/resource.rb', line 13

def embedded_collection_class
  @embedded_collection_class
end

#http_clientObject (readonly)

Returns the value of attribute http_client.



12
13
14
# File 'lib/hactor/hal/resource.rb', line 12

def http_client
  @http_client
end

Returns the value of attribute link_collection_class.



13
14
15
# File 'lib/hactor/hal/resource.rb', line 13

def link_collection_class
  @link_collection_class
end

#relObject (readonly)

Returns the value of attribute rel.



12
13
14
# File 'lib/hactor/hal/resource.rb', line 12

def rel
  @rel
end

#stateObject (readonly)

Returns the value of attribute state.



12
13
14
# File 'lib/hactor/hal/resource.rb', line 12

def state
  @state
end

Instance Method Details

#embedded_resource(rel) ⇒ Object



46
47
48
# File 'lib/hactor/hal/resource.rb', line 46

def embedded_resource(rel)
  embedded_resources.find(rel)
end

#embedded_resourcesObject



50
51
52
53
# File 'lib/hactor/hal/resource.rb', line 50

def embedded_resources
  @embedded_resources ||= embedded_collection_class.new state['_embedded'],
                                                        parent: self
end

#follow(rel, options = {}) ⇒ Object



23
24
25
# File 'lib/hactor/hal/resource.rb', line 23

def follow(rel, options={})
  http_client.follow link(rel), options_in_context(options)
end


37
38
39
# File 'lib/hactor/hal/resource.rb', line 37

def link(rel, options={})
  links.find(rel)
end


41
42
43
44
# File 'lib/hactor/hal/resource.rb', line 41

def links
  @links ||= link_collection_class.new state['_links'],
                                       parent: self
end

#propertiesObject



31
32
33
34
35
# File 'lib/hactor/hal/resource.rb', line 31

def properties
  @properties ||= state.reject { |k,v|
    RESERVED_PROPERTIES.include? k
  }
end

#traverse(rel, options = {}) ⇒ Object



27
28
29
# File 'lib/hactor/hal/resource.rb', line 27

def traverse(rel, options={})
  http_client.traverse link(rel), options_in_context(options)
end