Class: ActsAsIcontact::Subresource

Inherits:
Resource
  • Object
show all
Defined in:
lib/acts_as_icontact/subresource.rb

Overview

A read-only resource placed beneath the URL of another resource and containing secondary information.

Because of this intrinsic association, the usual #find methods don’t work; subresources must be obtained using an association method from the parent resource.

Property updates and saving are also prohibited (returning a ReadOnlyError exception.)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#==, #connection, #error, #errors, find_by_id, #id, #inspect, #method_missing, #new_record?, #property_names, #save, #save!

Constructor Details

#initialize(properties = {}) ⇒ Subresource

Should only be called by ResourceCollection. Raises an exception if a parent object is not passed.



10
11
12
13
# File 'lib/acts_as_icontact/subresource.rb', line 10

def initialize(properties={})
  @parent = properties.delete(:parent) or raise ActsAsIcontact::ValidationError, "#{self.class.readable_name} requires a #{self.class.readable_parent}" 
  super
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ActsAsIcontact::Resource

Instance Attribute Details

#parentObject (readonly)

Returns the value of attribute parent.



7
8
9
# File 'lib/acts_as_icontact/subresource.rb', line 7

def parent
  @parent
end

Class Method Details

.cannot_query(*arguments) ⇒ Object Also known as: all, first, find

Replace all search methods with an exception



29
30
31
# File 'lib/acts_as_icontact/subresource.rb', line 29

def cannot_query(*arguments)
  raise ActsAsIcontact::QueryError, "#{readable_name} must be obtained using the #{readable_parent}\##{parent_method} method."
end

.scoped_find(parent, options = {}) ⇒ Object

Returns the ContactHistory collection for the passed contact. Takes the usual iContact search parameters.



17
18
19
20
21
22
23
24
# File 'lib/acts_as_icontact/subresource.rb', line 17

def self.scoped_find(parent, options = {})
  query_options = default_options.merge(options)
  validate_options(query_options)
  uri_extension = uri_component + build_query(query_options)
  response = parent.connection[uri_extension].get
  parsed = JSON.parse(response)
  ResourceCollection.new(self, parsed, :parent => parent)
end