Module: Koala::Facebook::TestUserMethods

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



7
8
9
10
11
12
# File 'lib/koala/test_users.rb', line 7

def self.included(base)
  base.class_eval do
    # make the Graph API accessible in case someone wants to make other calls to interact with their users
    attr_reader :api, :app_id, :app_access_token, :secret
  end
end

Instance Method Details

#befriend(user1_hash, user2_hash) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/koala/test_users.rb', line 55

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 = API.new(user1_token)
  u2_graph_api = API.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



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

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
  @api.graph_call(accounts_path, args, "post", options)
end

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



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/koala/test_users.rb', line 74

def create_network(network_size, installed = true, permissions = '')
  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



41
42
43
44
# File 'lib/koala/test_users.rb', line 41

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

#delete_allObject



46
47
48
# File 'lib/koala/test_users.rb', line 46

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

#graph_apiObject



88
89
90
91
# File 'lib/koala/test_users.rb', line 88

def graph_api
  Koala::Utils.deprecate("the TestUsers.graph_api accessor is deprecated and will be removed in a future version; please use .api instead.")
  @api
end

#initialize(options = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/koala/test_users.rb', line 14

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
  @api = API.new(@app_access_token)
end

#listObject



37
38
39
# File 'lib/koala/test_users.rb', line 37

def list
  @api.graph_call(accounts_path)
end

#update(test_user, args = {}, http_options = {}) ⇒ Object



50
51
52
53
# File 'lib/koala/test_users.rb', line 50

def update(test_user, args = {}, http_options = {})
  test_user = test_user["id"] if test_user.is_a?(Hash)
  @api.graph_call(test_user, args, "post", http_options)
end