Module: Facy::Core

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

Instance Method Summary collapse

Instance Method Details

#_initObject



15
16
17
18
19
20
# File 'lib/facy/core.rb', line 15

def _init
  load_config
  
  inits.each { |block| class_eval(&block) }
  log(:info, "core module init success")
end

#async(&block) ⇒ Object



91
92
93
# File 'lib/facy/core.rb', line 91

def async(&block)
  Thread.start { block.call }
end

#configObject



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

def config
  @config ||= {}
end

#default_configObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/facy/core.rb', line 27

def default_config
  config = YAML.load_file(File.expand_path("../../../config.yml", __FILE__))
  {
    session_file_folder: "/tmp",
    session_file_name: ".facy_access_token.yml",
    log_folder: "/tmp",
    log_file_name: ".facy_log",
    app_id: config['app_id'],
    app_secret: config['app_secret'],
    permission: config['permission'],
    granted: config['granted'],
    redirect_uri: "http://www.facebook.com/connect/login_success.html",
    prompt: "\e[15;48;5;27m f \e[0m >> ",
    stream_fetch_interval: 2,
    notification_fetch_interval: 2,
    output_interval: 3,
    retry_interval: 2,
    comments_view_num: 10,
  }
end

#init(&block) ⇒ Object



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

def init(&block)
  inits << block
end

#initsObject



7
8
9
# File 'lib/facy/core.rb', line 7

def inits
  @inits ||= []
end

#load_configObject



22
23
24
25
# File 'lib/facy/core.rb', line 22

def load_config
  config.reverse_update(default_config)
  log(:info, "config loaded #{config.to_s}")
end

#mutexObject



81
82
83
# File 'lib/facy/core.rb', line 81

def mutex
  @mutex ||= Mutex.new
end

#start(options = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/facy/core.rb', line 48

def start(options={})
  _init      

  EM.run do
    Thread.start do
      while buf = Readline.readline(config[:prompt], true) 
        execute(buf.strip)
      end
    end

    Thread.start do
      EM.add_periodic_timer(config[:stream_fetch_interval]) do
        facebook_stream_fetch
      end
    end
    
    Thread.start do
      EM.add_periodic_timer(config[:output_interval]) do
        periodic_output 
      end
    end

    Thread.start do
      EM.add_periodic_timer(config[:notification_fetch_interval]) do
        facebook_notification_fetch
      end
    end

    Signal.trap("INT")  { stop_process }
    Signal.trap("TERM") { stop_process }
  end
end

#stop_processObject



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

def stop_process
  puts "\nfacy going to stop..."
  Thread.new {
    EventMachine.stop 
  }.join
end

#sync(&block) ⇒ Object



85
86
87
88
89
# File 'lib/facy/core.rb', line 85

def sync(&block)
  mutex.synchronize do
    block.call
  end
end