Class: EPC::Command::UpdateUserCommand

Inherits:
BaseCommand show all
Defined in:
lib/epc/command/user/update_user_command.rb

Constant Summary

Constants inherited from BaseCommand

BaseCommand::GIVEUP_TICKS, BaseCommand::SLEEP_TIME, BaseCommand::TICKER_TICKS

Instance Attribute Summary

Attributes inherited from BaseCommand

#client, #options

Instance Method Summary collapse

Methods inherited from BaseCommand

#go, inherited, #initialize, required_options

Methods included from PersistentAttributes

#auth_token, #caller_id, #target_url

Constructor Details

This class inherits a constructor from EPC::Command::BaseCommand

Instance Method Details

#execute(user = nil, *args) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
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
48
49
50
51
52
# File 'lib/epc/command/user/update_user_command.rb', line 6

def execute(user = nil, *args)

  if @options[:password].present?
    if numeric?(user)
      email = get_resource_attribute(EPC::Config::USERS_PATH, :email, :id, user.to_i)
    else
      email = user
    end
    raise FatalError, "User could not be determined" if email.blank?

    raise FatalError, "You need to specify the password change token with the --token option" if @options[:token].blank?

    status, response, headers = client.put(EPC::Config::USERS_PATH + "/#{email}/change_password", {:token => @options[:token], :password => @options[:password]})

    if status.successful?
      say("Password change successful")
    else
      say("Request failed: [#{response[:message]}]")
    end

    return status
  end

  if @options[:email].present? || @options[:name].present?
    if numeric?(user)
      user_id = user.to_i
    else
      user_id = get_resource_id(EPC::Config::USERS_PATH, :email, user)
    end

    raise FatalError, "User could not be determined" if user_id.blank?

    params = {}
    params[:email] = @options[:email] if @options[:email].present?
    params[:name] = @options[:name] if @options[:name].present?

    status, response, headers = client.put(EPC::Config::USERS_PATH + "/#{user_id}", params)
    
    if status.successful?
      say("User updated")
    else
      say("Request failed: [#{response[:message]}]")
    end

    return status
  end
end