module UserEngage::Operation::Create

Public Instance Methods

create(attributes_hash) click to toggle source

Public: Creates a new resource instance, create a new resource through UE API and returns the insance.

# File lib/user_engage/operation/create.rb, line 11
def create(attributes_hash)
  check_for_required_params!(attributes_hash)
  response = create_remote_resource(attributes_hash)
  check_for_response_code!(response)
  construct_new_resource(response)
end

Private Instance Methods

check_for_required_params!(attributes_hash) click to toggle source

Private: Checks for the set required_params and raises an CreateNotSuccessfulException with a message if not all are given.

# File lib/user_engage/operation/create.rb, line 56
def check_for_required_params!(attributes_hash)
  return if (
    missing_attributes = required_params - attributes_hash.keys
  ).empty?

  raise(
    UserEngage::CreateNotSuccessfulException,
    "Missing attributes {#{missing_attributes.inspect}}"
  )
end
check_for_response_code!(response) click to toggle source

Private: Checks for 201 status. If not 200 a UserEngage::CreateNotSuccessfulException with given message gets returned.

# File lib/user_engage/operation/create.rb, line 43
def check_for_response_code!(response)
  return if response.status == 201

  raise(
    UserEngage::CreateNotSuccessfulException,
    response.body
  )
end
construct_new_resource(response) click to toggle source

Private: Creates a new resource instance with the body of the given response.

# File lib/user_engage/operation/create.rb, line 34
def construct_new_resource(response)
  data = JSON.parse(response.body, symbolize_names: true)
  new(data)
end
create_remote_resource(attributes_hash) click to toggle source

Private: Sends the post request with the UE client and returns the response

# File lib/user_engage/operation/create.rb, line 26
def create_remote_resource(attributes_hash)
  path = "/#{resource_name}/"
  UserEngage.client.post(path, attributes_hash)
end