Class: Eco::API::Session::Batch::Feedback

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

Instance Attribute Summary collapse

Job shortcut methods collapse

Pure feedback methods collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(job:) ⇒ Feedback

Returns a new instance of Feedback.

Parameters:



35
36
37
38
# File 'lib/eco/api/session/batch/feedback.rb', line 35

def initialize(job:)
  raise "A Eco::API::Session::Batch::Job object is required. Given: #{job}" unless job.is_a?(Eco::API::Session::Batch::Job)
  @job  = job
end

Instance Attribute Details

#jobEco::API::Session::Batch::Job (readonly)

batch job the feedback is associated with

Returns:



6
7
8
# File 'lib/eco/api/session/batch/feedback.rb', line 6

def job
  @job
end

Class Method Details

.get_attr(entry, attr) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/eco/api/session/batch/feedback.rb', line 14

def get_attr(entry, attr)
  if entry.respond_to?(attr.to_sym)
    entry.public_send(attr.to_sym)
  elsif entry.is_a?(Hash)
    entry["#{attr}"]
  end
end

.get_row(value) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/eco/api/session/batch/feedback.rb', line 22

def get_row(value)
  case value
  when Eco::API::Common::People::PersonEntry
    value.idx
  when Ecoportal::API::V1::Person
    get_row(value.entry)
  end
end

.person_ref(entry) ⇒ Object



9
10
11
12
# File 'lib/eco/api/session/batch/feedback.rb', line 9

def person_ref(entry)
  row_str = (row = get_row(entry)) ? "(row: #{row}) " : nil
  "#{row_str}(id: '#{get_attr(entry, :id)}') '#{get_attr(entry, :name)}' ('#{get_attr(entry, :external_id)}': '#{get_attr(entry, :email)}')"
end

Instance Method Details

#as_update(entry, add_feedback: true) ⇒ Object

Note:

for better feedback

Slightly modifies the behaviour of Ecoportal::API::Common::BaseModel#as_update, so schema details fields show the alt_id It also fixes possible patch updates that are incomplete or unnecessary.

Parameters:

  • entry (Hash, Ecoportal::API::V1::Person, Ecoportal::API::Internal::Person)
  • add_feedback (Boolean) (defaults to: true)

    if true it tweak the hash update with additional data.



76
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
# File 'lib/eco/api/session/batch/feedback.rb', line 76

def as_update(entry, add_feedback: true)
  case
  when entry.is_a?(Hash)
    hash = entry
  else #entry.is_a?(Ecoportal::API::V1::Person)
    if only_ids?
      hash = entry.as_json.slice("id", "external_id", "email")
    else
      hash = entry.as_update

      if add_feedback && details = hash["details"]
        if hfields = details["fields"]
          hfields.each do |fld|
            fld.merge!("alt_id" => entry.details.get_field(fld["id"]).alt_id)
          end
        end
      end

      if  = hash["account"]
        if .keys == ["send_invites"] && !["send_invites"]
          hash.delete("account")
          hash.delete("id") if hash.keys == ["id"]
        end
      end
    end
  end
  hash || {}
end

#generate(requests = nil, max_chars: 800, only_stats: false) ⇒ String

Note:

if requests is not provided, it uses the last requests of the parent Batch::Job job

Generates the lines of feedback of the current requests

Parameters:

  • requests (Enumerable<Hash>) (defaults to: nil)

    raw requests as they would be sent to the Server

  • max_chars (Integer) (defaults to: 800)

    the max number of characters for the current feedback message

  • only_stats (Boolean) (defaults to: false)

    whether or not should only include a brief summary of stats

Returns:

  • (String)

    the feedback message



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/eco/api/session/batch/feedback.rb', line 120

def generate(requests = nil, max_chars: 800, only_stats: false)
  requests ||= job.requests
  [].tap do |msg|
    if !requests || !requests.is_a?(Enumerable) || requests.empty?
      msg << "#{"*" * 10} Nothing for #{signature} so far :)  #{"*" * 10}"
    else
      header = "#{"*"*10}  #{signature} - Feedback Sample #{"*"*10}"
      msg << header unless only_stats
      unless only_stats
        sample_length = 1
        sample = requests.slice(0, 20).map do |request|
          max_chars     -= request.pretty_inspect.length
          sample_length += 1 if max_chars > 0
          request
        end
        msg << "#{sample.slice(0, sample_length).pretty_inspect}"
      end
      stats_str = "#{"+"*3}  #{type.to_s.upcase} length: #{requests.length}  #{"+"*3}  STATS  (job '#{name}')  #{"+"*3}"
      msg << "#{"-"*stats_str.length}" if only_stats
      msg << stats_str
      msg << "#{request_stats(requests).message}"
      msg << "#{"-"*stats_str.length}" if only_stats
      msg << "*" * header.length unless only_stats
    end
  end.join("\n")
end

#job_requestsObject

See Also:



64
65
66
# File 'lib/eco/api/session/batch/feedback.rb', line 64

def job_requests
  job.requests
end

#nameString

Returns name of the batch job.

Returns:

  • (String)

    name of the batch job

See Also:



44
45
46
# File 'lib/eco/api/session/batch/feedback.rb', line 44

def name
  job.name
end

#optionsObject

See Also:



59
60
61
# File 'lib/eco/api/session/batch/feedback.rb', line 59

def options
  job.options
end

#request_stats(requests = nil) ⇒ Eco::API::Session::Batch::RequestStats

Note:

if requests is not provided, it uses the last requests of the parent Batch::Job job

Returns the stats object of the current requests.

Parameters:

  • requests (Enumerable<Hash>) (defaults to: nil)

    raw requests as they would be sent to the Server

Returns:



108
109
110
111
112
# File 'lib/eco/api/session/batch/feedback.rb', line 108

def request_stats(requests = nil)
  requests ||= job.requests
  return @request_stats if @request_stats && requests == job.requests
  @request_stats ||= Eco::API::Session::Batch::RequestStats.new(type: type, requests: requests)
end

#setsObject

See Also:



54
55
56
# File 'lib/eco/api/session/batch/feedback.rb', line 54

def sets
  job.sets
end

#typeObject

See Also:



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

def type
  job.type
end