Class: Microformats::Vcard

Inherits:
Object
  • Object
show all
Includes:
FormattingHelpers
Defined in:
lib/vcard.rb

Instance Method Summary collapse

Methods included from FormattingHelpers

#combine_class_names, #concat, #concat_tag, #content_tag, #encode_time, #humanize_time, #merge_html_attrs

Constructor Details

#initialize(template) ⇒ Vcard

You can directly initialize and runthis class, but it’s easier to use the Microformats::Helpers#vcard helper method.



6
7
8
9
# File 'lib/vcard.rb', line 6

def initialize(template)
  @template = template
  @default_tag = :span
end

Instance Method Details

#address(opts = {}, &block) ⇒ Object

Opens a new block for a nested vAddress.

OPTIONS:

  • :type - A string that specifies the type of address(‘home’, ‘work’, etc)

  • :tag - The HTML wrapper element (defaults to :div)

  • Any other passed options will be treated as HTML attributes.

EXAMPLE:

<% card.address :type => 'work', :id => 'my_adr' do |adr| %>
  I live at <%= adr.street "123 Main St" %>.
<% end %>


173
174
175
176
# File 'lib/vcard.rb', line 173

def address(opts = {}, &block)
  adr = Microformats::Address.new(@template)
  adr.run(opts, &block)
end

#company(str, opts = {}) ⇒ Object Also known as: organization

Marks up a company name. If this vCard represents a company rather than an individual person that works at a company, set the :is_company option to true.

OPTIONS:

  • :is_company - Boolean, true if this is a company vCard (defaults to false)

  • :tag - The HTML wrapper element (defaults to :span)

  • Any other passed options will be treated as HTML attributes.



47
48
49
50
# File 'lib/vcard.rb', line 47

def company(str, opts = {})
  classes = opts.delete(:is_company) ? 'fn org' : 'org'
  (str, merge_html_attrs({:class => classes, :itemprop => 'affiliation'}, opts))
end

#coordinates(lat, lng, opts = {}) ⇒ Object

Accepts latitude and longitude as arguments. It will only output a visible text node if you provide the :text option.

OPTIONS

  • :text - String, the text will be be displayed inside the ‘geo’ wrapper



135
136
137
138
139
140
141
142
# File 'lib/vcard.rb', line 135

def coordinates(lat, lng, opts = {})
  lat_meta = ('', :tag => :meta, :itemprop => 'latitude', :content => lat)
  lng_meta = ('', :tag => :meta, :itemprop => 'longitude', :content => lng)
  lat_span = (('', :class => 'value-title', :title => lat), :class => 'latitude')
  lng_span = (('', :class => 'value-title', :title => lng), :class => 'longitude')
  text = opts[:text] || ''
  (lat_meta + lng_meta + lat_span + lng_span + text, :class => 'geo', :itemprop => 'geo', :itemscope => 'itemscope', :itemtype => 'http://data-vocabulary.org/Geo')
end

Outputs a link to h2vx.com that will let the user download the vcard at the passed URL.

OPTIONS

  • :text - The link text (default is “Download vCard”)

  • Any other passed options will be treated as HTML attributes.

EXAMPLE

<%# In Rails, request.request_uri returns the URL of this page %>
<%= card.download_link request.request_uri %>


155
156
157
158
159
# File 'lib/vcard.rb', line 155

def download_link(url, opts = {})
  str = opts.delete(:text) || "Download vCard"
  new_url = "http://h2vx.com/vcf/" + url.gsub("http://", '')
  (str, merge_html_attrs({:tag => :a, :href => new_url, :type => 'text/directory'}, opts))
end

#email(str, opts = {}) ⇒ Object

Marks up an email address, takes the email as a string.

OPTIONS

  • :type - A string that specifies the type of phone number (‘home’, ‘work’, etc)

  • :tag - The HTML wrapper element (defaults to :a)

  • Any other passed options will be treated as HTML attributes.



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/vcard.rb', line 114

