Class: Zendesk2::CreateOrganization

Inherits:
Object
  • Object
show all
Includes:
Request
Defined in:
lib/zendesk2/create_organization.rb

Instance Attribute Summary

Attributes included from Request

#params

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Request

#call, cistern_included, #data, #delete!, #error!, #find!, #html_url_for, included, #mock_response, #page, #page_params!, #page_params?, #pluralize, #real, #real_request, #request_body, #request_params, #request_path, #resources, #response, #timestamp, #url_for

Class Method Details

.accepted_attributesObject



9
10
11
# File 'lib/zendesk2/create_organization.rb', line 9

def self.accepted_attributes
  %w(details domain_names external_id group_id organization_fields shared_comments shared_tickets tags name notes)
end

Instance Method Details

#mockObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/zendesk2/create_organization.rb', line 17

def mock
  identity = cistern.serial_id

  record = {
    'id'         => identity,
    'url'        => url_for("/organizations/#{identity}.json"),
    'created_at' => timestamp,
    'updated_at' => timestamp,
  }.merge(organization_params)

  unless record['name']
    error!(:invalid, details: { 'name' => [{ 'description' => 'Name cannot be blank' }] })
  end

  if data[:organizations].values.find { |o| o['name'].casecmp(record['name'].downcase).zero? }
    error!(:invalid, details: { 'name' => [{ 'description' => 'Name: has already been taken' }] })
  end

  external_id = record['external_id']
  matching_organization = external_id && data[:organizations].values.find do |o|
    o['external_id'].to_s.casecmp(external_id.to_s.downcase).zero?
  end

  if matching_organization
    error!(:invalid, details: { 'name' => [{ 'description' => 'External has already been taken' }] })
  end

  data[:organizations][identity] = record

  mock_response({ 'organization' => record }, { status: 201 })
end

#organization_paramsObject



13
14
15
# File 'lib/zendesk2/create_organization.rb', line 13

def organization_params
  @_organization_params ||= Cistern::Hash.slice(params.fetch('organization'), *self.class.accepted_attributes)
end