Class: FacebookTestUsers::App

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs) ⇒ App

Returns a new instance of App.



8
9
10
11
# File 'lib/facebook_test_users/app.rb', line 8

def initialize(attrs)
  @name, @id, @secret = attrs[:name].to_s, attrs[:id].to_s, attrs[:secret].to_s
  validate!
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



6
7
8
# File 'lib/facebook_test_users/app.rb', line 6

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/facebook_test_users/app.rb', line 6

def name
  @name
end

#secretObject (readonly)

Returns the value of attribute secret.



6
7
8
# File 'lib/facebook_test_users/app.rb', line 6

def secret
  @secret
end

Class Method Details

.allObject

query methods



61
62
63
64
65
66
67
# File 'lib/facebook_test_users/app.rb', line 61

def self.all
  if DB[:apps]
    DB[:apps].map {|attrs| new(attrs) }
  else
    []
  end
end

.create!(attrs) ⇒ Object



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

def self.create!(attrs)
  new_app = new(attrs)

  if all.find {|app| app.name == new_app.name }
    raise ArgumentError, "App names must be unique, and there is already an app named \"#{new_app.name}\"."
  end

  DB.update do |data|
    data[:apps] ||= []
    data[:apps] << new_app.attrs
  end
end

.find_by_name(name) ⇒ Object



69
70
71
# File 'lib/facebook_test_users/app.rb', line 69

def self.find_by_name(name)
  all.find {|a| a.name == name}
end

Instance Method Details

#access_tokenObject



73
74
75
# File 'lib/facebook_test_users/app.rb', line 73

def access_token
  @access_token ||= AccessToken.get(id, secret)
end

#add_user(options) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/facebook_test_users/app.rb', line 45

def add_user(options)
  raise "add_user called without uid" \
    unless options.has_key?(:uid)
  raise "add_user called without owner_access_token" \
    unless options.has_key?(:owner_access_token)

  user_data = RestClient.post(users_url, {:access_token => access_token}.merge(options))
  User.new(MultiJson.decode(user_data))
end

#attrsObject



13
14
15
# File 'lib/facebook_test_users/app.rb', line 13

def attrs
  {:name => name, :id => id, :secret => secret}
end

#create_user(options = {}) ⇒ Object



40
41
42
43
# File 'lib/facebook_test_users/app.rb', line 40

def create_user(options = {})
  user_data = RestClient.post(users_url, {:access_token => access_token}.merge(options))
  User.new(MultiJson.decode(user_data))
end

#rm_user(uid) ⇒ Object



55
56
57
58
# File 'lib/facebook_test_users/app.rb', line 55

def rm_user(uid)
  url = rm_user_url(uid, access_token)
  RestClient.delete(url)
end

#usersObject



30
31
32
33
34
35
36
37
38
# File 'lib/facebook_test_users/app.rb', line 30

def users
  users_data = RestClient.get(users_url, :params => {
      :access_token => access_token
    })

  MultiJson.decode(users_data)["data"].map do |user_data|
    User.new(user_data)
  end
end