Class: InvisibleCollector::Resources::GroupResource

Inherits:
Object
  • Object
show all
Includes:
DefaultHandlers
Defined in:
lib/invisible_collector/resources/group_resource.rb

Instance Method Summary collapse

Methods included from DefaultHandlers

#execute, #execute_get, #execute_post, #handle, #handles

Constructor Details

#initialize(options = {}) ⇒ GroupResource

Returns a new instance of GroupResource.



8
9
10
11
12
13
14
# File 'lib/invisible_collector/resources/group_resource.rb', line 8

def initialize(options = {})
  super(options)
  handle(400) { |response| raise InvisibleCollector::InvalidRequest.from_json(response.body) }
  handle(404) { |response| raise InvisibleCollector::NotFound.from_json(response.body) }
  handle(409) { |response| raise InvisibleCollector::InvalidRequest.from_json(response.body) }
  handle(422) { |response| raise InvisibleCollector::InvalidRequest.from_json(response.body) }
end

Instance Method Details

#add_customer(group, customer) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/invisible_collector/resources/group_resource.rb', line 16

def add_customer(group, customer)
  group_id = group.is_a?(Model::Group) ? group.id : group
  customer_id = customer.is_a?(Model::Customer) ? customer.gid : customer
  response = execute_post("groups/#{group_id}/customers/#{customer_id}", {})
  group = Model::Group.new(JSON.parse(response.body).deep_transform_keys(&:underscore))
  Response.new(response, group)
end

#all(params = {}) ⇒ Object



24
25
26
27
28
# File 'lib/invisible_collector/resources/group_resource.rb', line 24

def all(params = {})
  response = execute_get('groups', params)
  groups = JSON.parse(response.body).map { |json| Model::Group.new(json.deep_transform_keys(&:underscore)) }
  Response.new(response, groups)
end

#get!(gid) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/invisible_collector/resources/group_resource.rb', line 30

def get!(gid)
  response = @connection.get("groups/#{gid}")
  if handles.key? response.status
    handles[response.status].call response
  else
    group = Model::Group.new(JSON.parse(response.body).deep_transform_keys(&:underscore))
    Response.new(response, group)
  end
end

#save(group) ⇒ Object



40
41
42
43
44
# File 'lib/invisible_collector/resources/group_resource.rb', line 40

def save(group)
  response = execute_post('groups', group)
  group = Model::Group.new(JSON.parse(response.body).deep_transform_keys(&:underscore))
  Response.new(response, group)
end