Class: FacebookTestUsers::CLI::Users

Inherits:
Base
  • Object
show all
Defined in:
lib/facebook_test_users/cli.rb

Overview

Apps

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils

#bad_request_message, #find_app!, #handle_bad_request

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


108
# File 'lib/facebook_test_users/cli.rb', line 108

def self.exit_on_failure?() true end

Instance Method Details

#changeObject



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/facebook_test_users/cli.rb', line 183

def change
  app = find_app!(options[:app])
  user = app.users.find do |user|
    user.id.to_s == options[:user].to_s
  end

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

#createObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/facebook_test_users/cli.rb', line 137

def create
  app = find_app!(options[:app])
  attrs = options.select { |k, v| %w(name installed locale).include? k.to_s }
  user = handle_bad_request do
    app.create_user(attrs)
  end
  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

#friendObject



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/facebook_test_users/cli.rb', line 157

def friend
  app = find_app!(options[:app])
  users = app.users
  u1 = users.find {|u| u.id.to_s == options[:user1] } or \
    raise Thor::Error, "No user found w/id #{options[:user1].inspect}"
  u2 = users.find {|u| u.id.to_s == options[:user2] } or \
    raise Thor::Error, "No user found w/id #{options[:user2].inspect}"

  # The first request is just a request; the second request
  # accepts the first request.
  handle_bad_request do
    u1.send_friend_request_to(u2)
    u2.send_friend_request_to(u1)
  end
end

#listObject



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/facebook_test_users/cli.rb', line 113

def list
  app = find_app!(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

#list_appsObject



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/facebook_test_users/cli.rb', line 208

def list_apps
  app = find_app!(options[:app])
  user = app.users.find do |user|
    user.id.to_s == options[:user].to_s
  end

  if user
    response = handle_bad_request do
      user.owner_apps(app)
    end
    if response
      json = MultiJson.decode(response)
      apps = json['data'] rescue nil
      if apps
        shell.print_table([
                            ['App name', 'App ID'],
                            *(apps.map { |app| [app['name'], app['id']] })
                          ])
      else
        $stderr.write("No apps returned; response was: #{response}\n")
      end
    end
  else
    raise Thor::Error, "Unknown user '#{options[:user]}'"
  end
end

#nukeObject



278
279
280
281
# File 'lib/facebook_test_users/cli.rb', line 278

def nuke
  app = find_app!(options[:app])
  app.users.each(&:destroy)
end

#rmObject



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/facebook_test_users/cli.rb', line 239

def rm
  app = find_app!(options[:app])
  user = app.users.find do |user|
    user.id.to_s == options[:user].to_s
  end

  if user
    result = handle_bad_request(raise_error=false) do
      user.destroy
    end
    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:
        |
          fbtu users list-apps --app #{options[:app]} --user #{user.id}
        |
        then for each of the other apps, run:
        |
          fbtu 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