Skip to content

Commit

Permalink
Extract logic related zip file
Browse files Browse the repository at this point in the history
  • Loading branch information
mcls committed Apr 25, 2015
1 parent f6de2b4 commit 1cc0bcc
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 34 deletions.
40 changes: 6 additions & 34 deletions app/models/project/download.rb
Expand Up @@ -15,7 +15,9 @@ def initialize(project, logger: nil, http_client: HTTPClient)
def call
project.source_files.delete_all
Tempfile.create([filename, ".zip"], :encoding => 'ascii-8bit') do |zip_file|
http_client.get_content(project.download_zip_url) { |chunk| zip_file.write(chunk) }
http_client.get_content(project.download_zip_url) { |chunk|
zip_file.write(chunk)
}
rescue_reopen_error { unzip_to_source_files(zip_file) }
end
end
Expand All @@ -42,38 +44,8 @@ def rescue_reopen_error

# @param file [String]
def unzip_to_source_files(file)
Zip::File.open_buffer(file) do |zip_file|
@project.has_todo = zip_file.glob('**/.rubocop_todo.yml').present?
zip_file.glob('**/*.rb').compact.map do |entry|
next if ignore_file?(entry.name)
to_source_file(entry)
end
end
end

def ignore_file?(filename)
SourceFile::IgnoreCheck.call(filename)
end

def to_source_file(entry)
sf = SourceFile.new(project: project, path: sanitize_file_path(entry))
input_stream = entry.get_input_stream
sf.content = input_stream.read if input_stream.respond_to?(:read)
sf.save!
sf
rescue ActiveRecord::StatementInvalid => e
logger.warn { "While inserting SourceFile content, ignoring exception: #{e.inspect}" }
sf.update_attribute(:content, nil)
sf
rescue StandardError => e
logger.warn "FAILED ON FILE: #{entry.name} #{e.inspect}"
nil
end

# Pops of the name of the project directory ( == name of zip file )
def sanitize_file_path(entry)
parts = entry.name.split(File::SEPARATOR)
parts.shift
parts.join(File::SEPARATOR)
zipfile = Project::ZipFile.new(file, project: project)
project.has_todo = zipfile.has_rubocop_todos?
zipfile.source_files
end
end
64 changes: 64 additions & 0 deletions app/models/project/zip_file.rb
@@ -0,0 +1,64 @@
class Project::ZipFile
attr_reader :filepath, :project

def initialize(filepath, project:, logger: Rails.logger)
@filepath = filepath
@project = project
@parsed = false
@logger = logger
end

def source_files
parse_file
@source_files
end

def has_rubocop_todos?
parse_file
!!@has_rubocop_todos
end

private

attr_reader :logger

def parse_file
return if @parsed
Zip::File.open_buffer(filepath) do |zf|
@has_rubocop_todos = zf.glob('**/.rubocop_todo.yml').present?
@source_files = zf.glob('**/*.rb').compact.map do |entry|
next if ignore_file?(entry.name)
to_source_file(entry)
end
end
@parsed = true
end

def ignore_file?(filename)
SourceFile::IgnoreCheck.call(filename)
end

def to_source_file(entry)
sf = SourceFile.new(project: project, path: sanitize_file_path(entry))
input_stream = entry.get_input_stream
sf.content = input_stream.read if input_stream.respond_to?(:read)
sf.save!
sf
rescue ActiveRecord::StatementInvalid => e
logger.warn {
"While inserting SourceFile content, ignoring exception: #{e.inspect}"
}
sf.update_attribute(:content, nil)
sf
rescue StandardError => e
logger.warn "FAILED ON FILE: #{entry.name} #{e.inspect}"
nil
end

# Pops of the name of the project directory ( == name of zip file )
def sanitize_file_path(entry)
parts = entry.name.split(File::SEPARATOR)
parts.shift
parts.join(File::SEPARATOR)
end
end

0 comments on commit 1cc0bcc

Please sign in to comment.