Class: Agig::Session

Inherits:
Net::IRC::Server::Session
  • Object
show all
Defined in:
lib/agig/session.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Session

Returns a new instance of Session.



19
20
21
22
# File 'lib/agig/session.rb', line 19

def initialize(*args)
  super
  @notification_last_retrieved = @watch_last_retrieved = Time.now.utc - 3600
end

Instance Method Details

#channelsObject



15
16
17
# File 'lib/agig/session.rb', line 15

def channels
  ['#notification', '#watch']
end

#clientObject



24
25
26
27
28
29
30
# File 'lib/agig/session.rb', line 24

def client
  @client ||= if @opts.oauth_token
                Octokit::Client.new(oauth_token: @pass)
              else
                Octokit::Client.new(login: @nick, password: @pass)
              end
end

#on_disconnectedObject



32
33
34
# File 'lib/agig/session.rb', line 32

def on_disconnected
  @retrieve_thread.kill rescue nil
end

#on_user(m) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
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
80
81
82
83
84
85
86
87
88
89
# File 'lib/agig/session.rb', line 36

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...'

        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

          reachable_url = reachable_url_for(entry.subject.latest_comment_url)

          post entry.repository.owner., PRIVMSG, "#notification", "\0035#{entry.subject.title}\017 \00314#{reachable_url}\017"
          @notification_last_retrieved = updated_at
        end

        events = client.received_events(@nick)
        events.sort_by(&:created_at).reverse_each do |event|
          next if event.type != "WatchEvent"

          created_at = Time.parse(event.created_at).utc
          next if created_at <= @watch_last_retrieved

          post event.actor., PRIVMSG, "#watch", "\0035#{event.payload.action}\017 \00314http://github.com/#{event.repo.name}\017"
          @watch_last_retrieved = created_at
        end

        @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
  end
end

#reachable_url_for(latest_comment_url) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/agig/session.rb', line 91

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+?)$/)
    issue_id = issue_match[1]
    latest_comment = client.issue_comments(repos_owner, issue_id).last
    latest_comment ?
      latest_comment['html_url'] :
      latest_comment_url.sub(/api\./, '').sub(/repos\//, '').sub(/pulls\//, 'pull/')
  elsif comment_match = latest_comment_url.match(/comments\/(\d+?)$/)
    comment_id = comment_match[1]
    client.issue_comment(repos_owner, comment_id)['html_url']
  else
    nil
  end
end

#server_nameObject



7
8
9
# File 'lib/agig/session.rb', line 7

def server_name
  "github"
end

#server_versionObject



11
12
13
# File 'lib/agig/session.rb', line 11

def server_version
  Agig::VERSION
end