Module: Videojuicer::Resource

Includes:
Configurable, Exceptions, OAuth::ProxyFactory, Inferrable, PropertyRegistry, Relationships::BelongsTo, Types
Included in:
Campaign, Criterion::DateRange, Criterion::Geolocation, Criterion::Request, Criterion::WeekDay, Insert, Presentation, Preset, Seed, User
Defined in:
lib/videojuicer/resource/base.rb,
lib/videojuicer/resource/types.rb,
lib/videojuicer/resource/errors.rb,
lib/videojuicer/resource/taggable.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/has.rb,
lib/videojuicer/resource/relationships/belongs_to.rb

Defined Under Namespace

Modules: ClassMethods, Embeddable, Inferrable, PropertyRegistry, Relationships, Taggable, 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_clean!, #attr_dirty!, #attr_dirty?, #attr_get, #attr_set, #attributes, #attributes=, #clean_dirty_attributes!, #coerce_value, #default_attributes, #dirty_attribute_keys, #dirty_attributes, inherited, #initialize, #invalid_attributes, #returnable_attributes, #set_default_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.



119
120
121
# File 'lib/videojuicer/resource/base.rb', line 119

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:



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

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_committed_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_committed_response(response)
end

#to_json(*args) ⇒ String

Converts model attributes and errors (if any) to a json string

Parameters:

  • args (Array)

Returns:



131
132
133
134
135
136
# File 'lib/videojuicer/resource/base.rb', line 131

def to_json *args
  JSON.generate({
    :attributes => @attributes,
    :errors     => errors
  })
end

#update_attributes(attrs) ⇒ Object

Updates the attributes and saves the record in one go.



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

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_committed_response(response) ⇒ Object

Takes a change-making response from the API and cleans the dirty attributes if it was successful.



78
79
80
81
82
# File 'lib/videojuicer/resource/base.rb', line 78

def validate_committed_response(response)
  result = validate_response(response)
  clean_dirty_attributes! if result
  return result
end

#validate_response(response) ⇒ Object

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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/videojuicer/resource/base.rb', line 85

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