Class: AdoptAPetAccount

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/adopt_a_pet_account.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new_by_user(params, current_user) ⇒ Object


7
8
9
10
11
12
13
14
# File 'app/models/adopt_a_pet_account.rb', line 7

def self.new_by_user(params, current_user)    
   = self.new(params)
  .user_id = current_user.id
  .active = true
  .organization_id = current_user.organization_id
  .password = SecPass::encrypt(.password)
  
end

Instance Method Details

#ftp_csv(file, current_user) ⇒ Object


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/models/adopt_a_pet_account.rb', line 58

def ftp_csv(file, current_user)
  @account = AdoptAPetAccount.find_by_user_id(user.id)
  @account.password = SecPass::decrypt(@account.password)  #ftp it to adopt a pet account

  require 'net/ftp'
  ftp = Net::FTP.new('autoupload.adoptapet.com')
  ftp.passive = true
  ftp.(user = @account.user_name, passwd = @account.password)
  ftp.putbinaryfile(file.string(), 'pets.csv')
  ftp.putbinaryfile("#{Rails.root}/public/adopt_a_pet/import.cfg", 'import.cfg')
  ftp.quit()
  
  #delete the tmp file
  File.delete("#{Rails.root}/tmp/#{current_user.id}_adopt_a_pet_export_temp.csv")
end

#send_csv(current_user) ⇒ Object


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/models/adopt_a_pet_account.rb', line 16

def send_csv(current_user)
  require 'csv'  #get all animals for an organization

  @animals = Animal.organization(current_user).where(public: 1)
  csv_string = CSV.open("#{Rails.root}/tmp/#{current_user.id}_adopt_a_pet_export_temp.csv", 'w') do |csv|
    # header row 
    csv << [ 
      'ID',
      'Type',
      'Breed',
      'Breed2',
      'Name',
      'Sex',
      'Description',
      'Status',
      'SpayedNeutered',
      'PhotoURL'
      ] 

    # data rows 
    @animals.each do |animal|
      csv << [
                animal.id,
                animal.species_name,
                animal.species_name,
                '',
                animal.name,
                animal.sex,
                animal.special_needs,
                'Available',
                animal.spay,
                animal.image.url(:large).sub(/https:/, "http:")
             ]
    end
  end
  
  #read the newly created csv
  read_csv = CSV.new("#{Rails.root}/tmp/#{current_user.id}_adopt_a_pet_export_temp.csv")  # ftp it to adopt a pet

  self.ftp_csv(read_csv, current_user)
end