def email(str, opts = {})
  opts[:tag] ||= :a
  type = if opts[:type].to_s != ''
    type_inner_span = ('', :class => 'value-title', :title => opts.delete(:type))
    (type_inner_span, :class => 'type')
  else
    ''
  end
  if opts[:tag] == :a
    (type + str, merge_html_attrs({:class => 'email', :href => "mailto:#{str}"}, opts))
  else
    (type + str, merge_html_attrs({:class => 'email'}, opts))
  end
end

#name(str, opts = {}) ⇒ Object

Marks up a person’s name.

OPTIONS:

  • :tag - The HTML wrapper element (defaults to :span)

  • Any other passed options will be treated as HTML attributes.



34
35
36
# File 'lib/vcard.rb', line 34

def name(str, opts = {})
  (str, merge_html_attrs({:class => 'fn', :itemprop => 'name'}, opts))
end

#phone(str, opts = {}) ⇒ Object

Marks up a phone number, takes the phone number as a string.

OPTIONS

  • :type - A string that specifies the type of phone number (‘home’, ‘work’, etc)

  • :tag - The HTML wrapper element (defaults to :span)

  • Any other passed options will be treated as HTML attributes.



97
98
99
100
101
102
103
104
105
# File 'lib/vcard.rb', line 97

def phone(str, opts = {})
  type = if opts[:type].to_s != ''
    type_inner_span = ('', :class => 'value-title', :title => opts.delete(:type))
    (type_inner_span, :class => 'type')
  else
    ''
  end
  (type + str, merge_html_attrs({:class => 'tel'}, opts))
end

#photo(str, opts = {}) ⇒ Object

Marks up the vCard photo as an <img> tag. Takes the image URL as the first argument.

OPTIONS

  • :size - Pass a string with WIDTHxHEIGHT like “200x100” in lieu of the :width and :height options.

  • Any other passed options will be treated as HTML attributes.



83
84
85
86
87
88
# File 'lib/vcard.rb', line 83

def photo(str, opts = {})
  if size = opts.delete(:size)
    opts[:width], opts[:height] = size.split('x')
  end
  (nil, merge_html_attrs({:tag => :img, :class => 'photo', :itemprop => 'photo', :src => str}, opts))
end

#run(opts = {}, &block) ⇒ Object

You can directly initialize and runthis class, but it’s easier to use the Microformats::Helpers#vcard helper method.

OPTIONS:

  • :tag - The HTML wrapper element (defaults to :div)

  • Any other passed options will be treated as HTML attributes.



18
19
20
21
22
23
24
25
26
# File 'lib/vcard.rb', line 18

def run(opts = {}, &block)
  opts[:class] = combine_class_names('vcard', opts[:class])
  opts[:itemscope] = 'itemscope'
  opts[:itemtype] = 'http://data-vocabulary.org/Person'
  opts[:tag] ||= :div
  concat_tag(opts) do
    block.call(self)
  end
end

#url(str, opts = {}) ⇒ Object

Marks up the person’s URL. By default, it will output an <a> tag using the passed in string as both the href and the text. If the :href option is passed, then the string argument is treated as text.

OPTIONS:

  • :href - If passed, the string argument will be treated as the text node.

  • :tag - The HTML wrapper element (defaults to :span)

  • Any other passed options will be treated as HTML attributes.

EXAMPLES:

card.url('http://google.com') #=> <a class='url' href='http://google.com' itemprop='url'>http://google.com</a>
card.url('Google', :href => 'http://google.com') #=> <a class='url' href='http://google.com' itemprop='url'>Google</a>
card.url('http://google.com', :tag => :span) #=> <span class='url' itemprop='url'>http://google.com</span>


67
68
69
70
71
72
73
74
75
# File 'lib/vcard.rb', line 67

def url(str, opts = {})
  if opts[:href]
    (str, merge_html_attrs({:tag => :a, :class => 'url', :itemprop => 'url'}, opts))
  elsif opts[:tag]
    (str, merge_html_attrs({:class => 'url', :itemprop => 'url'}, opts))
  else
    (str, merge_html_attrs({:tag => :a, :class => 'url', :href => str, :itemprop => 'url'}, opts))
  end
end