Class: ConsoleUtils::RequestUtils::RequestParams

Inherits:
Object
  • Object
show all
Defined in:
lib/console_utils/request_utils/request_params.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uid_or_params = true, params = nil, headers = nil) ⇒ RequestParams

Returns a new instance of RequestParams


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/console_utils/request_utils/request_params.rb', line 5

def initialize(uid_or_params = true, params = nil, headers = nil)
  # puts "Request params: uid_or_params=#{uid_or_params} | params=#{params} | headers=#{headers}"

  if uid_or_params.is_a? Hash
    headers, params, uid_or_params = [params, uid_or_params, nil]
  end

  @params  = params  if params
  @headers = headers if headers
  @uid = auto_auth? && ((uid_or_params.nil? || uid_or_params == true) ? ConsoleUtils.default_uid : uid_or_params)

  ConsoleUtils.logger.debug { "#{uid}, #{params()}, #{headers()}" }

  auth_automator.(self)
end

Instance Attribute Details

#uidObject

Returns the value of attribute uid


3
4
5
# File 'lib/console_utils/request_utils/request_params.rb', line 3

def uid
  @uid
end

Instance Method Details

#can_auto_auth?Boolean

Returns:

  • (Boolean)

45
46
47
# File 'lib/console_utils/request_utils/request_params.rb', line 45

def can_auto_auth?
  auto_auth? && uid && auth_automator.respond_to?(:call)
end

#headersObject


25
26
27
# File 'lib/console_utils/request_utils/request_params.rb', line 25

def headers
  @headers ||= {}
end

#paramsObject


21
22
23
# File 'lib/console_utils/request_utils/request_params.rb', line 21

def params
  @params ||= {}
end

#to_aObject


29
30
31
# File 'lib/console_utils/request_utils/request_params.rb', line 29

def to_a
  [params, headers.presence].tap(&:compact!)
end

#with_default(default_params = nil) ⇒ Object


33
34
35
36
37
38
39
40
41
42
43
# File 'lib/console_utils/request_utils/request_params.rb', line 33

def with_default(default_params = nil)
  default_headers = default_params.delete(:headers) if default_params.is_a?(Hash)

  if params.is_a?(Hash)
    params.merge!(default_params.to_h)
  else
    headers.merge!(default_headers.to_h)
  end

  to_a
end