class GHAProvider

Public Class Methods

new() click to toggle source
# File lib/gh-archive.rb, line 119
def initialize
    @logger = Logger.new(STDOUT)
    
    @includes = {}
    @excludes = {}
    
    @checkpoint_name = nil
    @use_json = true
end

Public Instance Methods

each(from = Time.gm(2015, 1, 1), to = Time.now) { |event, current_time| ... } click to toggle source
# File lib/gh-archive.rb, line 199
def each(from = Time.gm(2015, 1, 1), to = Time.now)
    exceptions = []
    
    from = restore_checkpoint(from)
    
    self.each_time(from, to) do |current_time|
        events = []
        
        update_checkpoint(current_time)
        
        begin
            events = self.get(current_time)
        rescue GHAException => e
            @logger.warn(e.message)
            next
        rescue => e
            @logger.error("An exception occurred for #{current_time}: #{e.message}")
            exceptions << e
            next
        end
        
        events.each do |event|
            skip = false
            @includes.each do |key, value|
                skip = true unless value.include?(event[key])
            end
            
            @excludes.each do |key, value|
                skip = true if value.include?(event[key])
            end
            next if skip
            
            if @use_json
                yield event, current_time
            else
                yield GHArchive::Event.parse(event), current_time
            end
        end
        
        @logger.info("Scanned #{current_time}")
        
        events.clear
        GC.start
    end
    
    update_checkpoint(to)
    
    return exceptions
end
exclude(**args) click to toggle source
# File lib/gh-archive.rb, line 161
def exclude(**args)
    args.each do |key, value|
        @excludes[key.to_s] = [] unless @excludes[key.to_s]
        @excludes[key.to_s] << value
    end
    
    return self
end
get(date) click to toggle source
# File lib/gh-archive.rb, line 148
def get(date)
    raise "Not implemented"
end
include(**args) click to toggle source
# File lib/gh-archive.rb, line 152
def include(**args)
    args.each do |key, value|
        @includes[key.to_s] = [] unless @includes[key.to_s]
        @includes[key.to_s] << value
    end
    
    return self
end
logger=(logger) click to toggle source
# File lib/gh-archive.rb, line 141
def logger=(logger)
    @logger = logger
    
    return self
end
Also aliased as: use_logger
parse_events() click to toggle source
# File lib/gh-archive.rb, line 135
def parse_events
    @use_json = false
    
    return self
end
restore_checkpoint(from) click to toggle source
# File lib/gh-archive.rb, line 170
def restore_checkpoint(from)
    if @checkpoint_name && FileTest.exist?(@checkpoint_name)
        # Note that this throws an exception if the file is not readable. This is the intended behavior.
        # As opposed to that, failing to save the checkpoint information just results in a warning on the log.
        loaded_from = Marshal.load(File.read(@checkpoint_name))
        raise "The loaded checkpoint (#{loaded_from}) occurs before the current from date (#{from})" if loaded_from < from
        
        @logger.info("Valid checkpoint loaded. Restored execution from #{loaded_from}.")
        
        return loaded_from
    else
        return from
    end
end
update_checkpoint(current_time) click to toggle source
# File lib/gh-archive.rb, line 185
def update_checkpoint(current_time)
    if @checkpoint_name
        begin
            File.open(@checkpoint_name, "wb") do |f|
                f.write(Marshal.dump(current_time))
            end
        rescue
            @logger.warn(
                "Unable to save the checkpoint at the specified location (#{File.expand_path(@checkpoint_name)})."
            )
        end
    end
end
use_checkpoint(filename) click to toggle source
# File lib/gh-archive.rb, line 129
def use_checkpoint(filename)
    @checkpoint_name = filename
    
    return self
end
use_logger(logger)
Alias for: logger=