Class: HaveAPI::Client::ResourceInstance

Inherits:
Resource
  • Object
show all
Defined in:
lib/haveapi/client/resource_instance.rb

Overview

Instance of an object from the API. An instance of this class may be in three states:

  • resolved/persistent - the instance was created by an action that retrieved it from the API.

  • unresolved - this instance is an attribute of another instance that was resolved and will be resolved when first accessed.

  • not persistent - created by Resource.new, the object was not yet sent to the API.

Instance Attribute Summary

Attributes inherited from Resource

#actions, #prepared_args, #resources

Instance Method Summary collapse

Methods inherited from Resource

#_description, #_name, #inspect, #setup, #setup_from_clone

Constructor Details

#initialize(client, api, resource, action: nil, response: nil, resolved: false, meta: nil, persistent: true) ⇒ ResourceInstance

Returns a new instance of ResourceInstance.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/haveapi/client/resource_instance.rb', line 12

def initialize(client, api, resource, action: nil, response: nil,
               resolved: false, meta: nil, persistent: true)
  super(client, api, resource._name)

  @action = action
  @resource = resource
  @resolved = resolved
  @meta = meta
  @persistent = persistent
  @resource_instances = {}

  if response
    if response.is_a?(Hash)
      @params = response
      @meta ||= response[:_meta] || {}

    else
      @response = response
      @params = response.response
      @meta ||= response.meta || {}
    end

    @prepared_args = path_params_from(@meta)
    setup_from_clone(resource)
    define_attributes
  end

  return if @persistent

  setup_from_clone(resource)
  define_implicit_attributes
  define_attributes(@action.input_params)
end

Instance Method Details

#api_responseObject

Return Response object which created this instance.



96
97
98
# File 'lib/haveapi/client/resource_instance.rb', line 96

def api_response
  @response
end

#attributesObject

Return a hash of all object attributes retrieved from the API.



101
102
103
# File 'lib/haveapi/client/resource_instance.rb', line 101

def attributes
  @params
end

#newObject

Raises:

  • (NoMethodError)


46
47
48
# File 'lib/haveapi/client/resource_instance.rb', line 46

def new
  raise NoMethodError
end

#resolveObject

Resolve the object (fetch it from the API) if it is not resolved yet.

Raises:

  • (ArgumentError)


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/haveapi/client/resource_instance.rb', line 78

def resolve
  return self if @resolved

  @action.provide_args(*path_params_from(@meta))
  raise ArgumentError, 'one or more object ids missing' if @action.unresolved_args?

  @response = Response.new(@action, @action.execute({}))
  @params = @response.response

  setup_from_clone(@resource)
  define_attributes

  @prepared_args = path_params_from(@response.meta)
  @resolved = true
  self
end

#saveObject

Invoke create action if the object is not persistent, update action if it is.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/haveapi/client/resource_instance.rb', line 52

def save
  if @persistent
    method(:update).call

  else
    @action.provide_args
    @response = Response.new(@action, @action.execute(attributes_for_api(@action)))

    return nil unless @response.ok?

    @params = @response.response
    define_attributes

    @persistent = true
    self
  end
end

#save!Object

Call #save and raise ActionFailed if it fails.

Raises:



71
72
73
74
75
# File 'lib/haveapi/client/resource_instance.rb', line 71

def save!
  raise ActionFailed, @response if save.nil?

  self
end

#to_sObject



105
106
107
# File 'lib/haveapi/client/resource_instance.rb', line 105

def to_s
  "<#{self.class}:#{object_id}:#{@resource._name}>"
end