Class: ApiNavigator::Resource

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/api_navigator/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

Class Method 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.



49
50
51
52
53
54
55
# File 'lib/api_navigator/resource.rb', line 49

def initialize(representation, entry_point, response = nil)
  representation = validate(representation)
  links          = representation['_links'] || {}
  @_links        = LinkCollection.new(links, links['curies'], entry_point)
  @_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.



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/api_navigator/resource.rb', line 112

def method_missing(method, *args, &block)
  # if _links.respond_to?(method, include_private)
    if args.any? && args.first.is_a?(Hash)
      return _links.send(method, [], &block)._expand(*args)
    else
      return _links.send(method, *args, &block)
    end
  # end

  super
end

Instance Attribute Details

Returns the links of the Resource as a LinkCollection.



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

def _links
  @_links
end

#_responseObject (readonly)

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



26
27
28
# File 'lib/api_navigator/resource.rb', line 26

def _response
  @_response
end

Class Method Details

.from_representation(representation, entry_point, response = nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/api_navigator/resource.rb', line 32

def self.from_representation(representation, entry_point, response = nil)
  case (representation || {}).fetch('data', "_no_data")
    when Hash
      Resources::MemberResource.from_representation(representation, entry_point, response)
    when Array
      Resources::CollectionResource.new(representation, entry_point, response)
    when "_no_data"
      new(representation, entry_point, response)
    else
      raise InvalidRepresentationError.new("Representation has not valid data element - must be Hash or List", representation)
  end
end

Instance Method Details

#[](name) ⇒ Object



69
70
71
# File 'lib/api_navigator/resource.rb', line 69

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.



104
105
106
# File 'lib/api_navigator/resource.rb', line 104

def _self_link
  @_links['self']
end

#_statusObject



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

def _status
  _response && _response.status
end

#_success?Boolean

Returns:

  • (Boolean)


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

def _success?
  _response && _response.success?
end

#fetch(key, *args) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/api_navigator/resource.rb', line 73

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



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

def inspect
  "#<#{self.class.name} self_link:#{_self_link.inspect} attributes:#{@_attributes.inspect}  collection:#{@_collection.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)


126
127
128
# File 'lib/api_navigator/resource.rb', line 126

def respond_to_missing?(method, include_private = false)
  _links.respond_to?(method, include_private)
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.



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/api_navigator/resource.rb', line 88

def validate(representation)
  return {} unless representation

  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