Class: BingoDisk::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/bingo_disk.rb

Overview

A connection to BingoDisk

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username = BingoDisk.username, password = BingoDisk.password) ⇒ Connection

Starts the connection to BingoDisk, requires a block



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/bingo_disk.rb', line 33

def initialize(username = BingoDisk.username, password = BingoDisk.password)
  unless block_given?
    raise ArgumentError.new("No block given")
  end
  
  @username, @password = BingoDisk.make_username(username), password
  
  @uri = URI.parse(BingoDisk.make_host(username))
  Net::HTTP.start(@uri.host) do |http|
    @http = http
    @res = http.head(@uri.request_uri)
    yield self
  end
end

Instance Attribute Details

#last_responseObject (readonly)

Returns the value of attribute last_response.



30
31
32
# File 'lib/bingo_disk.rb', line 30

def last_response
  @last_response
end

Instance Method Details

#create_directory(directory) ⇒ Object

Creates a directory on BingoDisk. Will create parent directories.



58
59
60
61
62
63
64
65
66
# File 'lib/bingo_disk.rb', line 58

def create_directory(directory)
  directory_array = directory.split('/')
  directory_array.shift
  directory_array.each_index do |i|
    req = Net::HTTP::Mkcol.new('/bingo/' + directory_array[0..i].join('/'))
    req.digest_auth(@username, @password, @res)
    @last_response = @http.request(req)
  end
end

#delete(filename, directory = '/') ⇒ Object

Deletes a files from BingoDisk. Will delete it from the optional directory if directory is given.



70
71
72
73
74
# File 'lib/bingo_disk.rb', line 70

def delete(filename, directory = '/')
  req = Net::HTTP::Delete.new("/bingo#{directory}/#{filename}")
  req.digest_auth(@username, @password, @res)
  @last_response = @http.request(req)
end

#download(filename, directory = '/') ⇒ Object

Downloads a file from BringoDisk. Will download from the optional directory if directory is given.

BingoDisk::Connection.new do |bingo|
  open("test.jpg", "wb") do |file|
    file.write(bingo.download)
  end
end


94
95
96
97
98
99
# File 'lib/bingo_disk.rb', line 94

def download(filename, directory = '/')
  req = Net::HTTP::Get.new("/bingo#{directory}/#{filename}")
  req.digest_auth(@username, @password, @res)
  @last_response = @http.request(req)
  @last_response.body
end

#file_exsists?(filename, directory = '/') ⇒ Boolean

Checks if a file exsists on BingoDisk. Will check in the optional directory if directory is given.

Returns:

  • (Boolean)


78
79
80
81
82
83
# File 'lib/bingo_disk.rb', line 78

def file_exsists?(filename, directory = '/')
  req = Net::HTTP::Head.new("/bingo#{directory}/#{filename}")
  req.digest_auth(@username, @password, @res)
  @last_response = @http.request(req)
  return @last_response.code.eql?("200")
end

#upload(filename, file, directory = '/') ⇒ Object

Uploads a open IO object to BingoDisk. The optional directory argument places the files in a certain directory



50
51
52
53
54
55
# File 'lib/bingo_disk.rb', line 50

def upload(filename, file, directory = '/')
  create_directory(directory)
  req = Net::HTTP::Put.new("/bingo#{directory}/#{filename}")
  req.digest_auth(@username, @password, @res)
  @last_response = @http.request(req, file.read)
end