Class: Gift::Recipient
- Inherits:
-
Object
- Object
- Gift::Recipient
- Defined in:
- lib/recipient.rb
Instance Attribute Summary collapse
-
#host ⇒ Object
Returns the value of attribute host.
-
#id ⇒ Object
Returns the value of attribute id.
-
#password ⇒ Object
Returns the value of attribute password.
-
#path ⇒ Object
Returns the value of attribute path.
-
#port ⇒ Object
Returns the value of attribute port.
-
#username ⇒ Object
Returns the value of attribute username.
Class Method Summary collapse
Instance Method Summary collapse
- #connect ⇒ Object
- #disconnect ⇒ Object
- #last_remote_commit ⇒ Object
- #save ⇒ Object
- #save_commit(sha) ⇒ Object
- #setup_local_dirs ⇒ Object
- #setup_remote_dirs ⇒ Object
- #update_remote ⇒ Object
- #valid_connection? ⇒ Boolean
Instance Attribute Details
#host ⇒ Object
Returns the value of attribute host.
8 9 10 |
# File 'lib/recipient.rb', line 8 def host @host end |
#id ⇒ Object
Returns the value of attribute id.
8 9 10 |
# File 'lib/recipient.rb', line 8 def id @id end |
#password ⇒ Object
Returns the value of attribute password.
8 9 10 |
# File 'lib/recipient.rb', line 8 def password @password end |
#path ⇒ Object
Returns the value of attribute path.
8 9 10 |
# File 'lib/recipient.rb', line 8 def path @path end |
#port ⇒ Object
Returns the value of attribute port.
8 9 10 |
# File 'lib/recipient.rb', line 8 def port @port end |
#username ⇒ Object
Returns the value of attribute username.
8 9 10 |
# File 'lib/recipient.rb', line 8 def username @username end |
Class Method Details
.all ⇒ Object
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
#connect ⇒ Object
120 121 122 123 |
# File 'lib/recipient.rb', line 120 def connect @connection = Net::FTP.new(host, username, password) @connection.chdir(path) end |
#disconnect ⇒ Object
125 126 127 |
# File 'lib/recipient.rb', line 125 def disconnect @connection.close end |
#last_remote_commit ⇒ Object
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 |
#save ⇒ Object
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_dirs ⇒ Object
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_dirs ⇒ Object
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_remote ⇒ Object
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
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 |