Module: Koala::Facebook::TestUserMethods

Included in:
TestUsers
Defined in:
lib/koala/test_users.rb

Instance Method Summary collapse

Instance Method Details

#befriend(user1_hash, user2_hash) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/koala/test_users.rb', line 43

def befriend(user1_hash, user2_hash)
  user1_id = user1_hash["id"] || user1_hash[:id]
  user2_id = user2_hash["id"] || user2_hash[:id]
  user1_token = user1_hash["access_token"] || user1_hash[:access_token]
  user2_token = user2_hash["access_token"] || user2_hash[:access_token]
  unless user1_id && user2_id && user1_token && user2_token
    # we explicitly raise an error here to minimize the risk of confusing output
    # if you pass in a string (as was previously supported) no local exception would be raised
    # but the Facebook call would fail
    raise ArgumentError, "TestUsers#befriend requires hash arguments for both users with id and access_token"
  end
  
  u1_graph_api = GraphAPI.new(user1_token)
  u2_graph_api = GraphAPI.new(user2_token)

  u1_graph_api.graph_call("#{user1_id}/friends/#{user2_id}", {}, "post") && 
    u2_graph_api.graph_call("#{user2_id}/friends/#{user1_id}", {}, "post")
end

#create(installed, permissions = nil, args = {}, options = {}) ⇒ Object



23
24
25
26
27
28
# File 'lib/koala/test_users.rb', line 23

def create(installed, permissions = nil, args = {}, options = {})
  # Creates and returns a test user
  args['installed'] = installed
  args['permissions'] = (permissions.is_a?(Array) ? permissions.join(",") : permissions) if installed
  result = @graph_api.graph_call(accounts_path, args, "post", options)
end

#create_network(network_size, installed = true, permissions = '') ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/koala/test_users.rb', line 62

def create_network(network_size, installed = true, permissions = '')
  network_size = 50 if network_size > 50 # FB's max is 50
  users = (0...network_size).collect { create(installed, permissions) }
  friends = users.clone
  users.each do |user|
    # Remove this user from list of friends
    friends.delete_at(0)
    # befriend all the others
    friends.each do |friend|
      befriend(user, friend)
    end
  end
  return users
end

#delete(test_user) ⇒ Object



34
35
36
37
# File 'lib/koala/test_users.rb', line 34

def delete(test_user)
  test_user = test_user["id"] if test_user.is_a?(Hash)
  @graph_api.delete_object(test_user)
end

#delete_allObject



39
40
41
# File 'lib/koala/test_users.rb', line 39

def delete_all
  list.each {|u| delete u }
end

#initialize(options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/koala/test_users.rb', line 7

def initialize(options = {})
  @app_id = options[:app_id]
  @app_access_token = options[:app_access_token]
  @secret = options[:secret]
  unless @app_id && (@app_access_token || @secret) # make sure we have what we need
    raise ArgumentError, "Initialize must receive a hash with :app_id and either :app_access_token or :secret! (received #{options.inspect})"
  end

  # fetch the access token if we're provided a secret
  if @secret && !@app_access_token
    oauth = Koala::Facebook::OAuth.new(@app_id, @secret)
    @app_access_token = oauth.get_app_access_token
  end
  @graph_api = GraphAPI.new(@app_access_token)
end

#listObject



30
31
32
# File 'lib/koala/test_users.rb', line 30

def list
  @graph_api.graph_call(accounts_path)["data"]
end