Class: Hyperclient::Resource

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

Overview

Represents a resource from your API. Its responsability is to ease the way you access its attributes, links and embedded resources.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(representation, entry_point, response = nil) ⇒ Resource

Initializes a Resource.

Parameters:

  • representation

    The hash with the HAL representation of the Resource.

  • entry_point

    The EntryPoint object to inject the configutation.


42
43
44
45
46
47
48
49
50
51
# File 'lib/hyperclient/resource.rb', line 42

def initialize(representation, entry_point, response = nil)
  representation = validate(representation)
  links = representation['_links'] || {}

  @_links       = LinkCollection.new(links, links['curies'], entry_point)
  @_embedded    = ResourceCollection.new(representation['_embedded'], entry_point)
  @_attributes  = Attributes.new(representation)
  @_entry_point = entry_point
  @_response    = response
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)

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.

Delegate the method to various elements of the resource.

This allows `api.posts` instead of `api.links.posts.resource` as well as api.posts(id: 1) assuming posts is a link.


108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/hyperclient/resource.rb', line 108

def method_missing(method, *args, &block)
  if args.any? && args.first.is_a?(Hash)
    _links.send(method, [], &block)._expand(*args)
  elsif !Array.method_defined?(method)
    %i[_attributes _embedded _links].each do |target|
      target = send(target)
      if target.respond_to?(method.to_s)
        return target.send(method, *args, &block)
      end
    end
    super
  end
end

Instance Attribute Details

#_attributesObject (readonly)

Returns the attributes of the Resource as Attributes.


21
22
23
# File 'lib/hyperclient/resource.rb', line 21

def _attributes
  @_attributes
end

#_embeddedObject (readonly)

Returns the embedded resource of the Resource as a ResourceCollection.


28
29
30
# File 'lib/hyperclient/resource.rb', line 28

def _embedded
  @_embedded
end

Returns the links of the Resource as a LinkCollection.


24
25
26
# File 'lib/hyperclient/resource.rb', line 24

def _links
  @_links
end

#_responseObject (readonly)

Returns the response object for the HTTP request that created this resource, if one exists.


32
33
34
# File 'lib/hyperclient/resource.rb', line 32

def _response
  @_response
end

Instance Method Details

#[](name) ⇒ Object


65
66
67
# File 'lib/hyperclient/resource.rb', line 65

def [](name)
  send(name) if respond_to?(name)
end

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 the self Link of the Resource. Used to handle the HTTP methods.


100
101
102
# File 'lib/hyperclient/resource.rb', line 100

def _self_link
  @_links['self']
end

#_statusObject


61
62
63
# File 'lib/hyperclient/resource.rb', line 61

def _status
  _response&.status
end

#_success?Boolean

Returns:

  • (Boolean)

57
58
59
# File 'lib/hyperclient/resource.rb', line 57

def _success?
  _response&.success?
end

#fetch(key, *args) ⇒ Object


69
70
71
72
73
74
75
76
77
78
79
# File 'lib/hyperclient/resource.rb', line 69

def fetch(key, *args)
  return self[key] if respond_to?(key)

  if args.any?
    args.first
  elsif block_given?
    yield key
  else
    raise KeyError
  end
end

#inspectObject


53
54
55
# File 'lib/hyperclient/resource.rb', line 53

def inspect
  "#<#{self.class.name} self_link:#{_self_link.inspect} attributes:#{@_attributes.inspect}>"
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean (private)

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.

Accessory method to allow the resource respond to methods that will hit method_missing.

Returns:

  • (Boolean)

124
125
126
127
128
129
# File 'lib/hyperclient/resource.rb', line 124

def respond_to_missing?(method, include_private = false)
  %i[_attributes _embedded _links].each do |target|
    return true if send(target).respond_to?(method, include_private)
  end
  false
end

#validate(representation) ⇒ Object (private)

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.

Ensures the received representation is a valid Hash-lookalike.


84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/hyperclient/resource.rb', line 84

def validate(representation)
  return {} if representation.nil? || representation.empty?

  if representation.respond_to?(:to_hash)
    representation.to_hash.dup
  else
    raise InvalidRepresentationError.new(
      "Invalid representation for resource (got #{representation.class}, expected Hash). " \
      "Is your web server returning JSON HAL data with a 'Content-Type: application/hal+json' header?",
      representation
    )
  end
end