Module: Facy::Core

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

Instance Method Summary collapse

Instance Method Details

#_initObject



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

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

#async(&block) ⇒ Object



100
101
102
# File 'lib/facy/core.rb', line 100

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

#configObject



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

def config
  @config ||= {}
end

#config_fileObject



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

def config_file
  config_file_folder = "/tmp"
  config_file_name = ".facy_config.yml"
  File.expand_path(config_file_name, config_file_folder)
end

#default_configObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/facy/core.rb', line 33

def default_config
  default_conf_file = File.expand_path("../../../config.yml", __FILE__)
  file = File.exist?(config_file) ? config_file : default_conf_file

  config = YAML.load_file(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



28
29
30
31
# File 'lib/facy/core.rb', line 28

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

#mutexObject



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

def mutex
  @mutex ||= Mutex.new
end

#start(options = {}) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/facy/core.rb', line 57

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



104
105
106
107
108
109
# File 'lib/facy/core.rb', line 104

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

#sync(&block) ⇒ Object



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

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