Module: Facy::Core

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

Instance Method Summary collapse

Instance Method Details

#_initObject



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

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

#async(&block) ⇒ Object



109
110
111
# File 'lib/facy/core.rb', line 109

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

#configObject



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

def config
  @config ||= {}
end

#config_fileObject



19
20
21
22
23
# File 'lib/facy/core.rb', line 19

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



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/facy/core.rb', line 42

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: "~",
    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



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

def init(&block)
  inits << block
end

#initsObject



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

def inits
  @inits ||= []
end

#load_configObject



37
38
39
40
# File 'lib/facy/core.rb', line 37

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

#meObject



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

def me
  @me
end

#mutexObject



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

def mutex
  @mutex ||= Mutex.new
end

#set_meObject



33
34
35
# File 'lib/facy/core.rb', line 33

def set_me
  @me = facebook_me
end

#start(options = {}) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/facy/core.rb', line 66

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



113
114
115
116
117
118
# File 'lib/facy/core.rb', line 113

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

#sync(&block) ⇒ Object



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

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