Module: Service

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::API, ActiveModel::Attributes, Memery, Initializer
Included in:
Amazon::RecordSalesRanks, Amazon::SalesRankBackfill, Amazon::SalesRankMetrics, ArticleTechnical::PromoteAttachment, Assistant::MicrosoftAdsChangeApproval, Assistant::MicrosoftAdsChangePayload, Assistant::TechnicalSupport::CaseCandidateSelector, Assistant::TechnicalSupport::CaseReplay, Assistant::TechnicalSupport::CaseReplayBatch, Assistant::TechnicalSupport::CaseReplayPersistence, Assistant::TechnicalSupport::ResponseGroundingAnswerSegments, Assistant::TechnicalSupport::ResponseGroundingAssessmentBuilder, Assistant::TechnicalSupport::ResponseGroundingAssessmentLease, Assistant::TechnicalSupport::ResponseGroundingAssessmentPayload, Assistant::TechnicalSupport::ResponseGroundingClaimCitations, Assistant::TechnicalSupport::ResponseGroundingDecision, Assistant::TechnicalSupport::ResponseGroundingEvidence, Assistant::TechnicalSupport::ResponseGroundingFailureRecorder, Assistant::TechnicalSupport::ResponseGroundingGate, Assistant::TechnicalSupport::ResponseGroundingPreparation, Assistant::TechnicalSupport::ResponseGroundingToolResult, Bom::XmlFromZone, Edi::Wayfair::CatalogReadV2Persister, Edi::Wayfair::CatalogReadV2Request, Edi::Wayfair::RecordCatalogParentCoverage, EmployeePhoneStatus::BroadcastSwitchboard, EmployeePhoneStatus::QueueCoverageImpact, EmployeePhoneStatus::QueueStatusPuller, Faq::RefreshEmbeddingPosts, Faq::Update, HeatingSystemCalculator::BaseCalculator, ListingIssues::BuildWayfairDuplicateParentIssue, ListingIssues::BuildWayfairInsightIssues, ListingIssues::BuildWayfairMissingRequiredSpecIssue, NewLead::ActivityAgendaReconciler, NewLead::LeadActivityReassigner, NewLead::ProtectLastOpenLeadActivity, OpportunityBriefing::ActivitySnapshot, OpportunityBriefing::BroadcastUpdate, OpportunityBriefing::CallSnapshot, OpportunityBriefing::CommunicationSnapshot, OpportunityBriefing::ContextBuilder, OpportunityBriefing::ConversationSharing, OpportunityBriefing::EngagementSnapshot, OpportunityBriefing::Generate, OpportunityBriefing::OpportunitySnapshot, OpportunityBriefing::OrderSnapshot, OpportunityBriefing::OutletPurchaseSnapshot, OpportunityBriefing::ParticipantSnapshot, OpportunityBriefing::QuoteCollectionSnapshot, OpportunityBriefing::QuoteLineItemSnapshot, OpportunityBriefing::QuoteLineSummary, OpportunityBriefing::QuoteParentChange, OpportunityBriefing::QuoteRecordSnapshot, OpportunityBriefing::QuoteRevisionChange, OpportunityBriefing::QuoteRevisionSnapshot, OpportunityBriefing::QuoteSnapshot, OpportunityBriefing::Request, RoomConfiguration::CalculateQuote, Seo::RecommendationCompletionVerifier, Shipping::RegisterRmaTracking, SiteSearch::PublicationsAdapter, SiteSearch::Query, SiteSearch::SwiftypeAdapter, Warranty::OrderLookup, Warranty::PublicRegistration, Warranty::RegistrantResolver, Warranty::ReviewPrompt
Defined in:
app/concerns/service.rb

Overview

ActiveSupport::Concern mixin: service.

Base concern for service objects. Provides a .call convenience entry
point, memoization (via Memery), a plucked :logger attribute, and a
standardized Result value object with HTTP-cache metadata
(etag / last_modified).

Defined Under Namespace

Modules: Initializer Classes: Result

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Initializer

#initialize

Class Method Details

.callObject

Convenience entry point: instantiates the service and calls it.
All arguments are forwarded to .new and #call.

Returns:

  • (Object)

    whatever the instance's #call returns



32
33
34
# File 'app/concerns/service.rb', line 32

def call(*)
  new(*).call
end

Instance Method Details

#attributes_usedHash{Symbol => Object}

Snapshot of the attributes the service was initialized with.

Returns:

  • (Hash{Symbol => Object})

    attributes with symbol keys, minus :logger



96
97
98
# File 'app/concerns/service.rb', line 96

def attributes_used
  attributes.transform_keys(&:to_sym).except(:logger)
end

#build_result(output, last_modified: nil, etag: nil, cache_key: nil) ⇒ Service::Result

Builds a Result for the given output, deriving HTTP-cache
metadata (etag, last_modified) when not supplied.

Logic Details

  • etag falls back to an MD5 of the output's cache_key (when the
    output responds to it), then to an MD5 of attributes_used.
  • last_modified falls back to the output's updated_at, then
    created_at (via method or [] access), then Time.current.

Parameters:

  • output (Object)

    the service output payload

  • last_modified (Time, DateTime, nil) (defaults to: nil)

    explicit last-modified timestamp

  • etag (String, nil) (defaults to: nil)

    explicit ETag; derived when nil

  • cache_key (String, nil) (defaults to: nil)

    explicit cache key used for ETag derivation

Returns:



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'app/concerns/service.rb', line 114

def build_result(output, last_modified: nil, etag: nil, cache_key: nil)
  if etag.nil?
    cache_key ||= output.cache_key if output.respond_to?(:cache_key)
    etag = Digest::MD5.hexdigest(cache_key) if cache_key
    etag ||= Digest::MD5.hexdigest(attributes_used.to_s)
  end
  if last_modified.nil?
    last_modified = output.try(:updated_at)
    last_modified ||= begin
      output.try(:[], :updated_at)
    rescue StandardError
      nil
    end
    last_modified = output.try(:created_at)
    last_modified ||= begin
      output.try(:[], :created_at)
    rescue StandardError
      nil
    end
    last_modified ||= Time.current
  end
  Result.new(output:, params: attributes_used, last_modified:, etag:)
end

#loggerLogger, ActiveSupport::Logger

Logger for the service; defaults to Rails.logger unless one was
injected via the :logger attribute at initialization.

Returns:

  • (Logger, ActiveSupport::Logger)

    the logger instance



89
90
91
# File 'app/concerns/service.rb', line 89

def logger
  @logger ||= Rails.logger
end