Class: GrowlGithubFeed::Master
- Inherits:
-
Object
- Object
- GrowlGithubFeed::Master
- Defined in:
- lib/growl-github-feed/master.rb
Instance Attribute Summary collapse
-
#conf ⇒ Object
readonly
Returns the value of attribute conf.
-
#growl ⇒ Object
readonly
Returns the value of attribute growl.
Instance Method Summary collapse
- #daemonize ⇒ Object
- #execute ⇒ Object
-
#extract_event_info(event) ⇒ Object
utils.
- #get_auth ⇒ Object
- #get_img(user_id) ⇒ Object
-
#initialize ⇒ Master
constructor
A new instance of Master.
- #open_pid_file ⇒ Object
-
#run ⇒ Object
daemon.
- #shutdown ⇒ Object
Constructor Details
#initialize ⇒ Master
Returns a new instance of Master.
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/growl-github-feed/master.rb', line 12 def initialize @conf = Config.new @growl = GrowlGithubFeed::PopUpper.new @last_event_time = Time.now.getlocal - 24*60*60; # daemonize @term = false @logger = Logger.new("./growl-github-feed.log") @logger.info "GrowlGithubFeed daemon start .." @pid_file_path = './growl-github-feed.pid' end |
Instance Attribute Details
#conf ⇒ Object (readonly)
Returns the value of attribute conf.
10 11 12 |
# File 'lib/growl-github-feed/master.rb', line 10 def conf @conf end |
#growl ⇒ Object (readonly)
Returns the value of attribute growl.
10 11 12 |
# File 'lib/growl-github-feed/master.rb', line 10 def growl @growl end |
Instance Method Details
#daemonize ⇒ Object
74 75 76 77 78 79 |
# File 'lib/growl-github-feed/master.rb', line 74 def daemonize exit!(0) if Process.fork Process.setsid exit!(0) if Process.fork open_pid_file end |
#execute ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/growl-github-feed/master.rb', line 24 def execute github = self.get_auth loop do re_feeds = github.received_events("#{@conf.user}") usr_feeds = github.user_events("#{@conf.user}") tmp_last_event_time = @last_event_time [ re_feeds, usr_feeds ].each do |feeds| return [] if feeds.empty? events = feeds.map{|r| Event.new(r)} events.each do |event| = event.created_at if @last_event_time < title, msg, img = self.extract_event_info event @logger.info "[#{}]" @logger.info "title: #{title}" @logger.info "message: #{msg}" @growl.notify(title, msg, img) else next end end # /events.each{} ts = feeds[0].created_at tmp_last_event_time = ts if tmp_last_event_time < ts end @last_event_time = tmp_last_event_time sleep 10 end end |
#extract_event_info(event) ⇒ Object
utils
94 95 96 97 98 99 100 101 102 |
# File 'lib/growl-github-feed/master.rb', line 94 def extract_event_info(event) title = "#{event.user}" title += "@#{event.repo_name}" msg = "#{event.comment_body}\n" msg += "#{event.created_at}" avatar_id = "#{event.user_avatar_id}" img = self.get_img(avatar_id) return title, msg, img end |
#get_auth ⇒ Object
114 115 116 117 118 119 |
# File 'lib/growl-github-feed/master.rb', line 114 def get_auth return Octokit::Client.new(:login => "#{@conf.user}", :password => "#{@conf.pass}") if @conf.token.nil? Octokit::Client.new :access_token => "#{@conf.token}" end |
#get_img(user_id) ⇒ Object
104 105 106 107 108 109 110 111 112 |
# File 'lib/growl-github-feed/master.rb', line 104 def get_img(user_id) return File.open(__DIR__ + "/../appIcons.icns").read if user_id.nil? uri = URI("http://www.gravatar.com/avatar/#{user_id}.jpg") host = uri.host path = uri.path http = Net::HTTP.new(host) response = http.get(path) response.body.to_s.force_encoding("UTF-8") end |
#open_pid_file ⇒ Object
81 82 83 84 85 86 87 88 89 |
# File 'lib/growl-github-feed/master.rb', line 81 def open_pid_file begin open( @pid_file_path, 'w' ) {|f| f << Process.pid } if @pid_file_path rescue => ex @logger.error "could not open pid file (#{@pid_file_path})" @logger.error "error: #{ex}" @logger.error ex.backtrace * "\n" end end |
#run ⇒ Object
daemon
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/growl-github-feed/master.rb', line 56 def run daemonize begin Signal.trap(:TERM) { shutdown } Signal.trap(:INT) { shutdown } execute rescue => ex @logger.error ex end end |
#shutdown ⇒ Object
67 68 69 70 71 72 |
# File 'lib/growl-github-feed/master.rb', line 67 def shutdown @term = true @logger.info "GrowlGithubFeed close.." @logger.close #FileUtils.rm @pid_file_path end |