Class: Akamaized::Connection

Inherits:
Object
  • Object
show all
Includes:
Exception
Defined in:
lib/akamaized/connection.rb

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Connection

Returns a new instance of Connection.



29
30
31
32
# File 'lib/akamaized/connection.rb', line 29

def initialize(config = {})
  @config = Config.new(config)
  create_connection
end

Instance Method Details

#create_connectionObject



35
36
37
38
39
40
41
42
43
44
# File 'lib/akamaized/connection.rb', line 35

def create_connection
  begin
    @connection = Net::FTP.new(@config.host)
    @connection.passive = true
    @connection.(@config.username, @config.password)
    @connection.chdir(@config.base_dir) if @config.base_dir
  rescue IOError, SystemCallError, Net::FTPError => e
    raise ConnectionError.new(e.message)
  end
end

#delete(file, location = nil) ⇒ Object

Will attempt to login to the server an remove a file. Returns false if unable to delete the file



77
78
79
80
81
82
83
84
# File 'lib/akamaized/connection.rb', line 77

def delete(file, location = nil)
  begin
    @connection.chdir(location) unless location.nil?
    @connection.delete(file)
  rescue Exception => e
    false
  end
end

#delete!(file, location = nil) ⇒ Object

Calls the delete method, however, will raise an exception if there was an issue whereas “delete” itself will swallow all errors

Raises:



89
90
91
# File 'lib/akamaized/connection.rb', line 89

def delete!(file, location = nil)
  raise DeleteError.new(file) unless delete(file, location)
end

#exists?(file, location = nil) ⇒ Boolean

Check to see if a file

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
56
# File 'lib/akamaized/connection.rb', line 48

def exists?(file, location = nil)
  begin
    @connection.chdir(location) unless location.nil?
    !@connection.nlst(file).empty?
  rescue Net::FTPPermError => e
    return false if e.message.include?("not exist")
    raise
  end
end

#get(remote, local, location = nil) ⇒ Object

Get a file off of the server



69
70
71
72
# File 'lib/akamaized/connection.rb', line 69

def get(remote, local, location = nil)
  @connection.chdir(location) unless location.nil?
  @connection.get(remote, "#{local}/#{File.basename(remote)}")
end

#put(file, location = nil) ⇒ Object

Take a local file and push it up to akamai Will raise an exception if unable to push the file up or the file is empty after upload (0 bytes)



62
63
64
65
# File 'lib/akamaized/connection.rb', line 62

def put(file, location = nil)
  @connection.chdir(location) unless location.nil?
  @connection.put(file, File.basename(file))
end