Class: SocialBee::Facebook

Inherits:
Object
  • Object
show all
Includes:
Koala, YAML
Defined in:
lib/social-bee/facebook.rb

Instance Method Summary collapse

Constructor Details

#initialize(p = {}) ⇒ Facebook

Returns a new instance of Facebook.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/social-bee/facebook.rb', line 9

def initialize(p={})

  raise 'Undefined config file' unless config = YAML.load_file(Rails.root + p[:config])[Rails.env]
  raise 'Undefined application id' unless @app_id = config['app_id']
  raise 'Undefined application key' unless @app_key = config['app_key']
  raise 'Undefined callback url' unless @callback = config['callback_url']

  Koala::Facebook::OAuth.class_eval do
    def initialize_with_default_settings(*args)
    case args.size
    when 0, 1
      raise "Application id and/or secret are not specified in the config" unless @app_id && @app_key
      initialize_without_default_settings(@app_id.to_s, @app_key.to_s, @callback.to_s)
    when 2, 3
      initialize_without_default_settings(*args)
    end
    end
    alias_method_chain :initialize, :default_settings
  end

end

Instance Method Details

#client(token) ⇒ Object



31
32
33
# File 'lib/social-bee/facebook.rb', line 31

def client( token )
  Koala::Facebook::API.new( token )
end

#get_friends(token) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/social-bee/facebook.rb', line 50

def get_friends( token )
  arr = []
  client( token ).get_connections('me', 'friends').each do |itm|
    arr.push itm['id']
  end
  arr
end

#say_friends(message, token) ⇒ Object



44
45
46
47
48
# File 'lib/social-bee/facebook.rb', line 44

def say_friends( message, token )
  get_friends( token ).each do |itm|
    say_private( itm, message, token )
  end
end

#say_private(to, message, token) ⇒ Object



40
41
42
# File 'lib/social-bee/facebook.rb', line 40

def say_private( to, message, token )
  client( token ).put_object( to, 'inbox', :message => message )
end

#say_public(message, token) ⇒ Object



36
37
38
# File 'lib/social-bee/facebook.rb', line 36

def say_public( message, token )
  client( token ).put_wall_post( message )
end