Class: Gift::Recipient

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#hostObject

Returns the value of attribute host.



8
9
10
# File 'lib/recipient.rb', line 8

def host
  @host
end

#idObject

Returns the value of attribute id.



8
9
10
# File 'lib/recipient.rb', line 8

def id
  @id
end

#passwordObject

Returns the value of attribute password.



8
9
10
# File 'lib/recipient.rb', line 8

def password
  @password
end

#pathObject

Returns the value of attribute path.



8
9
10
# File 'lib/recipient.rb', line 8

def path
  @path
end

#portObject

Returns the value of attribute port.



8
9
10
# File 'lib/recipient.rb', line 8

def port
  @port
end

#usernameObject

Returns the value of attribute username.



8
9
10
# File 'lib/recipient.rb', line 8

def username
  @username
end

Class Method Details

.allObject



134
135
136
# File 'lib/recipient.rb', line 134

def self.all
  YAML::load_file('.gift/recipients.yml')
end

.find_by_id(id) ⇒ Object



129
130
131
132
# File 'lib/recipient.rb', line 129

def self.find_by_id(id)
  #search recipients.yml for id
  YAML::load_file('.gift/recipients.yml')
end

Instance Method Details

#connectObject



120
121
122
123
# File 'lib/recipient.rb', line 120

def connect
  @connection = Net::FTP.new(host, username, password)
  @connection.chdir(path)
end

#disconnectObject



125
126
127
# File 'lib/recipient.rb', line 125

def disconnect
  @connection.close
end

#last_remote_commitObject



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/recipient.rb', line 81

def last_remote_commit
  sha = ""
  connect
  @connection.chdir('.gift/deliveries')
  if @connection.nlst.length > 2
    @connection.gettextfile(@connection.nlst.last) do |f|
      sha = f
    end
  end
  disconnect
  
  sha
end

#saveObject



109
110
111
112
113
114
115
116
117
118
# File 'lib/recipient.rb', line 109

def save
  unless self.id
    self.id = "ftp-1"
  end
  
  yaml = YAML::dump(self)
  fp = open('.gift/recipients.yml', 'w')
  fp.write(yaml)
  fp.close
end

#save_commit(sha) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/recipient.rb', line 95

def save_commit(sha)
  file_name = ".gift/deliveries/#{id}/#{Time.now.to_i.to_s}"
  fp = File.open(file_name, "w")
  fp.puts sha
  fp.close
  
  connect
  @connection.chdir('.gift/deliveries')
  @connection.puttextfile(file_name)
  disconnect
  
  puts "Remote state saved"
end

#setup_local_dirsObject



38
39
40
41
42
# File 'lib/recipient.rb', line 38

def setup_local_dirs
  File.makedirs '.gift' unless File.exists?('.gift')
  File.makedirs '.gift/deliveries' unless File.exists?('.gift/deliveries')
  File.makedirs ".gift/deliveries/#{self.id}"# unless File.exists?(".gift/deliveries/#{self.id}")
end

#setup_remote_dirsObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/recipient.rb', line 20

def setup_remote_dirs
  connect
  unless @connection.nlst.include?('.gift')
    @connection.mkdir('.gift')
  else
    puts 'Remote gift directory already exists'
  end
  
  @connection.chdir('.gift')
  
  unless @connection.nlst.include?('deliveries')
    @connection.mkdir('deliveries')
  else
    puts 'Remote deliveries directory already exists'
  end
  disconnect
end

#update_remoteObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/recipient.rb', line 44

def update_remote
  repo = Git.open('./')
  
  remote_commit = last_remote_commit
  remote_commit = repo.log.to_a.last.sha if remote_commit == ""
        
  file_count = 0  
  
  puts("Last remote commit: #{remote_commit} | Current local commit: #{repo.log.to_a.first.sha}")
  
  connect
  repo.diff(remote_commit, repo.log.to_a.first.sha).each do |file|
    unless(file.path.split('/').first == ".gift")
      if file.type == "deleted"
        begin
          size = @connection.size(file.path)
          puts "Deleting #{file.path} [          ] 0%"
          delete_remote_file(file.path)
        rescue Exception => e
          puts "Delete skipped #{file.path} (file not found)"
        end
      else
        puts "Uploading #{file.path} [          ] 0%"
        upload_file(file.path)
      end
    end
    file_count += 1
  end
  disconnect
  
  if file_count == 0
    puts "Everything up to date!"
  else
    save_commit(repo.log.to_a.first.sha)
  end
end

#valid_connection?Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
16
17
18
# File 'lib/recipient.rb', line 10

def valid_connection?
  begin
    connect
    disconnect
    true
  rescue Exception => e
    false
  end
end