Module: GHArchive::Utils

Included in:
Downloader, Provider
Defined in:
lib/gh-archive/core.rb

Instance Method Summary collapse

Instance Method Details

#each_time(from, to) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/gh-archive/core.rb', line 104

def each_time(from, to)
    current_time = from
    while current_time < to
        yield current_time
        current_time += 3600
    end
end

#get_gha_filename(date) ⇒ Object



75
76
77
# File 'lib/gh-archive/core.rb', line 75

def get_gha_filename(date)
    return ("%04d-%02d-%02d-%d.json.gz" % [date.year, date.month, date.day, date.hour])
end

#read_gha_file(file) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/gh-archive/core.rb', line 86

def read_gha_file(file)
    
    if !file.is_a?(StringIO) && file.path.end_with?(".json")
        content = file.read
    elsif file.is_a?(StringIO) || file.path.end_with?(".gz") || file.path.start_with?("/tmp/open-uri")
        content = read_gha_file_content(file)
    else
        raise "Invalid file extension for #{file.path}: expected `.json.gz` or `json`,"
    end
        
    result = []
    content.lines.each do |line|
        result << JSON.parse(line)
    end
    
    return result
end

#read_gha_file_content(gz) ⇒ Object



79
80
81
82
83
84
# File 'lib/gh-archive/core.rb', line 79

def read_gha_file_content(gz)
    gzip = Zlib::GzipReader.new(gz)
    return gzip.read
ensure
    gzip.close if gzip
end