class Eco::API::Session::Batch::Feedback

@attr_reader job [Eco::API::Session::Batch::Job] `batch job` the feedback is associated with

Attributes

job[R]

Public Class Methods

new(job:) click to toggle source

@param job [Eco::API::Session::Batch::Job] `batch job` the feedback is associated with

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

Public Instance Methods

as_update(entry, add_feedback: true) click to toggle source

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. @note for better feedback @param entry [Hash, Ecoportal::API::V1::Person, Ecoportal::API::Internal::Person] @param add_feedback [Boolean] if `true` it tweak the hash update with additional data.

# File lib/eco/api/session/batch/feedback.rb, line 52
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 account = hash["account"]
        if account.keys == ["send_invites"] && !account["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) click to toggle source

Generates the lines of feedback of the current requests @note if `requests` is not provided, it uses the last requests of the parent `Batch::Job` `job` @param requests [Enumerable<Hash>] raw requests as they would be sent to the Server @param max_chars [Integer] the max number of characters for the current feedback message @param only_stats [Boolean] whether or not should only include a brief summary of stats @return [String] the feedback message

# File lib/eco/api/session/batch/feedback.rb, line 96
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_requests() click to toggle source

@see Eco::API::Session::Batch::Job#requests

# File lib/eco/api/session/batch/feedback.rb, line 40
def job_requests
  job.requests
end
name() click to toggle source

@see Eco::API::Session::Batch::Job#name @return [String] name of the `batch job`

# File lib/eco/api/session/batch/feedback.rb, line 20
def name
  job.name
end
options() click to toggle source

@see Eco::API::Session::Batch::Job#options

# File lib/eco/api/session/batch/feedback.rb, line 35
def options
  job.options
end
request_stats(requests = nil) click to toggle source

@note if `requests` is not provided, it uses the last requests of the parent `Batch::Job` `job` @param requests [Enumerable<Hash>] raw requests as they would be sent to the Server @return [Eco::API::Session::Batch::RequestStats] the stats object of the current requests

# File lib/eco/api/session/batch/feedback.rb, line 84
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
sets() click to toggle source

@see Eco::API::Session::Batch::Job#sets

# File lib/eco/api/session/batch/feedback.rb, line 30
def sets
  job.sets
end
type() click to toggle source

@see Eco::API::Session::Batch::Job#type

# File lib/eco/api/session/batch/feedback.rb, line 25
def type
  job.type
end

Private Instance Methods

only_ids?() click to toggle source

@!endgroup

# File lib/eco/api/session/batch/feedback.rb, line 126
def only_ids?
  [:delete, :get].include?(type)
end
sets_title() click to toggle source
# File lib/eco/api/session/batch/feedback.rb, line 134
def sets_title
  "#{sets.map {|s| s.to_s}.join(", ")}"
end
signature() click to toggle source
# File lib/eco/api/session/batch/feedback.rb, line 130
def signature
  "Batch job \"#{name}\" ['#{type.to_s.upcase}': #{sets_title}]"
end