Module: Api::ReviewsIo::ReviewPayload
- Defined in:
- app/services/api/reviews_io/review_payload.rb
Overview
Interprets raw review hashes from the Reviews.io HTTP API so we can mirror
dashboard visibility (e.g. deleted / unpublished) without removing rows locally.
Constant Summary collapse
- UNPUBLISHED_STATUSES =
Recognised unpublished statuses.
%w[ deleted removed unpublished rejected declined hidden moderated_rejected moderation_rejected ].freeze
Class Method Summary collapse
- .deleted_flags?(data) ⇒ Boolean
- .explicitly_unpublished?(data) ⇒ Boolean
- .falsy_flag?(value) ⇒ Boolean
- .hidden_after_sync?(review_data, existing_record) ⇒ Boolean
- .status_fields_unpublished?(data) ⇒ Boolean
- .truthy_flag?(value) ⇒ Boolean
- .unpublished_code?(raw) ⇒ Boolean
-
.unpublished_on_source?(review_data) ⇒ Boolean
True when Reviews.io indicates the review must not appear on the public storefront.
Class Method Details
.deleted_flags?(data) ⇒ Boolean
37 38 39 40 41 |
# File 'app/services/api/reviews_io/review_payload.rb', line 37 def self.deleted_flags?(data) truthy_flag?(data['deleted']) || truthy_flag?(data['review_deleted']) || truthy_flag?(data['is_deleted']) end |
.explicitly_unpublished?(data) ⇒ Boolean
43 44 45 46 |
# File 'app/services/api/reviews_io/review_payload.rb', line 43 def self.explicitly_unpublished?(data) (data.key?('published') && falsy_flag?(data['published'])) || (data.key?('is_published') && falsy_flag?(data['is_published'])) end |
.falsy_flag?(value) ⇒ Boolean
66 67 68 69 70 |
# File 'app/services/api/reviews_io/review_payload.rb', line 66 def self.falsy_flag?(value) value == false || (value.is_a?(Numeric) && value.zero?) || value.to_s.downcase.in?(%w[0 false no]) end |
.hidden_after_sync?(review_data, existing_record) ⇒ Boolean
20 21 22 |
# File 'app/services/api/reviews_io/review_payload.rb', line 20 def self.hidden_after_sync?(review_data, existing_record) unpublished_on_source?(review_data) || existing_record&.hidden == true end |
.status_fields_unpublished?(data) ⇒ Boolean
48 49 50 51 52 |
# File 'app/services/api/reviews_io/review_payload.rb', line 48 def self.status_fields_unpublished?(data) %w[status review_status moderation_status visibility].any? do |key| unpublished_code?(data[key]) end end |
.truthy_flag?(value) ⇒ Boolean
60 61 62 63 64 |
# File 'app/services/api/reviews_io/review_payload.rb', line 60 def self.truthy_flag?(value) value == true || value == 1 || value.to_s.downcase.in?(%w[1 true yes]) end |
.unpublished_code?(raw) ⇒ Boolean
54 55 56 57 58 |
# File 'app/services/api/reviews_io/review_payload.rb', line 54 def self.unpublished_code?(raw) return false if raw.blank? UNPUBLISHED_STATUSES.include?(raw.to_s.strip.downcase) end |
.unpublished_on_source?(review_data) ⇒ Boolean
True when Reviews.io indicates the review must not appear on the public storefront.
The API shape varies by endpoint; we treat several common keys as "unpublished".
28 29 30 31 32 33 34 35 |
# File 'app/services/api/reviews_io/review_payload.rb', line 28 def self.unpublished_on_source?(review_data) return false if review_data.blank? || !review_data.is_a?(Hash) return true if deleted_flags?(review_data) return true if explicitly_unpublished?(review_data) status_fields_unpublished?(review_data) end |