Class: Animoto::Resources::Base Abstract

Inherits:
Object
  • Object
show all
Includes:
Support::StandardEnvelope
Defined in:
lib/animoto/resources/base.rb

Overview

This class is abstract.

Set #endpoint and maybe override Support::StandardEnvelope::ClassMethods#unpack_standard_envelope to subclass.

Direct Known Subclasses

Jobs::Base, Storyboard, Video

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Support::StandardEnvelope

find_class_for, included

Constructor Details

#initialize(attributes = {}) ⇒ Resources::Base

Creates a new resource.

Parameters:

  • attributes (Hash{String=>Object}) (defaults to: {})

    hash of attributes for this resource

See Also:



95
96
97
# File 'lib/animoto/resources/base.rb', line 95

def initialize attributes = {}
  instantiate attributes
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



88
89
90
# File 'lib/animoto/resources/base.rb', line 88

def errors
  @errors
end

#urlObject (readonly)

Returns the value of attribute url.



88
89
90
# File 'lib/animoto/resources/base.rb', line 88

def url
  @url
end

Class Method Details

.endpoint(path) ⇒ String .endpointString

Overloads:

  • .endpoint(path) ⇒ String

    Sets the endpoint for this class. This is the URL where all requests related to this service will go.

    Parameters:

    • path (String)

      the path to set

    Returns:

    • (String)

      the endpoint

  • .endpointString

    Returns the endpoint for this class.

    Returns:

    • (String)

      the endpoint



19
20
21
22
# File 'lib/animoto/resources/base.rb', line 19

def self.endpoint path = nil
  @endpoint = path if path
  @endpoint
end

.load(body) ⇒ Resources::Base

Makes a new instance of this class from a deserialized response body. Note that it assumes the hash you’re passing is structured correctly and does no format checking at all, so if the hash is not in the “standard envelope”, this method will most likely raise an error.

Parameters:

  • body (Hash{String=>Object})

    the deserialized response body

Returns:



39
40
41
# File 'lib/animoto/resources/base.rb', line 39

def self.load body
  new unpack_standard_envelope(body)
end

.new(attributes = {}) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/animoto/resources/base.rb', line 59

def new attributes = {}
  if attributes[:url] && instances[attributes[:url]]
    instances[attributes[:url]].instantiate attributes
  else
    original_new attributes
  end
end

.original_newResources::Base

If an instance is instantiated with the same unique identifier (i.e. URL) as another, instead of creating a brand new object, will instead update and return the existing object. This way there should only be one object representing any one resource.

Examples:

client = Animoto::Client.new
storyboard1 = Animoto::Resources::Storyboard.new :url => "https://platform.animoto.com/storyboards/1"
storyboard2 = client.find! Animoto::Resources::Storyboard, "https://platform.animoto.com/storyboards/1"
storyboard1.equal?(storyboard2) # => true

Parameters:

  • attributes (Hash{String=>Object})

    a hash of attributes for this resource

Returns:

  • (Resources::Base)

    either a new Resource instance, or an existing one with updated attributes



58
# File 'lib/animoto/resources/base.rb', line 58

alias_method :original_new, :new

.register(instance) ⇒ Object

Registers an instance in the identity map so that subsequent finds or instantiations of this resource with the same URL will return the same object.

Parameters:

Raises:

  • (ArgumentError)

    if the instance isn’t of this class



73
74
75
76
# File 'lib/animoto/resources/base.rb', line 73

def register instance
  raise ArgumentError unless instance.is_a?(self)
  instances[instance.url] = instance
end

Instance Method Details

#endpointString

Returns the endpoint for this class.

Returns:

  • (String)

    the endpoint



27
28
29
# File 'lib/animoto/resources/base.rb', line 27

def endpoint
  self.class.endpoint
end

#instantiate(attributes = {}) ⇒ self

Since resources can be created a number of different ways, this method does the actual attribute setting for a resource, acting much like a public version of #initialize.

Parameters:

  • attributes (Hash{Symbol=>Object}) (defaults to: {})

    hash of attributes for this resource

Returns:

  • (self)

    this instance



115
116
117
118
119
120
# File 'lib/animoto/resources/base.rb', line 115

def instantiate attributes = {}
  @errors = (attributes[:errors] || []).collect { |e| wrap_error e  }      
  @url    = attributes[:url]
  self.class.register(self) if @url
  self
end

#load(body = {}) ⇒ self

Update this instance with new attributes from the response body.

Parameters:

  • body (Hash{String=>Object}) (defaults to: {})

    deserialized from a response body

Returns:

  • (self)

    this instance, updated



104
105
106
# File 'lib/animoto/resources/base.rb', line 104

def load body = {}
  instantiate unpack_standard_envelope(body)
end