Sha256: bdf535291d88924a1ddd341eb144acef1e1a4d81d2e150c6e7d2f0d89c279730

Contents?: true

Size: 1.58 KB

Versions: 13

Compression:

Stored size: 1.58 KB

Contents

module Govspeak
  class HCardPresenter
    def self.address_formats
      @address_formats ||= YAML.load_file(
        File.expand_path('config/address_formats.yml', Govspeak.root)
      )
    end

    attr_reader :contact_address

    def initialize(contact_address)
      @contact_address = contact_address
    end

    def render
      "<p class=\"adr\">\n#{interpolate_address_template}\n</p>\n".html_safe
    end

    def interpolate_address_property(our_name, hcard_name)
      value = contact_address[our_name]

      if value.present?
        "<span class=\"#{hcard_name}\">#{ERB::Util.html_escape(value)}</span>"
      else
        ""
      end
    end

  private

    def interpolate_address_template
      address = address_template

      properties = {
        title: "fn",
        street_address: "street-address",
        locality: "locality",
        region: "region",
        postal_code: "postal-code",
        world_location: "country-name",
      }

      properties.each do |our_name, hcard_name|
        address.gsub!(/\{\{#{hcard_name}\}\}/, interpolate_address_property(our_name, hcard_name))
      end

      address.gsub(/^\n/, '')         # get  rid of blank lines
             .strip                   # get rid of any trailing whitespace
             .gsub(/\n/, "<br />\n")  # add break tags where appropriate
    end

    def address_template
      country_code = contact_address[:iso2_country_code].to_s.downcase
      (self.class.address_formats[country_code] || default_format_string).dup
    end

    def default_format_string
      self.class.address_formats['gb']
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
govspeak-6.5.0 lib/govspeak/presenters/h_card_presenter.rb
govspeak-6.4.0 lib/govspeak/presenters/h_card_presenter.rb
govspeak-6.3.0 lib/govspeak/presenters/h_card_presenter.rb
govspeak-6.2.1 lib/govspeak/presenters/h_card_presenter.rb
govspeak-6.2.0 lib/govspeak/presenters/h_card_presenter.rb
govspeak-6.1.1 lib/govspeak/presenters/h_card_presenter.rb
govspeak-6.1.0 lib/govspeak/presenters/h_card_presenter.rb
govspeak-6.0.0 lib/govspeak/presenters/h_card_presenter.rb
govspeak-5.9.1 lib/govspeak/presenters/h_card_presenter.rb
govspeak-5.9.0 lib/govspeak/presenters/h_card_presenter.rb
govspeak-5.8.0 lib/govspeak/presenters/h_card_presenter.rb
govspeak-5.7.1 lib/govspeak/presenters/h_card_presenter.rb
govspeak-5.7.0 lib/govspeak/presenters/h_card_presenter.rb