Sha256: 70e7bb07b2909a56c6f9d3e1e46c0206c7027444f902466f3ce9833e069085eb

Contents?: true

Size: 1.36 KB

Versions: 6

Compression:

Stored size: 1.36 KB

Contents

module PushbulletRuby
  class Pushable
    class File < Pushable
      def push
        raise MissingParameter unless required_parameters.all? { |e| params.keys.include?(e) }

        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
        [:file_name, :file_path, :body]
      end

      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

6 entries across 6 versions & 1 rubygems

Version Path
pushbullet_ruby-1.1.2 lib/pushbullet_ruby/pushable/file.rb
pushbullet_ruby-1.1.1 lib/pushbullet_ruby/pushable/file.rb
pushbullet_ruby-1.1.0 lib/pushbullet_ruby/pushable/file.rb
pushbullet_ruby-1.0.4.1 lib/pushbullet_ruby/pushable/file.rb
pushbullet_ruby-1.0.4 lib/pushbullet_ruby/pushable/file.rb
pushbullet_ruby-1.0.3 lib/pushbullet_ruby/pushable/file.rb