Class: BlueStateDigital::ConstituentGroups
Constant Summary
collapse
- CONSTITUENTS_BATCH_SIZE =
100
Instance Attribute Summary
#connection
Instance Method Summary
collapse
#initialize
Instance Method Details
#add_cons_ids_to_group(cons_group_id, cons_ids, options = {wait_for_result: true}) ⇒ Object
24
25
26
|
# File 'lib/blue_state_digital/constituent_group.rb', line 24
def add_cons_ids_to_group(cons_group_id, cons_ids, options = {wait_for_result: true})
add_or_remove_cons_ids_from_group(:add, cons_group_id, cons_ids, options)
end
|
#create(attrs = {}) ⇒ Object
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/blue_state_digital/constituent_group.rb', line 32
def create(attrs = {})
cons_group = ConstituentGroup.new(attrs)
xml = connection.perform_request '/cons_group/add_constituent_groups', {}, "POST", cons_group.to_xml
doc = Nokogiri::XML(xml)
group = doc.xpath('//cons_group')
cons_group.id = group.first[:id]
cons_group
end
|
#delete_constituent_groups(group_ids) ⇒ Object
64
65
66
67
|
# File 'lib/blue_state_digital/constituent_group.rb', line 64
def delete_constituent_groups(group_ids)
group_ids_concat = group_ids.is_a?(Array) ? group_ids.join(',') : group_ids.to_s
connection.wait_for_deferred_result( connection.perform_request( '/cons_group/delete_constituent_groups', {cons_group_ids: group_ids_concat}, "POST") )
end
|
#find_by_id(id) ⇒ Object
60
61
62
|
# File 'lib/blue_state_digital/constituent_group.rb', line 60
def find_by_id(id)
get_constituent_group(id)
end
|
#find_or_create(attr = {}) ⇒ Object
43
44
45
46
47
48
49
50
|
# File 'lib/blue_state_digital/constituent_group.rb', line 43
def find_or_create(attr = {})
group = get_constituent_group_by_slug(attr[:slug])
if group
return group
else
return create(attr)
end
end
|
#get_cons_ids_for_group(cons_group_id) ⇒ Object
98
99
100
101
|
# File 'lib/blue_state_digital/constituent_group.rb', line 98
def get_cons_ids_for_group(cons_group_id)
response = connection.wait_for_deferred_result( connection.perform_request '/cons_group/get_cons_ids_for_group', {cons_group_id: cons_group_id}, "GET" )
response.split("\n")
end
|
#get_constituent_group(id) ⇒ Object
56
57
58
|
# File 'lib/blue_state_digital/constituent_group.rb', line 56
def get_constituent_group(id)
from_response(connection.perform_request '/cons_group/get_constituent_group', {cons_group_id: id}, "GET")
end
|
#get_constituent_group_by_name(name) ⇒ Object
69
70
71
72
|
# File 'lib/blue_state_digital/constituent_group.rb', line 69
def get_constituent_group_by_name( name )
name = name.slice(0..254)
from_response( connection.perform_request '/cons_group/get_constituent_group_by_name', {name: name}, "GET" )
end
|
#get_constituent_group_by_slug(slug) ⇒ Object
74
75
76
77
|
# File 'lib/blue_state_digital/constituent_group.rb', line 74
def get_constituent_group_by_slug( slug )
slug = slug.slice(0..31)
from_response( connection.perform_request '/cons_group/get_constituent_group_by_slug', {slug: slug}, "GET" )
end
|
#list_constituent_groups ⇒ Object
52
53
54
|
# File 'lib/blue_state_digital/constituent_group.rb', line 52
def list_constituent_groups
from_response(connection.perform_request '/cons_group/list_constituent_groups', {}, "GET")
end
|
#remove_cons_ids_from_group(cons_group_id, cons_ids, options = {wait_for_result: true}) ⇒ Object
28
29
30
|
# File 'lib/blue_state_digital/constituent_group.rb', line 28
def remove_cons_ids_from_group(cons_group_id, cons_ids, options = {wait_for_result: true})
add_or_remove_cons_ids_from_group(:remove, cons_group_id, cons_ids, options)
end
|
#rename_group(id, new_name) ⇒ Object
79
80
81
|
# File 'lib/blue_state_digital/constituent_group.rb', line 79
def rename_group(id, new_name)
connection.perform_request '/cons_group/rename_group', {cons_group_id: id, new_name: new_name}, "POST"
end
|
#replace_constituent_group!(old_group_id, new_group_attrs) ⇒ Object
Warning: this is an expensive, potentially dangerous operation! You should almost always use rename group instead.
84
85
86
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/blue_state_digital/constituent_group.rb', line 84
def replace_constituent_group!(old_group_id, new_group_attrs)
group = get_constituent_group(old_group_id)
raise "Group being renamed does not exist!" if group.nil?
cons_ids = get_cons_ids_for_group(old_group_id)
delete_constituent_groups(old_group_id)
new_group = find_or_create(new_group_attrs)
add_cons_ids_to_group(new_group.id, cons_ids)
new_group
end
|