Class: Octospy::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/octospy/worker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repositories, &block) ⇒ Worker

Returns a new instance of Worker.



5
6
7
8
9
10
# File 'lib/octospy/worker.rb', line 5

def initialize(repositories, &block)
  @repositories = repositories
  @block = block
  @last_event_id = nil
  thread_loop
end

Instance Attribute Details

#threadObject (readonly)

Returns the value of attribute thread.



3
4
5
# File 'lib/octospy/worker.rb', line 3

def thread
  @thread
end

Instance Method Details

#eventsObject



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/octospy/worker.rb', line 26

def events
  @repositories.each_with_object([]) do |repo, arr|
    if Octokit.rate_limit.remaining.zero?
      @block.call "ヾ(;´Д`)ノ #{::Octokit.rate_limit}"
      break
    end

    sleep Octospy.api_request_interval
    arr.concat ::Octokit.repository_events(repo)
  end
end

#thread_loopObject



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/octospy/worker.rb', line 12

def thread_loop
  @thread = Thread.start do
    loop do
      begin
        watch_repositories
        sleep Octospy.worker_interval
      rescue => e
        @block.call "Octospy Error: #{e.message}"
        sleep Octospy.worker_interval
      end
    end
  end
end

#watch_repositoriesObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/octospy/worker.rb', line 42

def watch_repositories
  # ascending by event.id
  events.sort_by(&:id).each { |event|
    case
    when @last_event_id.nil? && while_ago >= event.created_at
      next
    when !@last_event_id.nil? && @last_event_id >= event.id.to_i
      next
    end

    parsed_event = Octospy.parse(event)
    next unless parsed_event

    @last_event_id = event.id.to_i
    parsed_event.each { |p| @block.call p[:message] }
  }
end

#while_agoObject



38
39
40
# File 'lib/octospy/worker.rb', line 38

def while_ago
  Time.now.utc - (60 * 30)
end