Sha256: 2440bfc90fa4b8a603c54f6bdedc5225e0a36cc64134fa373b4fdbab61b974c6

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

module Washbullet
  class Pushable
    class File < Washbullet::Pushable
      def push
        raise MissingParameter unless params.keys == required_parameters

        file_name = params[:file_name]
        file_path = params[:file_path]

        upload_file(file_name, file_path) do |data|
          payload = {
            file_name: data['file_name'],
            file_type: data['file_type'],
            file_url:  data['file_url'],
            body:      params['body'],
            type:      type
          }

          payload = specify_receiver(payload)

          client.post('/v2/pushes', payload)
        end
      end

      def type
        :file
      end

      def required_parameters
        %i(file_name file_path body)
      end

      private

      def upload_file(file_name, file_path, &block)
        mime_type = MIME::Types.type_for(file_path).first.to_s

        data = upload_request(file_name, mime_type)

        upload_url = data.body['upload_url']
        params     = data.body['data']

        io = Faraday::UploadIO.new(file_path, mime_type)

        client.post upload_url, params.merge(file: io)

        yield data.body
      end

      def upload_request(file_name, mime_type)
        client.post '/v2/upload-request', file_name: file_name, file_type: mime_type
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
washbullet-0.4.0 lib/washbullet/pushable/file.rb