Class: Restify::Resource

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/restify/resource.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, response: nil, data: nil, relations: {}) ⇒ Resource

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Resource.



9
10
11
12
13
14
15
# File 'lib/restify/resource.rb', line 9

def initialize(context, response: nil, data: nil, relations: {})
  super(data)

  @context   = context
  @response  = response
  @relations = relations
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



60
61
62
# File 'lib/restify/resource.rb', line 60

def response
  @response
end

Instance Method Details

#_restify_relationsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



86
87
88
# File 'lib/restify/resource.rb', line 86

def _restify_relations
  @relations
end

#_restify_response=(response) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



91
92
93
# File 'lib/restify/resource.rb', line 91

def _restify_response=(response)
  @response = response
end

#followRelation

Follow a LOCATION or CONTEXT-LOCATION header.

Returns:

  • (Relation)

    Relation to follow resource or nil.



66
67
68
# File 'lib/restify/resource.rb', line 66

def follow
  relation :_restify_follow if relation? :_restify_follow
end

#follow!Relation

Follow a LOCATION or CONTEXT-LOCATION header.

rubocop:disable Style/GuardClause

Returns:

  • (Relation)

    Relation to follow resource.

Raises:

  • RuntimeError If nothing to follow.



76
77
78
79
80
81
82
# File 'lib/restify/resource.rb', line 76

def follow!
  if (rel = follow)
    rel
  else
    raise 'Nothing to follow'
  end
end

#inspectObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



96
97
98
99
100
101
102
103
# File 'lib/restify/resource.rb', line 96

def inspect
  text = {
    '@data' => data,
    '@relations' => @relations
  }.map {|k, v| "#{k}=#{v.inspect}" }.join(' ')

  "#<#{self.class} #{text}>"
end

#relation(name) ⇒ Relation Also known as: rel

Return relation with given name.

Parameters:

  • name (String, Symbol)

    Relation name.

Returns:



35
36
37
38
39
40
41
# File 'lib/restify/resource.rb', line 35

def relation(name)
  if @relations.key? name
    Relation.new @context, @relations.fetch(name)
  else
    Relation.new @context, @relations.fetch(name.to_s)
  end
end

#relation?(name) ⇒ Boolean Also known as: rel?, has_rel?, has_relation?

Check if resource has a relation with given name.

Parameters:

  • name (String, Symbol)

    Relation name.

Returns:

  • (Boolean)

    True if resource has relation, false otherwise.



22
23
24
# File 'lib/restify/resource.rb', line 22

def relation?(name)
  @relations.key?(name) || @relations.key?(name.to_s)
end