Sha256: 379183ecbb4375be818537a34ec7dbc52742671858fd61fc37ab18eedc729ae4

Contents?: true

Size: 1.47 KB

Versions: 7

Compression:

Stored size: 1.47 KB

Contents

class Zendesk2::Client::CreateOrganization < Zendesk2::Client::Request
  request_method :post
  request_path { |_| "/organizations.json" }
  request_body { |r|  { "organization" => r.organization_params } }

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

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

  def mock
    identity = service.serial_id

    record = {
      "id"         => identity,
      "url"        => url_for("/organizations/#{identity}.json"),
      "created_at" => Time.now.iso8601,
      "updated_at" => Time.now.iso8601,
    }.merge(self.organization_params)

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

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

    if record["external_id"] && self.data[:organizations].values.find { |o| o["external_id"].to_s.downcase == record["external_id"].to_s.downcase }
      error!(:invalid, details: {"name" => [ { "description" => "External has already been taken" } ]})
    end

    self.data[:organizations][identity] = record

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

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
zendesk2-1.7.5 lib/zendesk2/client/requests/create_organization.rb
zendesk2-1.7.4 lib/zendesk2/client/requests/create_organization.rb
zendesk2-1.7.2 lib/zendesk2/client/requests/create_organization.rb
zendesk2-1.7.1 lib/zendesk2/client/requests/create_organization.rb
zendesk2-1.7.0 lib/zendesk2/client/requests/create_organization.rb
zendesk2-1.6.1 lib/zendesk2/client/requests/create_organization.rb
zendesk2-1.5.17 lib/zendesk2/client/requests/create_organization.rb