Class: DeferredRequest::DeferredRequest

Inherits:
Object
  • Object
show all
Defined in:
app/models/deferred_request/deferred_request.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_request(request) ⇒ Object

request: ActionDispatch::Request create a deferred request from a ActionDispatch::Request



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/models/deferred_request/deferred_request.rb', line 14

def self.from_request(request)
  deferred_request = ::DeferredRequest.deferred_request_instance_class.constantize.new

  deferred_request.controller = request.controller_class.name
  deferred_request.action = request.params["action"]

  deferred_request.url = request.url
  deferred_request.method = request.method
  deferred_request.headers = get_headers(request)
  deferred_request.params = request.params.except(:controller, :action)
  deferred_request.remote_ip = request.remote_ip

  deferred_request
end

.get_headers(request) ⇒ Object



60
61
62
63
64
65
66
67
# File 'app/models/deferred_request/deferred_request.rb', line 60

def self.get_headers(request)
  # Get the request headers from the request
  {}.tap do |t|
    request.headers.each do |key, value|
      t[key] = value if key.downcase.starts_with?("http")
    end
  end
end

.perform_later_from_request!(request) ⇒ Object



29
30
31
32
33
34
35
36
# File 'app/models/deferred_request/deferred_request.rb', line 29

def self.perform_later_from_request!(request)
  deferred_request = DeferredRequest.from_request(request)
  deferred_request.save!

  deferred_request.perform_later

  deferred_request
end

Instance Method Details

#perform!Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/models/deferred_request/deferred_request.rb', line 42

def perform!
  begin
    self.status = :processing
    save!

    klass = controller.constantize.new

    self.result = klass.send("#{action}_deferred".to_sym, self)
    self.status = :complete
  rescue => e
    Rails.logger.error("DeferredRequest::DeferredRequestJob: #{e.message}")
    self.result = e.message
    self.status = :error
  end

  save!
end

#perform_laterObject



38
39
40
# File 'app/models/deferred_request/deferred_request.rb', line 38

def perform_later
  DeferredRequestJob.perform_later(id)
end