Class: DhEasy::Login::Flow
- Inherits:
-
Object
- Object
- DhEasy::Login::Flow
- Includes:
- Core::Plugin::Executor
- Defined in:
- lib/dh_easy/login/flow.rb
Overview
Login flow executor designed to recover from an expired or invalid
session.
Instance Attribute Summary collapse
-
#app_config ⇒ DhEasy::Core::Config
App configuration to store login flow configuration.
-
#collection ⇒ String
Output collection used to store held pages to be fixed and fetched.
-
#config_key ⇒ String
Key to be used to store login flow configuration data at #app_config.
-
#custom_fix ⇒ Proc, ...
Block with custom user defined fixing logic to be executed on help page fix.
-
#keep_response_keys ⇒ Boolean
Force held pages to keep response_keys on save.
-
#per_page ⇒ Integer
Batch size used on query outputs.
-
#vars_key ⇒ String
Page’s “vars” key to store the login cookie used to fetch.
Instance Method Summary collapse
-
#add_vars!(new_page) ⇒ Object
Add login flow reserved vars into a page.
-
#clean_page_response!(held_page) ⇒ Object
Remove all response keys from a held page hash.
-
#config ⇒ Hash
Gets the existing login flow config or load it from app config when invalid.
-
#custom_fix!(held_page) ⇒ Object
Execute #custom_fix block into a held page.
-
#expire! ⇒ Object
Set expire flag as ‘true`.
-
#expired? ⇒ Boolean
Indicates whenever latest session has been set as expired.
-
#fix_page!(held_page) ⇒ Object
Fixes a held page session.
-
#fix_session(&enqueue_login) ⇒ Object
Fixes current page session by enqueue it using latest working session or held it for fix and enqueue login page by executing block.
-
#initialize_hook_login_flow(opts = {}) ⇒ Object
Hook to initialize login flow configuration.
-
#merge_cookie(old_cookie) ⇒ Object
Updates an old cookie with current cookie saved on app configuration.
-
#reload_config ⇒ Object
Invalidates login flow config, so it is reloaded on next config usage.
-
#response_key?(key) ⇒ Boolean
Check whenever a key name is categorized as a response key.
-
#response_keys ⇒ Array<String>
Response keys to remove from a held page before enqueue it again.
-
#response_keys=(value) ⇒ Object
Set response key list to remove from a page before enqueue it again.
-
#restore_held_pages ⇒ Object
Restore all held pages stored as outputs.
-
#seeded! ⇒ Object
Set seeded flag as ‘true`.
-
#seeded? ⇒ Boolean
Indicates whenever login flow has been seeded.
-
#update_config(data = {}) ⇒ Object
Updates app configuration.
Instance Attribute Details
#app_config ⇒ DhEasy::Core::Config
App configuration to store login flow configuration.
10 11 12 |
# File 'lib/dh_easy/login/flow.rb', line 10 def app_config @app_config end |
#collection ⇒ String
Output collection used to store held pages to be fixed and fetched.
14 15 16 |
# File 'lib/dh_easy/login/flow.rb', line 14 def collection @collection end |
#config_key ⇒ String
Key to be used to store login flow configuration data at #app_config.
22 23 24 |
# File 'lib/dh_easy/login/flow.rb', line 22 def config_key @config_key end |
#custom_fix ⇒ Proc, ...
Block with custom user defined fixing logic to be executed on help page
fix.
31 32 33 |
# File 'lib/dh_easy/login/flow.rb', line 31 def custom_fix @custom_fix end |
#keep_response_keys ⇒ Boolean
Force held pages to keep response_keys on save.
35 36 37 |
# File 'lib/dh_easy/login/flow.rb', line 35 def keep_response_keys @keep_response_keys end |
#per_page ⇒ Integer
Batch size used on query outputs.
18 19 20 |
# File 'lib/dh_easy/login/flow.rb', line 18 def per_page @per_page end |
#vars_key ⇒ String
Page’s “vars” key to store the login cookie used to fetch.
26 27 28 |
# File 'lib/dh_easy/login/flow.rb', line 26 def vars_key @vars_key end |
Instance Method Details
#add_vars!(new_page) ⇒ Object
Add login flow reserved vars into a page.
215 216 217 218 219 |
# File 'lib/dh_easy/login/flow.rb', line 215 def add_vars! new_page key = new_page.has_key?(:vars) ? :vars : 'vars' new_page[key] = {} if new_page[key].nil? new_page[key][vars_key] = config['cookie'] end |
#clean_page_response!(held_page) ⇒ Object
Remove all response keys from a held page hash.
197 198 199 200 201 202 |
# File 'lib/dh_easy/login/flow.rb', line 197 def clean_page_response! held_page keys = held_page.keys + [] keys.each do |key| held_page.delete key if response_key? key end end |
#config ⇒ Hash
Gets the existing login flow config or load it from app config when
invalid.
141 142 143 144 145 146 147 |
# File 'lib/dh_easy/login/flow.rb', line 141 def config @config ||= nil return @config unless @config.nil? @config = app_config.find_config config_key @config.freeze @config end |
#custom_fix!(held_page) ⇒ Object
Execute #custom_fix block into a held page.
224 225 226 |
# File 'lib/dh_easy/login/flow.rb', line 224 def custom_fix! held_page self.custom_fix.call held_page unless self.custom_fix.nil? end |
#expire! ⇒ Object
Set expire flag as ‘true`.
181 182 183 |
# File 'lib/dh_easy/login/flow.rb', line 181 def expire! update_config 'expired' => true end |
#expired? ⇒ Boolean
Indicates whenever latest session has been set as expired.
176 177 178 |
# File 'lib/dh_easy/login/flow.rb', line 176 def expired? config['expired'] || false end |
#fix_page!(held_page) ⇒ Object
Fixes a held page session.
231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/dh_easy/login/flow.rb', line 231 def fix_page! held_page clean_page_response! held_page = held_page.has_key?(:cookie) ? :cookie : 'cookie' headers_key = held_page.has_key?(:headers) ? :headers : 'headers' held_page[] = held_page[] held_page[headers_key] = {} unless held_page.has_key? headers_key = held_page[headers_key].has_key?('cookie') ? 'cookie' : 'Cookie' held_page[headers_key][] = '' unless held_page[headers_key].has_key? held_page[headers_key][] = held_page[headers_key][] add_vars! held_page custom_fix! held_page end |
#fix_session(&enqueue_login) ⇒ Object
Fixes current page session by enqueue it using latest working session or
held it for fix and enqueue login page by executing block.
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/dh_easy/login/flow.rb', line 246 def fix_session &enqueue_login # Expire cookie when same as current page = DhEasy::Core::Helper::Cookie.parse_from_request page['vars'][vars_key] = DhEasy::Core::Helper::Cookie.parse_from_request config['cookie'] = DhEasy::Core::Helper::Cookie.include? , expire! if && !config['expired'] # Hold self page if config['expired'].nil? || config['expired'] held_page = page.merge({}) clean_page_response!(held_page) unless keep_response_keys save( '_collection' => collection, '_id' => page['gid'], 'page' => held_page, 'fetched' => '0' ) enqueue_login.call unless enqueue_login.nil? return end # Refetch self with new session new_page = {}.merge page fix_page! new_page enqueue new_page end |
#initialize_hook_login_flow(opts = {}) ⇒ Object
‘opts` will default to a predefined list whenever `nil`.
Hook to initialize login flow configuration.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/dh_easy/login/flow.rb', line 59 def initialize_hook_login_flow opts = {} self.per_page = opts[:per_page] || 100 self.collection = opts[:collection] || 'login_flow_held_pages' self.config_key = opts[:config_key] || 'login_flow' self.vars_key = opts[:vars_key] || 'login_flow_cookie' self.response_keys = opts[:reponse_keys] || nil self.app_config = opts[:app_config] || nil self.custom_fix = opts[:custom_fix] || nil self.keep_response_keys = opts[:keep_response_keys] || false if self.app_config.nil? raise ArgumentError.new('":app_config" option is required!') end end |
#merge_cookie(old_cookie) ⇒ Object
Updates an old cookie with current cookie saved on app configuration.
207 208 209 210 |
# File 'lib/dh_easy/login/flow.rb', line 207 def return config['cookie'] if .nil? DhEasy::Core::Helper::Cookie.update , config['cookie'] end |
#reload_config ⇒ Object
Invalidates login flow config, so it is reloaded on next config usage.
133 134 135 |
# File 'lib/dh_easy/login/flow.rb', line 133 def reload_config @config = nil end |
#response_key?(key) ⇒ Boolean
Check whenever a key name is categorized as a response key.
190 191 192 |
# File 'lib/dh_easy/login/flow.rb', line 190 def response_key? key self.response_keys.include? key.to_s.strip end |
#response_keys ⇒ Array<String>
Response keys to remove from a held page before enqueue it again.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/dh_easy/login/flow.rb', line 77 def response_keys @response_keys ||= [ 'content_size', 'content_type', 'created_at', 'effective_url', 'failed_at', 'failed_cid', 'failed_content_size', 'failed_content_type', 'failed_effective_url', 'failed_response_checksum', 'failed_response_cookie', 'failed_response_headers', 'failed_response_proto', 'failed_response_status_code', 'failed_response_status', 'fetched_at', 'fetched_from', 'fetching_at', 'fetching_try_count', 'forced_fetch', 'fresh', 'gid', 'hostname', 'job_id', 'job_status', 'parsed_at', 'parsing_at', 'parsing_fail_count', 'parsing_failed_at', 'parsing_status', 'parsing_try_count', 'parsing_updated_at', 'response_checksum', 'response_cookie', 'response_headers', 'response_proto', 'response_status_code', 'response_status', 'status', 'to_fetch', 'total_failures', 'total_requests', 'total_successes' ] end |
#response_keys=(value) ⇒ Object
Set response key list to remove from a page before enqueue it again.
128 129 130 |
# File 'lib/dh_easy/login/flow.rb', line 128 def response_keys= value @response_keys = value end |
#restore_held_pages ⇒ Object
Restore all held pages stored as outputs.
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/dh_easy/login/flow.rb', line 274 def restore_held_pages current_page = 1 held_pages = find_outputs collection, {'fetched' => '0'}, current_page, per_page while !held_pages&.first.nil? do held_pages.each do |output| # Parse and seed pages held_page = output['page'].is_a?(String) ? JSON.parse(output['page']) : output['page'] fix_page! held_page pages << held_page output['fetched'] = '1' outputs << output end # Fetch next page current_page += 1 enqueue pages save outputs held_pages = find_outputs collection, {'fetched' => '0'}, current_page, per_page end end |
#seeded! ⇒ Object
Set seeded flag as ‘true`.
169 170 171 |
# File 'lib/dh_easy/login/flow.rb', line 169 def seeded! update_config 'seeded' => true end |
#seeded? ⇒ Boolean
Indicates whenever login flow has been seeded. Useful when using files
to held pages.
153 154 155 |
# File 'lib/dh_easy/login/flow.rb', line 153 def seeded? config['seeded'] || false end |
#update_config(data = {}) ⇒ Object
Updates app configuration.
160 161 162 163 164 165 166 |
# File 'lib/dh_easy/login/flow.rb', line 160 def update_config data = {} reload_config new_config = {}.merge(config) new_config.merge!(data) save new_config reload_config end |