Class: Algorithmia::DataFile

Inherits:
DataObject show all
Defined in:
lib/algorithmia/data_file.rb

Instance Attribute Summary

Attributes inherited from DataObject

#data_uri

Instance Method Summary collapse

Methods inherited from DataObject

#basename, #initialize, #parent

Constructor Details

This class inherits a constructor from Algorithmia::DataObject

Instance Method Details

#deleteObject



50
51
52
53
# File 'lib/algorithmia/data_file.rb', line 50

def delete
  Algorithmia::Requester.new(@client).delete(@url)
  true
end

#exists?Boolean

Returns:

  • (Boolean)


6
7
8
9
10
11
# File 'lib/algorithmia/data_file.rb', line 6

def exists?
  Algorithmia::Requester.new(@client).head(@url)
  true
rescue Errors::NotFoundError
  false
end

#getObject



24
25
26
# File 'lib/algorithmia/data_file.rb', line 24

def get
  Algorithmia::Requester.new(@client).get(@url).body
end

#get_fileObject



13
14
15
16
17
18
19
20
21
22
# File 'lib/algorithmia/data_file.rb', line 13

def get_file
  response = get

  tempfile = Tempfile.open(File.basename(@url)) do |f|
    f.write response
    f
  end

  File.new(tempfile.path)
end

#put(data) ⇒ Object Also known as: put_json



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/algorithmia/data_file.rb', line 28

def put(data)
  content_type = case
    when data.kind_of?(String) && data.encoding == Encoding::ASCII_8BIT
      'application/octet-stream'
    when data.kind_of?(String)
      'text/plain'
    else
      'application/json'
  end

  headers = {'Content-Type' => content_type }
  Algorithmia::Requester.new(@client).put(@url, data, headers: headers)
  true
end

#put_file(file_path) ⇒ Object



45
46
47
48
# File 'lib/algorithmia/data_file.rb', line 45

def put_file(file_path)
  data = File.binread(file_path)
  put(data)
end