Class: Octospy::Worker
- Inherits:
-
Object
- Object
- Octospy::Worker
- Defined in:
- lib/octospy/worker.rb
Instance Attribute Summary collapse
-
#thread ⇒ Object
readonly
Returns the value of attribute thread.
Instance Method Summary collapse
- #events ⇒ Object
-
#initialize(repositories, &block) ⇒ Worker
constructor
A new instance of Worker.
- #thread_loop ⇒ Object
- #watch_repositories ⇒ Object
- #while_ago ⇒ Object
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
#thread ⇒ Object (readonly)
Returns the value of attribute thread.
3 4 5 |
# File 'lib/octospy/worker.rb', line 3 def thread @thread end |
Instance Method Details
#events ⇒ Object
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_loop ⇒ Object
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_repositories ⇒ Object
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_ago ⇒ Object
38 39 40 |
# File 'lib/octospy/worker.rb', line 38 def while_ago Time.now.utc - (60 * 30) end |