Module: PostageApp::FailedRequest

Defined in:
lib/postageapp/failed_request.rb

Class Method Summary collapse

Class Method Details

.initialize_request(uid) ⇒ Object

Initializing PostageApp::Request object from the file



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/postageapp/failed_request.rb', line 49

def self.initialize_request(uid)
  return false if !store_path
  
  if File.exists?(file_path(uid))
    begin
      Marshal.load(File.read(file_path(uid))) 
    rescue
      File.delete(file_path(uid))
      return false
    end
  end
end

.resend_allObject

Attempting to resend failed requests



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
# File 'lib/postageapp/failed_request.rb', line 18

def self.resend_all
  return false if !store_path
  
  Dir.foreach(store_path) do |filename|
    next if !filename.match /^\w{40}$/
    
    request = initialize_request(filename)
    
    receipt_response = PostageApp::Request.new(:get_message_receipt, :uid => filename).send(true)
    if receipt_response.fail?
      return
    elsif receipt_response.ok?
      PostageApp.logger.info "NOT RESENDING FAILED REQUEST [#{filename}]"
      File.delete(file_path(filename)) rescue nil
      
    elsif receipt_response.not_found?
      PostageApp.logger.info "RESENDING FAILED REQUEST [#{filename}]"
      response = request.send(true)
      
      # Not a fail, so we can remove this file, if it was then
      # there will be another attempt to resend
      File.delete(file_path(filename)) rescue nil if !response.fail?
    else
      PostageApp.logger.info "NOT RESENDING FAILED REQUEST [#{filename}], RECEIPT CANNOT BE PROCESSED"
      File.delete(file_path(filename)) rescue nil
    end
  end
  return
end

.store(request) ⇒ Object

Stores request object into a file for future re-send returns true if stored, false if not (due to undefined project path)



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/postageapp/failed_request.rb', line 5

def self.store(request)
  return false if !store_path || !PostageApp.configuration.requests_to_resend.member?(request.method.to_s)
  
  open(file_path(request.uid), 'wb') do |f|
    f.write(Marshal.dump(request))
  end unless File.exists?(file_path(request.uid))
  
  PostageApp.logger.info "STORING FAILED REQUEST [#{request.uid}]"
  
  true
end