Sha256: 2fb36e8b93d6852f3438f6c9ad9eff4f8b825fb49b308300b5f30dc35d5a298a

Contents?: true

Size: 1003 Bytes

Versions: 1

Compression:

Stored size: 1003 Bytes

Contents

require 'celluloid'
require 'mime/types'

module Buildbox
  class Uploader
    include Celluloid
    include Celluloid::Logger

    def upload(credentials, absolute_path, &block)
      info "Uploading #{absolute_path}"

      action = credentials[:action]
      data   = credentials[:data]

      connection = Faraday.new(:url => action[:url]) do |faraday|
        faraday.request :multipart

        faraday.options[:timeout] = 60
        faraday.options[:open_timeout] = 60

        faraday.adapter Faraday.default_adapter
      end

      mime_type = MIME::Types.type_for(absolute_path)[0].to_s
      file_input_key = action[:file_input]

      form_data = credentials[:data].to_hash.dup
      form_data[file_input_key] = Faraday::UploadIO.new(absolute_path, mime_type)

      yield("uploading")

      response = connection.post(action[:path], form_data)

      info "Finished uploading #{File.basename(absolute_path)} with a status of #{response.status}"

      yield("finished")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
buildbox-0.6.beta1 lib/buildbox/uploader.rb