Class: Users

Inherits:
Thor
  • Object
show all
Defined in:
lib/sacrifice/users.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/sacrifice/users.rb', line 9

def self.exit_on_failure?()
  true
end

Instance Method Details

#changeObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/sacrifice/users.rb', line 80

def change
  user = App.find!(options[:app]).find_user(options[:user])

  puts user

  if user
    success = user.change(options)
    if success
      puts 'Successfully changed user'
    else
      puts 'Failed to change user'
    end
  else
    raise Thor::Error, "Unknown user '#{options[:user]}'"
  end
end

#createObject



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/sacrifice/users.rb', line 40

def create
  app = App.find!(options[:app])
  attrs = options.select { |k, v| %w(name installed locale).include? k.to_s }
  user = app.create_user(attrs)
  if user
    puts "User ID:      #{user.id}"
    puts "Access Token: #{user.access_token}"
    puts "Login URL:    #{user.}"
    puts "Email:        #{user.email}"
    puts "Password:     #{user.password}"
  end
end

#destroyObject



135
136
137
138
139
140
141
142
143
# File 'lib/sacrifice/users.rb', line 135

def destroy
  app = App.find!(options[:app])
  while (users = app.users).size > 0
    users.each { |user|
      user.destroy
      puts "remove user ##{user.id}"
    }
  end
end

#eraseObject



158
159
160
# File 'lib/sacrifice/users.rb', line 158

def erase
  Csv.erase options[:app], options[:file]
end

#friendObject



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/sacrifice/users.rb', line 58

def friend
  users = App.find!(options[:app]).users

  friends = []
  [options[:user1], options[:user2]].each { |user|
    friends.push (users.find { |u| u.id.to_s == user } or raise Thor::Error, "No user found w/id #{user.inspect}")
  }

  # The first request is just a request, the second request accepts the first request.
  friends.each_index { |idx| friends[idx].send_friend_request_to(friends[(idx + 1) % 2]) }
end

#friendsObject



168
169
170
171
172
# File 'lib/sacrifice/users.rb', line 168

def friends
  app = App.find!(options[:app])
  user = app.find_user(options[:user])
  Csv.friends user, Csv.ids(app, options[:friends])
end

#generateObject



150
151
152
# File 'lib/sacrifice/users.rb', line 150

def generate
  Csv.generate options[:app], options[:file], options[:friends]
end

#listObject



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/sacrifice/users.rb', line 16

def list
  app = App.find!(options[:app])
  if app.users.any?
    shell.print_table([
                          ['User ID', 'Access Token', 'Login URL'],
                          *(app.users.map do |user|
                            [user.id, user.access_token, user.]
                          end)
                      ])
  else
    puts "App #{app.name} has no users."
  end
end

#rmObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/sacrifice/users.rb', line 101

def rm
  user = App.find!(options[:app]).find_user(options[:user])

  if user
    result = user.destroy
    if result
      puts "User ID #{user.id} removed"
    else
      if @message.match /(\(#2903\) Cannot delete this test account because it is associated with other applications.)/
        error = <<-EOMSG.unindent.gsub(/^\|/, '')
#$1
            Run:
            |
              sacrifice users list-apps --app #{options[:app]} --user #{user.id}
            |
            then for each of the other apps, run:
            |
              sacrifice apps rm-user --app APP-NAME --user #{user.id}
            |
            Then re-run this command.
        EOMSG
      else
        error = @message
      end
      raise Thor::Error, error
    end
  else
    raise Thor::Error, "Unknown user '#{options[:user]}'"
  end
end