Module: Videojuicer::Resource

Includes:
Configurable, Exceptions, OAuth::ProxyFactory, Inferrable, PropertyRegistry, Relationships::BelongsTo, Types
Included in:
Campaign, Campaign::CampaignPolicy, Presentation, User
Defined in:
lib/videojuicer/resource/base.rb,
lib/videojuicer/resource/types.rb,
lib/videojuicer/resource/errors.rb,
lib/videojuicer/resource/collection.rb,
lib/videojuicer/resource/embeddable.rb,
lib/videojuicer/resource/inferrable.rb,
lib/videojuicer/resource/property_registry.rb,
lib/videojuicer/resource/relationships/belongs_to.rb

Defined Under Namespace

Modules: ClassMethods, Embeddable, Inferrable, PropertyRegistry, Relationships, Types Classes: Collection, Errors

Instance Attribute Summary

Attributes included from Configurable

#local_config

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PropertyRegistry

#attr_get, #attr_set, #attributes, #attributes=, #coerce_value, #default_attributes, inherited, #initialize, #returnable_attributes

Methods included from OAuth::ProxyFactory

#proxy_for

Methods included from Configurable

#api_version, #config, #configure!, #consumer_key, #consumer_secret, #host, #port, #protocol, #scope, #seed_name, #token, #token_secret, #user_id

Class Method Details

.included(base) ⇒ Object



23
24
25
26
27
28
# File 'lib/videojuicer/resource/base.rb', line 23

def self.included(base)
  base.extend(ClassMethods)        
  Inferrable.included(base)
  PropertyRegistry.included(base)
  Relationships::BelongsTo.included(base)
end

Instance Method Details

#destroyObject

Attempts to delete this record. Will raise an exception if the record is new or if it has already been deleted.



112
113
114
# File 'lib/videojuicer/resource/base.rb', line 112

def destroy
  proxy_for(config).delete(resource_path)
end

#errorsObject



74
# File 'lib/videojuicer/resource/base.rb', line 74

def errors; @errors ||= Videojuicer::Resource::Errors.new({}); end

#errors=(arg) ⇒ Object

The hash of errors on this object - keyed by attribute name, with the error description as the value.



73
# File 'lib/videojuicer/resource/base.rb', line 73

def errors=(arg); @errors = Videojuicer::Resource::Errors.new(arg); end

#errors_on(key) ⇒ Object



75
# File 'lib/videojuicer/resource/base.rb', line 75

def errors_on(key); errors.on(key); end

#new_record?Boolean

Determines if this instance is a new record. For the purposes of the SDK, this really means “does the item already have an ID?” - because if i reconstitute a known item without first retrieving it from the API, then saving that object should push changes to the correct resource rather than creating a new object.

Returns:



35
36
37
# File 'lib/videojuicer/resource/base.rb', line 35

def new_record?
  (id.to_i > 0)? false : true
end

#reloadObject

Makes a call to the API for the current attributes on this object. Overwrites the current instance attribute values.

Raises:



104
105
106
107
108
# File 'lib/videojuicer/resource/base.rb', line 104

def reload
  raise NoResource, "Cannot load remote attributes for new records" if new_record?
  response = proxy_for(config).get(resource_path(nil, :nested=>false))
  return validate_response(response)
end

#saveObject

Saves the record, creating it if is new and updating it if it is not. Returns TRUE on success and FALSE on fail.



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

def save 
  proxy = proxy_for(config)
  param_key = self.class.parameter_name
  response =  if new_record?
                proxy.post(resource_path, param_key=>returnable_attributes)
              else
                proxy.put(resource_path, param_key=>returnable_attributes)
              end
              
  # Parse and handle response
  return validate_response(response)
end

#update_attributes(attrs) ⇒ Object

Updates the attributes and saves the record in one go.



97
98
99
100
# File 'lib/videojuicer/resource/base.rb', line 97

def update_attributes(attrs)
  self.attributes = attrs
  return save
end

#valid?Boolean

Attempts to validate this object with the remote service. Returns TRUE on success, and returns FALSE on failure. Failure will also populate the #errors object on this instance.

Returns:



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/videojuicer/resource/base.rb', line 57

def valid?
  proxy = proxy_for(config)
  param_key = self.class.parameter_name
  response =  if new_record?
                proxy.post(resource_path(:validate, :nested=>false), param_key=>returnable_attributes)
              else
                proxy.put(resource_path(:validate, :nested=>false), param_key=>returnable_attributes)
              end
              
  # Parse and handle response
  return validate_response(response)
end

#validate_response(response) ⇒ Object

Takes a response from the API and performs the appropriate actions.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/videojuicer/resource/base.rb', line 78

def validate_response(response)
  body = response.body
  attribs = (body.is_a?(Hash))? body : JSON.parse(body) rescue raise(JSON::ParserError, "Could not parse #{body}: \n\n #{body}")
  attribs.each do |prop, value|
    next if (prop == "id") and (value.to_i < 1)
    self.send("#{prop}=", value) rescue next
  end
  
  if e = attribs["errors"]
    self.errors = e
    return false
  else
    self.id = attribs["id"].to_i
    self.errors = {}
    return true
  end
end