Sha256: 4f95f5ecd5984e0d1fb8aef36d94df424e8f6b44e7b8a824e04cdd88d191978f

Contents?: true

Size: 1.84 KB

Versions: 9

Compression:

Stored size: 1.84 KB

Contents

require "active_support/core_ext/array"
require "active_support/core_ext/hash"

module Govspeak
  class ContactPresenter
    attr_reader :contact

    def initialize(contact)
      @contact = ActiveSupport::HashWithIndifferentAccess.new(contact)
    end

    def content_id
      contact[:content_id]
    end

    def title
      contact[:title]
    end

    def description
      contact[:description]
    end

    def post_addresses
      @post_addresses ||= begin
        addresses = contact.dig(:details, :post_addresses) || []
        filter_post_addresses(addresses)
      end
    end

    def email_addresses
      @email_addresses ||= begin
        emails = contact.dig(:details, :email_addresses) || []
        emails.select { |e| e[:email].present? }
      end
    end

    def phone_numbers
      @phone_numbers ||= begin
        phone_numbers = contact.dig(:details, :phone_numbers) || []
        phone_numbers.select { |p| p[:number].present? }
      end
    end

    def contact_form_links
      @contact_form_links ||= begin
        contact_form_links = contact.dig(:details, :contact_form_links) || []
        contact_form_links.select { |c| c[:link].present? }
      end
    end

  private

    def filter_post_addresses(addresses)
      addresses.each do |address|
        # A lot of the postal addresses published to the Publishing API have
        # a populated array of postal addresses but each field an empty string :-(
        address.delete_if do |key, value|
          # Not showing United Kingdom country is a "feature" lifted and shifted
          # from Whitehall:
          # https://github.com/alphagov/whitehall/blob/c67d53d80f9856549c2da1941a10dbb9170be494/lib/address_formatter/formatter.rb#L17
          key == "world_location" && value.strip == "United Kingdom" || value == ""
        end
      end

      addresses.reject(&:empty?)
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
govspeak-8.7.0 lib/govspeak/presenters/contact_presenter.rb
govspeak-8.6.1 lib/govspeak/presenters/contact_presenter.rb
govspeak-8.6.0 lib/govspeak/presenters/contact_presenter.rb
govspeak-8.5.1 lib/govspeak/presenters/contact_presenter.rb
govspeak-8.5.0 lib/govspeak/presenters/contact_presenter.rb
govspeak-8.4.1 lib/govspeak/presenters/contact_presenter.rb
govspeak-8.4.0 lib/govspeak/presenters/contact_presenter.rb
govspeak-8.3.4 lib/govspeak/presenters/contact_presenter.rb
govspeak-8.3.3 lib/govspeak/presenters/contact_presenter.rb