Module: Googletastic::Sync::Form::ClassMethods

Defined in:
lib/googletastic/sync/form.rb

Instance Method Summary collapse

Instance Method Details

#cleanup(syncables, options) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/googletastic/sync/form.rb', line 87

def cleanup(syncables, options)
  syncables.each do |syncable|
    syncable.synced_at = Time.now
    syncable.save!
    path = File.join(options[:folder], options[:key], syncable.formkey)
    path += options[:ext] if options.has_key?(:ext)
    begin
      File.delete(path)
    rescue Exception => e
      
    end
  end
end

#process(forms, options = {}) ⇒ Object



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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/googletastic/sync/form.rb', line 28

def process(forms, options = {})
  options[:key] ||= "forms"
  # defaults to a reasonable limit (3MB) for heroku
  options[:max_size] ||= 3000000
  # per max_size chunk
  forms_processed = []
  # total
  updated_forms = []
  counted_size = 0
  forms.each_with_index do |form, index|
    if form.formkey.nil?
      puts "Missing formkey... #{form.title}"
      if forms_processed.length > 0 and index == forms.length - 1
        Googletastic::Sync.push(options[:username], options[:password], options)
        cleanup(forms_processed, options.merge(:ext => ".html"))
      end
      next
    end
    remote  = form.remote
    content = nil
    puts "Processing Form... #{form.title}"
    begin
      form.remote.form_key = form.formkey
      title = form.formkey + ".html"
      content = form.remote.body
      
      tempfile = Tempfile.new("googltastic-form-tempfiles-#{title}-#{Time.now}-#{rand(10000)}")
      tempfile.write(content)
      
      path = File.join(options[:folder], options[:key])
      Dir.mkdir(path) unless File.exists?(path)
      path = File.join(path, title)
      
      # if we have passed the 5MB (or our 3MB) limit,
      # then push the files to GAE
      if tempfile.size + counted_size >= options[:max_size] || index == forms.length - 1
        Googletastic::Sync.push(options[:username], options[:password], options)
        cleanup(forms_processed, options.merge(:ext => ".html"))
        counted_size = 0
        forms_processed = []
      end
      
      content = Googletastic::PrettyPrint.xml(content)
      
      File.open(path, 'w') {|f| f.write(content) }
      forms_processed << form
      counted_size += tempfile.size
      tempfile.close

      updated_forms << form
      
    rescue Exception => e
      puts "Error... #{e.inspect}"
    end
  end
  
  updated_forms
end

#sync(forms, options = {}, &block) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/googletastic/sync/form.rb', line 9

def sync(forms, options = {}, &block)
  updated = process(forms, options)
  key = options[:key].to_s
  data = {key => []}
  updated.each do |form|
    data[key] << {
      :id => form.remote.id,
      :formkey => form.formkey
    }
    yield(data, key, form) if block_given?
  end
  response = Googletastic::Sync.post(
    :url => options[:url],
    :path => options[:path],
    :format => :json,
    :data => data
  )
end