Sha256: 40a502c0e07f7cbb91734be00d681e40b33464cb4ec8881cf2781db060220b82

Contents?: true

Size: 1.27 KB

Versions: 2

Compression:

Stored size: 1.27 KB

Contents

# encoding: utf-8
#
module Mode
  module Sdk
    # Represents a Mode Upload containing raw CSV with no header
    #
    class Upload
      attr_reader :content, :options

      # Construct a new Upload instance
      #
      # @param content [String, #read] the content
      # @param options [optional, Hash] hash of options
      #
      # @option options [Integer] :content_size the size of the provided
      #   content
      #
      # @return [Mode::Sdk::Upload] the instance
      #
      def initialize(content, options = {})
        @content = content
        @options = options
      end

      # Create the Upload through the Mode API
      #
      # @return [Mode::Sdk::Client::Response] the response
      #
      def create
        @create ||= Mode::Sdk::Client.post(
          collection_path,
          content,
          content_type: 'application/csv',
          content_length: content_length,
          expect: [201])
      end

      # The unique token assigned to the upload
      #
      # @return [String] the token
      #
      def token
        create.body.fetch('token')
      end

      private

      def content_length
        options.fetch(:content_length, nil) || content.size
      end

      def collection_path
        '/api/uploads'
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mode-sdk-0.1.0 lib/mode/sdk/upload.rb
mode-sdk-0.0.1 lib/mode/sdk/upload.rb