Class: Govspeak::HCardPresenter

Inherits:
Object
  • Object
show all
Defined in:
lib/govspeak/presenters/h_card_presenter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(properties, country_code) ⇒ HCardPresenter

Returns a new instance of HCardPresenter.



30
31
32
33
# File 'lib/govspeak/presenters/h_card_presenter.rb', line 30

def initialize(properties, country_code)
  @properties = properties
  @country_code = country_code
end

Instance Attribute Details

#country_codeObject (readonly)

Returns the value of attribute country_code.



28
29
30
# File 'lib/govspeak/presenters/h_card_presenter.rb', line 28

def country_code
  @country_code
end

#propertiesObject (readonly)

Returns the value of attribute properties.



28
29
30
# File 'lib/govspeak/presenters/h_card_presenter.rb', line 28

def properties
  @properties
end

Class Method Details

.address_formatsObject



24
25
26
# File 'lib/govspeak/presenters/h_card_presenter.rb', line 24

def self.address_formats
  @address_formats ||= YAML.load_file('config/address_formats.yml')
end

.contact_properties(contact) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/govspeak/presenters/h_card_presenter.rb', line 7

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

.country_name(contact) ⇒ Object



16
17
18
# File 'lib/govspeak/presenters/h_card_presenter.rb', line 16

def self.country_name(contact)
  contact.country_name unless contact.country_code == 'GB'
end

.from_contact(contact) ⇒ Object



3
4
5
# File 'lib/govspeak/presenters/h_card_presenter.rb', line 3

def self.from_contact(contact)
  new(contact_properties(contact), contact.country_code)
end

.property_keysObject



20
21
22
# File 'lib/govspeak/presenters/h_card_presenter.rb', line 20

def self.property_keys
  ['fn', 'street-address', 'postal-code', 'locality', 'region', 'country-name']
end

Instance Method Details

#interpolate_address_property(property_name) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/govspeak/presenters/h_card_presenter.rb', line 39

def interpolate_address_property(property_name)
  value = properties[property_name]

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

#renderObject



35
36
37
# File 'lib/govspeak/presenters/h_card_presenter.rb', line 35

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