Sha256: 243e9d67feb63d91b440ce4465d597bf46513337ae4a4532a5ad0ba97c25710f

Contents?: true

Size: 1.68 KB

Versions: 10

Compression:

Stored size: 1.68 KB

Contents

module Octobat
  class FileUpload < APIResource
    include Octobat::APIOperations::Create
    extend Octobat::APIOperations::List
    
    def self.url
      "/files"
    end
    
    
    def self.create(params = {}, opts = {})
      if params[:attachment] && !params[:attachment].is_a?(String)
        unless params[:attachment].respond_to?(:read)
          raise ArgumentError, "attachment must respond to `#read`"
        end
      end
      
      api_key, headers = Util.parse_opts(opts)
      headers = headers.merge(content_type: MultipartEncoder::MULTIPART_FORM_DATA)
      
      response, api_key = Octobat.request(:post, self.url, api_key, params, headers, Octobat.uploads_base)
      Util.convert_to_octobat_object(response, api_key)
      
    end
        
    def refresh
      response, api_key = Octobat.request(:get, url, @api_key, @retrieve_options, @headers, Octobat.uploads_base)
      refresh_from(response, api_key)
    end
    
    
    def self.list(filters={}, opts={})
      set_parent_resource(filters)
      api_key, headers = Util.parse_opts(opts)
      
      api_key ||= @api_key

      f = filters.select{|request_filter| !@parent_resource.has_key?(request_filter)}

      response, api_key = Octobat.request(:get, url, api_key, f, headers, Octobat.uploads_base)
      obj = ListObject.construct_from(response, api_key)

      obj.filters = filters.dup
      obj.cursors[:ending_before] = obj.filters.delete(:ending_before)
      obj.cursors[:starting_after] = obj.filters.delete(:starting_after)

      obj.filters.delete(:expand)
      obj.parent_resource = @parent_resource

      obj
    end
    
    self.singleton_class.send(:alias_method, :all, :list)

    
    
    
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
octobat-2.0.27 lib/octobat/file_upload.rb
octobat-2.0.26 lib/octobat/file_upload.rb
octobat-2.0.25 lib/octobat/file_upload.rb
octobat-2.0.24 lib/octobat/file_upload.rb
octobat-2.0.23 lib/octobat/file_upload.rb
octobat-2.0.22 lib/octobat/file_upload.rb
octobat-2.0.21 lib/octobat/file_upload.rb
octobat-2.0.20 lib/octobat/file_upload.rb
octobat-2.0.19 lib/octobat/file_upload.rb
octobat-2.0.18 lib/octobat/file_upload.rb