class HCardPresenter def self.from_contact(contact) new(contact_properties(contact), contact.country_code) end def self.contact_properties(contact) { 'fn' => contact.recipient, 'street-address' => contact.street_address, 'postal-code' => contact.postal_code, 'locality' => contact.locality, 'region' => contact.region, 'country-name' => country_name(contact) } end def self.country_name(contact) contact.country_name unless contact.country_code == 'GB' end def self.property_keys ['fn', 'street-address', 'postal-code', 'locality', 'region', 'country-name'] end def self.address_formats @address_formats ||= YAML.load_file('config/address_formats.yml') end attr_reader :properties, :country_code def initialize(properties, country_code) @properties = properties @country_code = country_code end def render "
\n#{interpolate_address_template}\n
\n".html_safe end def interpolate_address_property(property_name) value = properties[property_name] if value.present? "#{ERB::Util.html_escape(value)}" else "" end end private def interpolate_address_template address = address_template self.class.property_keys.each do |key| address.gsub!(/\{\{#{key}\}\}/, interpolate_address_property(key)) end address.gsub(/^\n/, '') # get rid of blank lines .strip # get rid of any trailing whitespace .gsub(/\n/, "