Class: Eco::API::Session::Batch::RequestStats

Inherits:
Object
  • Object
show all
Defined in:
lib/eco/api/session/batch/request_stats.rb

Constant Summary collapse

CORE_ATTRS =
Eco::API::Common::People::PersonParser::CORE_ATTRS
ACCOUNT_ATTRS =
(Eco::API::Common::People::PersonParser::ACCOUNT_ATTRS + ["permissions_custom"]).uniq
DETAILS_ATTRS =
["fields"]
BLANKED_PREFIX =
"blanked_"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, requests: []) ⇒ RequestStats

Returns a new instance of RequestStats.



65
66
67
68
69
70
# File 'lib/eco/api/session/batch/request_stats.rb', line 65

def initialize(type:, requests: [])
  raise "type should be one of #{Eco::API::Session::Batch::Job.types}. Given: #{type}"  unless self.class.valid_type?(type.to_sym)
  @type  = type.to_sym
  @count = requests && requests.length
  @stats = build(requests)
end

Instance Attribute Details

#countInteger (readonly)

the total number of requests

Returns:

  • (Integer)

    the current value of count



7
8
9
# File 'lib/eco/api/session/batch/request_stats.rb', line 7

def count
  @count
end

#statsHash (readonly)

plain Hash with the number of requests that include an attribute

Returns:

  • (Hash)

    the current value of stats



7
8
9
# File 'lib/eco/api/session/batch/request_stats.rb', line 7

def stats
  @stats
end

#typeObject (readonly)

Returns the value of attribute type.



63
64
65
# File 'lib/eco/api/session/batch/request_stats.rb', line 63

def type
  @type
end

Class Method Details

.account_attrs(stats: false, all: false) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/eco/api/session/batch/request_stats.rb', line 28

def (stats: false, all: false)
  ACCOUNT_ATTRS.dup.tap do |attrs|
    if stats || all
      attrs.unshift("account_remove")
      attrs.unshift("account") if all
      attrs.concat(blank_attrs(ACCOUNT_ATTRS))
    end
  end
end

.blank_attrs(attrs) ⇒ Object



57
58
59
# File 'lib/eco/api/session/batch/request_stats.rb', line 57

def blank_attrs(attrs)
  attrs.map {|attr| "#{blanked_prefix}#{attr}"}
end

.blanked_prefix(attr = nil) ⇒ Object



47
48
49
50
51
# File 'lib/eco/api/session/batch/request_stats.rb', line 47

def blanked_prefix(attr = nil)
  @blanked_prefix ||= BLANKED_PREFIX
  return @blanked_prefix unless attr
  "#{blanked_prefix}#{attr}"
end

.blanked_prefix=(value) ⇒ Object



53
54
55
# File 'lib/eco/api/session/batch/request_stats.rb', line 53

def blanked_prefix=(value)
  @blanked_prefix = value || blanked_prefix
end

.core_attrs(stats: false, all: false) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/eco/api/session/batch/request_stats.rb', line 19

def core_attrs(stats: false, all: false)
  CORE_ATTRS.dup.tap do |attrs|
    if stats || all
      attrs.unshift("core")
      attrs.concat(blank_attrs(CORE_ATTRS))
    end
  end
end

.details_attrs(stats: false, all: false) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/eco/api/session/batch/request_stats.rb', line 38

def details_attrs(stats: false, all: false)
  DETAILS_ATTRS.dup.tap do |attrs|
    if stats || all
      attrs.unshift("details_remove")
      attrs.unshift("details") if all
    end
  end
end

.valid_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/eco/api/session/batch/request_stats.rb', line 15

def valid_type?(type)
  Eco::API::Session::Batch::Job.valid_type?(type.to_sym)
end

Instance Method Details

#message(percent: false) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/eco/api/session/batch/request_stats.rb', line 76

def message(percent: false)
  key_val_delimiter = ": "; attr_delimiter = " ++ "
  pairs_to_line = Proc.new do |pairs|
    pairs.map do |p|
      [p.first.to_s, "#{p.last.to_s}" + (percent ? "%" : "")].join(key_val_delimiter)
    end.join(attr_delimiter)
  end

  lines = []
  lines << pairs_to_line.call(core_pairs(percent: percent))
  lines << pairs_to_line.call((percent: percent))
  lines << pairs_to_line.call(details_pairs(percent: percent))
  lines.join("\n")
end

#to_hObject



72
73
74
# File 'lib/eco/api/session/batch/request_stats.rb', line 72

def to_h
  @stats
end