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
-
.call ⇒ Object
Convenience entry point: instantiates the service and calls it.
Instance Method Summary collapse
-
#attributes_used ⇒ Hash{Symbol => Object}
Snapshot of the attributes the service was initialized with.
-
#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. -
#logger ⇒ Logger, ActiveSupport::Logger
Logger for the service; defaults to Rails.logger unless one was injected via the
:loggerattribute at initialization.
Methods included from Initializer
Class Method Details
.call ⇒ Object
Convenience entry point: instantiates the service and calls it.
All arguments are forwarded to .new and #call.
32 33 34 |
# File 'app/concerns/service.rb', line 32 def call(*) new(*).call end |
Instance Method Details
#attributes_used ⇒ Hash{Symbol => Object}
Snapshot of the attributes the service was initialized with.
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
etagfalls back to an MD5 of the output'scache_key(when the
output responds to it), then to an MD5 ofattributes_used.last_modifiedfalls back to the output'supdated_at, then
created_at(via method or[]access), thenTime.current.
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 |
#logger ⇒ Logger, ActiveSupport::Logger
Logger for the service; defaults to Rails.logger unless one was
injected via the :logger attribute at initialization.
89 90 91 |
# File 'app/concerns/service.rb', line 89 def logger @logger ||= Rails.logger end |