lib/agig/session.rb in agig-0.1.5 vs lib/agig/session.rb in agig-0.2.0

- old
+ new

@@ -1,8 +1,9 @@ require 'ostruct' require 'time' require 'net/irc' +require 'string-irc' require 'octokit' class Agig::Session < Net::IRC::Server::Session def server_name "github" @@ -20,73 +21,67 @@ super @notification_last_retrieved = @watch_last_retrieved = Time.now.utc - 3600 end def client - @client ||= if @opts.oauth_token - Octokit::Client.new(oauth_token: @pass) - else - Octokit::Client.new(login: @nick, password: @pass) - end + @client ||= Octokit::Client.new(access_token: @pass) end def on_disconnected @retrieve_thread.kill rescue nil end def on_user(m) super - @real, *@opts = @real.split(/\s+/) - @opts = OpenStruct.new @opts.inject({}) {|r, i| - key, value = i.split("=", 2) - r.update key => case value - when nil then true - when /\A\d+\z/ then value.to_i - when /\A(?:\d+\.\d*|\.\d+)\z/ then value.to_f - else value - end - } channels.each{|channel| post @nick, JOIN, channel } @retrieve_thread = Thread.start do loop do - begin - @log.info 'retrieveing feed...' + retrieve @opts.interval + end + end + end - entries = client.notifications(all: true) - entries.sort_by(&:updated_at).reverse_each do |entry| - updated_at = Time.parse(entry.updated_at).utc - next if updated_at <= @notification_last_retrieved + private - reachable_url = reachable_url_for(entry.subject.latest_comment_url) + def retrieve(interval) + @log.info 'retrieveing feed...' - post entry.repository.owner.login, PRIVMSG, "#notification", "\0035#{entry.subject.title}\017 \00314#{reachable_url}\017" - @notification_last_retrieved = updated_at - end + entries = client.notifications(all: true) + entries.sort_by(&:updated_at).each do |entry| + updated_at = Time.parse(entry.updated_at.to_s).utc + next if updated_at <= @notification_last_retrieved - events = client.received_events(@nick) - events.sort_by(&:created_at).reverse_each do |event| - next if event.type != "WatchEvent" + title = StringIrc.new(entry.subject.title).brown.to_s + url = StringIrc.new(reachable_url_for(entry.subject.latest_comment_url)).grey.to_s - created_at = Time.parse(event.created_at).utc - next if created_at <= @watch_last_retrieved + post entry.repository.owner.login, PRIVMSG, "#notification", "#{title} #{url}" + @notification_last_retrieved = updated_at + end - post event.actor.login, PRIVMSG, "#watch", "\0035#{event.payload.action}\017 \00314http://github.com/#{event.repo.name}\017" - @watch_last_retrieved = created_at - end + events = client.received_events(@nick) + events.sort_by(&:created_at).each do |event| + next if event.type != "WatchEvent" - @log.info 'sleep' - sleep 30 - rescue Exception => e - @log.error e.inspect - e.backtrace.each do |l| - @log.error "\t#{l}" - end - sleep 10 - end - end + created_at = Time.parse(event.created_at.to_s).utc + next if created_at <= @watch_last_retrieved + + action = StringIrc.new(event.payload.action).brown.to_s + url = StringIrc.new("http://github.com/#{event.repo.name}").grey.to_s + + post event.actor.login, PRIVMSG, "#watch", "#{action} #{url}" + @watch_last_retrieved = created_at end + + @log.info 'sleep' + sleep interval + rescue Exception => e + @log.error e.inspect + e.backtrace.each do |l| + @log.error "\t#{l}" + end + sleep 10 end def reachable_url_for(latest_comment_url) repos_owner = latest_comment_url.match(/repos\/(.+?\/.+?)\//)[1] if issue_match = latest_comment_url.match(/(?:issues|pulls)\/(\d+?)$/)