Class: Change::Resources::MemberResource

Inherits:
Resource
  • Object
show all
Defined in:
lib/resources/member_resource.rb

Direct Known Subclasses

Organization, Petition, User

Instance Attribute Summary collapse

Attributes inherited from Resource

#client

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#auth_key, #auth_key=, #endpoint, #make_request, #needs_authorization?, #needs_request_signature?, #request_auth_key

Constructor Details

#initialize(client, properties = {}) ⇒ MemberResource

Returns a new instance of MemberResource.



33
34
35
36
# File 'lib/resources/member_resource.rb', line 33

def initialize(client, properties = {})
  @id = properties.delete(:id) || properties.delete("#{self.class.member_name}_id")
  super(client, properties)
end

Instance Attribute Details

#idObject

The unique Change.org ID of the resource.



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

def id
  @id
end

#propertiesObject

The fields on the resource. The Change.org API documentation has the full list of fields that may be returned for each resource.



31
32
33
# File 'lib/resources/member_resource.rb', line 31

def properties
  @properties
end

Class Method Details

.collection_nameString

This is the Change.org pluralized name for the resource type. While it can be overridden in sub-classes for non-standard English pluralizations, it is automatically derived from the class name.

Returns:

  • (String)

    the pluralized name of the resource



20
21
22
# File 'lib/resources/member_resource.rb', line 20

def collection_name
  "#{self.name.split('::').last.downcase}s"
end

.member_nameString

This is the Change.org name for the resource type. It is automatically derived from the class name.

Returns:

  • (String)

    the name of the resource



11
12
13
# File 'lib/resources/member_resource.rb', line 11

def member_name
  self.name.split('::').last.downcase
end

Instance Method Details

#get_id(resource_url) ⇒ Integer

Retrieves the unique Change.org ID for the current resource by its resource current URL.

Parameters:

  • resource_url (String)

    the current Change.org URL of the resource

Returns:

  • (Integer)

    the unique Change.org ID of the resource



45
46
47
48
# File 'lib/resources/member_resource.rb', line 45

def get_id(resource_url)
  response = make_request(:collection, { :method => :get, :action => :get_id }, { "#{self.class.member_name}_url".to_sym => resource_url })
  response["#{self.class.member_name}_id"]
end

#load(resource_id_or_url = nil, params = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/resources/member_resource.rb', line 50

def load(resource_id_or_url = nil, params = {})
  if resource_id_or_url.is_a?(Integer)
    @id = resource_id_or_url
  elsif resource_id_or_url.is_a?(String)
    @id = get_id(resource_id_or_url)
  end
  raise "Missing resource ID." if @id.nil?
  response = make_request(:member, { :method => :get }, params)
  response.delete("#{self.class.member_name}_id")
  @properties = response
end

#load_collection(collection, params = {}) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/resources/member_resource.rb', line 62

def load_collection(collection, params = {})
  response = make_request(:member, { :method => :get, :collection => collection }, params)
  if response.is_a?(Array)
    self.send(collection).collection = response
  else  
    self.send(collection).collection = response[collection.to_s]
  end
end