Sha256: 14ea6f4756e922838570efa636a9e06197f595df034e18995f7d216c012a4b65

Contents?: true

Size: 1.37 KB

Versions: 3

Compression:

Stored size: 1.37 KB

Contents

module Octospy
  class Worker
    attr_reader :thread

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

    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

    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

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

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
octospy-0.0.8 lib/octospy/worker.rb
octospy-0.0.7 lib/octospy/worker.rb
octospy-0.0.6 lib/octospy/worker.rb