Module: Facy::Facebook

Included in:
Facy
Defined in:
lib/facy/facebook.rb

Defined Under Namespace

Modules: ConnectionStatus

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#authen_hashObject (readonly)

Returns the value of attribute authen_hash.



3
4
5
# File 'lib/facy/facebook.rb', line 3

def authen_hash
  @authen_hash
end

#restObject (readonly)

Returns the value of attribute rest.



3
4
5
# File 'lib/facy/facebook.rb', line 3

def rest
  @rest
end

Instance Method Details

#expired_sessionObject



84
85
86
87
88
# File 'lib/facy/facebook.rb', line 84

def expired_session
  FileUtils.rm(session_file)
  instant_output(Item.new(info: :info, content: "Please restart facy to obtain new access token!"))
  exit
end

#facebook_comment(post_id, comment) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/facy/facebook.rb', line 74

def facebook_comment(post_id, comment)
  @graph.put_comment(post_id, comment)
rescue Koala::Facebook::ServerError
  retry_wait
rescue Koala::Facebook::APIError
  expired_session
rescue Exception => e
  error e
end

#facebook_like(post_id) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/facy/facebook.rb', line 53

def facebook_like(post_id)
  @graph.put_like(post_id)
rescue Koala::Facebook::ServerError
  retry_wait
rescue Koala::Facebook::APIError
  expired_session
rescue Exception => e
  error e
end

#facebook_notification_fetchObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/facy/facebook.rb', line 29

def facebook_notification_fetch
  return unless facebook_status == ConnectionStatus::NORMAL
  notifications = @graph.get_connections("me", "notifications")
  notifications.each { |notifi| notification_print_queue << graph2item(notifi) }
  log(:info, "fetch notification ok")
rescue Koala::Facebook::ServerError
  retry_wait
rescue Koala::Facebook::APIError
  expired_session
rescue Exception => e
  error e
end

#facebook_post(text) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/facy/facebook.rb', line 42

def facebook_post(text)
  @graph.put_wall_post(text)
rescue Koala::Facebook::ServerError
  retry_wait
rescue Koala::Facebook::APIError
  expired_session
rescue Exception => e
  error e
end

#facebook_set_seen(notification_id) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/facy/facebook.rb', line 63

def facebook_set_seen(notification_id)
  @graph.put_connection("#{notification_id}", "unread=false") 
rescue Koala::Facebook::ServerError
  retry_wait
rescue Koala::Facebook::APIError
  expired_session
rescue Exception => e
  error e
end

#facebook_statusObject



11
12
13
# File 'lib/facy/facebook.rb', line 11

def facebook_status
  @status ||= ConnectionStatus::NORMAL
end

#facebook_stream_fetchObject

RULE: all facebook method should be prefix with facebook



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/facy/facebook.rb', line 16

def facebook_stream_fetch
  return unless facebook_status == ConnectionStatus::NORMAL
  streams = @graph.get_connections("me", "home")
  streams.each { |post| stream_print_queue << graph2item(post) }
  log(:info, "fetch stream ok")
rescue Koala::Facebook::ServerError
  retry_wait
rescue Koala::Facebook::APIError
  expired_session
rescue Exception => e
  error e
end

#loginObject



96
97
98
99
100
# File 'lib/facy/facebook.rb', line 96

def 
  token = config[:access_token]
  @graph = Koala::Facebook::API.new(token)
  log(:info, "login ok at facebook module: #{@graph}")
end

#retry_waitObject



90
91
92
93
94
# File 'lib/facy/facebook.rb', line 90

def retry_wait
  log(:error, "facebook server error, need retry")
  instant_output(Item.new(info: :error, content: "facebook server error, retry in #{config[:retry_interval]} seconds"))
  sleep(config[:retry_interval])
end