Class: DhEasy::Login::Flow

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Instance Attribute Details

#app_configDhEasy::Core::Config

App configuration to store login flow configuration.

Returns:

  • (DhEasy::Core::Config)


10
11
12
# File 'lib/dh_easy/login/flow.rb', line 10

def app_config
  @app_config
end

#collectionString

Output collection used to store held pages to be fixed and fetched.

Returns:

  • (String)


14
15
16
# File 'lib/dh_easy/login/flow.rb', line 14

def collection
  @collection
end

#config_keyString

Key to be used to store login flow configuration data at #app_config.

Returns:

  • (String)


22
23
24
# File 'lib/dh_easy/login/flow.rb', line 22

def config_key
  @config_key
end

#custom_fixProc, ...

Block with custom user defined fixing logic to be executed on help page

fix.

Returns:

  • (Proc, Lambda, nil)


31
32
33
# File 'lib/dh_easy/login/flow.rb', line 31

def custom_fix
  @custom_fix
end

#keep_response_keysBoolean

Force held pages to keep response_keys on save.

Returns:

  • (Boolean)

    ‘true` when enabled, else `false`.



35
36
37
# File 'lib/dh_easy/login/flow.rb', line 35

def keep_response_keys
  @keep_response_keys
end

#per_pageInteger

Batch size used on query outputs.

Returns:

  • (Integer)


18
19
20
# File 'lib/dh_easy/login/flow.rb', line 18

def per_page
  @per_page
end

#vars_keyString

Page’s “vars” key to store the login cookie used to fetch.

Returns:

  • (String)


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.

Parameters:

  • new_page (Hash)

    Page to add login flow reserved vars.



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.

Parameters:

  • held_page (Hash)

    Held page to clean.



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

#configHash

Gets the existing login flow config or load it from app config when

invalid.

Returns:

  • (Hash)


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.

Parameters:

  • held_page (Hash)

    Held page to fix.



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.

Returns:

  • (Boolean)

    ‘true` when expired, else `false`.



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.

Parameters:

  • held_page (Hash)

    Held page to fix.



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
  cookie_key = held_page.has_key?(:cookie) ? :cookie : 'cookie'
  headers_key = held_page.has_key?(:headers) ? :headers : 'headers'
  held_page[cookie_key] = merge_cookie held_page[cookie_key]
  held_page[headers_key] = {} unless held_page.has_key? headers_key
  header_cookie_key = held_page[headers_key].has_key?('cookie') ? 'cookie' : 'Cookie'
  held_page[headers_key][header_cookie_key] = '' unless  held_page[headers_key].has_key? header_cookie_key
  held_page[headers_key][header_cookie_key] = merge_cookie held_page[headers_key][header_cookie_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 &
  # Expire cookie when same as current page
  old_cookie_hash = DhEasy::Core::Helper::Cookie.parse_from_request page['vars'][vars_key]
  newest_cookie_hash = DhEasy::Core::Helper::Cookie.parse_from_request config['cookie']
  same_cookie = DhEasy::Core::Helper::Cookie.include? old_cookie_hash, newest_cookie_hash
  expire! if same_cookie && !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'
    )
    .call unless .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

Note:

opts` will default to a predefined list whenever `nil`.

Hook to initialize login flow configuration.

Parameters:

  • opts (Hash) (defaults to: {})

    ({}) Configuration options.

Options Hash (opts):

  • :app_config (DhEasy::Core::Config) — default: nil

    App configuration to use.

  • :per_page (Integer) — default: 100

    Batch size used on query outputs.

  • :collection (String) — default: 'login_flow_held_pages'

    Output collection used to store held pages to be fixed and fetched.

  • :config_key (String) — default: 'login_flow'

    Key to be used to store login flow configuration data at #app_config.

  • :vars_key (String) — default: 'login_flow_cookie'

    Page’s “vars” key to store the login cookie used to fetch.

  • :reponse_keys (String) — default: nil

    Response keys to remove from a held page before enqueue it again.

  • :custom_fix (Block) — default: nil

    Custom block to be executed on fixing a held page.

Raises:

  • (ArgumentError)

    When ‘opts` is not provided or `nil`.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/dh_easy/login/flow.rb', line 59

def  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

Updates an old cookie with current cookie saved on app configuration.

Parameters:

  • old_cookie (String, Array, Hash)

    Old cookie to update.



207
208
209
210
# File 'lib/dh_easy/login/flow.rb', line 207

def merge_cookie old_cookie
  return config['cookie'] if old_cookie.nil?
  DhEasy::Core::Helper::Cookie.update old_cookie, config['cookie']
end

#reload_configObject

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.

Parameters:

  • key (String)

    Key name to check.

Returns:

  • (Boolean)

    ‘true` when key name is a response key, else `false`.



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_keysArray<String>

Response keys to remove from a held page before enqueue it again.

Returns:

  • (Array<String>)


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.

Parameters:

  • value (Array<String>)

    Response key list.



128
129
130
# File 'lib/dh_easy/login/flow.rb', line 128

def response_keys= value
  @response_keys = value
end

#restore_held_pagesObject

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.

Returns:

  • (Boolean)

    ‘true` if seeded, else `false`.



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.

Parameters:

  • data (Hash) (defaults to: {})

    ({}) Data to be saved on 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