Class: AsyncEndpoint::AsyncRequest
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- AsyncEndpoint::AsyncRequest
show all
- Defined in:
- app/models/async_endpoint/async_request.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
42
43
44
45
46
47
48
49
|
# File 'app/models/async_endpoint/async_request.rb', line 42
def method_missing(method, *args)
if method.to_s.ends_with?('=')
key = method.to_s.chomp('=').to_sym
self.response = response_hash.merge(key => args.first).to_json
else
response_hash[method]
end
end
|
Instance Attribute Details
#controller ⇒ Object
Returns the value of attribute controller.
4
5
6
|
# File 'app/models/async_endpoint/async_request.rb', line 4
def controller
@controller
end
|
Class Method Details
.init(controller, hash_params) ⇒ Object
6
7
8
9
10
11
12
13
14
15
16
17
|
# File 'app/models/async_endpoint/async_request.rb', line 6
def self.init(controller, hash_params)
if controller.params[:async_request_id].present?
async_request = find(controller.params[:async_request_id])
unless async_request.token_is_valid(controller.params[:async_request_token])
fail 'Authenticity token is not valid'
end
else
async_request = create(params: hash_params.to_json)
end
async_request.controller = controller
async_request
end
|
Instance Method Details
#done ⇒ Object
19
20
21
|
# File 'app/models/async_endpoint/async_request.rb', line 19
def done
self.status = AsyncRequest.statuses[:done]
end
|
#error_message ⇒ Object
28
29
30
|
# File 'app/models/async_endpoint/async_request.rb', line 28
def error_message
response_hash[:error_message]
end
|
#execute ⇒ Object
60
61
62
63
|
# File 'app/models/async_endpoint/async_request.rb', line 60
def execute
execute_task
save
end
|
#execute_task ⇒ Object
65
66
67
|
# File 'app/models/async_endpoint/async_request.rb', line 65
def execute_task
fail 'execute_task must be implemented in subclass'
end
|
#failed(message = nil) ⇒ Object
23
24
25
26
|
# File 'app/models/async_endpoint/async_request.rb', line 23
def failed(message = nil)
self.status = AsyncRequest.statuses[:failed]
self.error_message = message
end
|
#failed_or_crashed? ⇒ Boolean
51
52
53
|
# File 'app/models/async_endpoint/async_request.rb', line 51
def failed_or_crashed?
failed? || job_failed?
end
|
#handle(done, fail) ⇒ Object
69
70
71
72
73
74
|
# File 'app/models/async_endpoint/async_request.rb', line 69
def handle(done, fail)
return fail.call if failed_or_crashed?
start_worker if pending?
render_accepted if pending? || processing?
done.call if done?
end
|
#job_failed? ⇒ Boolean
55
56
57
58
|
# File 'app/models/async_endpoint/async_request.rb', line 55
def job_failed?
false
end
|
#params_hash ⇒ Object
32
33
34
35
|
# File 'app/models/async_endpoint/async_request.rb', line 32
def params_hash
return {} unless params.present?
JSON.parse(params).deep_symbolize_keys
end
|
#render_accepted ⇒ Object
76
77
78
|
# File 'app/models/async_endpoint/async_request.rb', line 76
def render_accepted
controller.instance_exec(id, jid, &render_accepted_proc)
end
|
#render_accepted_proc ⇒ Object
80
81
82
83
84
85
|
# File 'app/models/async_endpoint/async_request.rb', line 80
def render_accepted_proc
proc do |async_request_id, jid|
render json: { async_request_id: async_request_id, async_request_token: jid },
status: :accepted
end
end
|
#response_hash ⇒ Object
37
38
39
40
|
# File 'app/models/async_endpoint/async_request.rb', line 37
def response_hash
return {} unless response.present?
JSON.parse(response).deep_symbolize_keys
end
|
#start_worker ⇒ Object
87
88
89
90
|
# File 'app/models/async_endpoint/async_request.rb', line 87
def start_worker
jid = AsyncEndpointWorker.perform_async(id)
update_attributes(jid: jid, status: AsyncRequest.statuses[:processing])
end
|
#token_is_valid(token) ⇒ Object
92
93
94
|
# File 'app/models/async_endpoint/async_request.rb', line 92
def token_is_valid(token)
token.present? && ActiveSupport::SecurityUtils.secure_compare(token, jid)
end
|