Module: Uploader

Defined in:
lib/helpers/uploader.rb

Constant Summary collapse

REMOTE_URL =
"http://zenkeeper.io/api/v1/dependency_files"
ERROR_CODES =
{
  "404" => "Project not found. Please make sure your credentials are valid."
}

Class Method Summary collapse

Class Method Details

.upload(project_token:, forced_date: nil, sha: nil, comment: nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/helpers/uploader.rb', line 7

def self.upload(project_token:, forced_date: nil, sha: nil, comment: nil)
  upload_tag = ::SecureRandom.hex(32)
  %w(Gemfile Gemfile.lock).map do |file_name|
    puts file_name

    puts "\tReading..."
    begin
      file = File.open(file_name, "rb")
    rescue Errno::ENOENT
      puts "\t\tNo file!"
      next
    end
    contents = file.read

    puts "\tUploading..."
    res = Net::HTTP.post_form(
      URI(REMOTE_URL),
      content: contents,
      file_type: file_name.downcase.gsub("\.", "_"),
      project_token: project_token,
      upload_tag: upload_tag,
      forced_date: forced_date,
      sha: sha,
      comment: comment,
      client_version: Zenkeeper::VERSION
    )

    if res.code == "200"
      puts "\t" + JSON.parse(res.body)["info"]
    else
      error_message = ERROR_CODES[res.code]
      if error_message
        puts "\tFailure: #{error_message}"
      else
        puts "\tUnknown error, please contact support."
      end
    end
  end
end