Class: PUNK::CreateSessionAction

Inherits:
Action show all
Defined in:
lib/punk/actions/sessions/create.rb

Instance Attribute Summary

Attributes included from Validatable

#errors

Instance Method Summary collapse

Methods inherited from Action

perform, #present

Methods inherited from Service

#result, run

Methods included from Loggable

#exception, #logger, #profile_debug, #profile_info, #profile_trace

Methods inherited from Runnable

args, #method_missing, #respond_to_missing?

Methods included from Validatable

#default_validation_helpers_options, #get_column_value, #valid?, #validates_not_empty

Methods included from Plugins::Validation::InstanceMethods

#validates_email, #validates_event, #validates_phone, #validates_state, #validates_url

Methods inherited from Settings

#method_missing, #respond_to_missing?

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class PUNK::Runnable

Instance Method Details

#claim_typeObject



42
43
44
# File 'lib/punk/actions/sessions/create.rb', line 42

def claim_type
  @claim_type ||= _guess_claim_type
end

#processObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/punk/actions/sessions/create.rb', line 22

def process
  PUNK.db.transaction do
    identity =
      Identity.find_or_create(claim: _normalize_claim) do |i|
        i.claim_type = claim_type
      end
    session = Session.create(identity: identity, remote_addr: remote_addr, user_agent: user_agent)
    ChallengeClaimService.run(session: session)
    IdentifySessionWorker.perform_async(session_id: session.id)
    message =
      case identity.claim_type
      when :email
        "We have sent a verification code to your email address. Please enter it to verify your identity."
      when :phone
        "We have sent a verification code to your phone number by SMS. Please enter it to verify your identity."
      end
    present PendingSessionView, session: session, message: message, status: 201
  end
end

#validateObject



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/punk/actions/sessions/create.rb', line 10

def validate
  validates_not_null :claim
  validates_not_empty :claim
  validates_not_null :claim_type, message: "is not an email address or phone number"
  validates_email :claim if claim_type == :email
  validates_phone :claim if claim_type == :phone
  validates_not_null :remote_addr
  validates_not_empty :remote_addr
  validates_not_null :user_agent
  validates_not_empty :user_agent
end