]> git.openstreetmap.org Git - rails.git/blob - app/helpers/open_graph_helper.rb
Merge remote-tracking branch 'upstream/pull/4944'
[rails.git] / app / helpers / open_graph_helper.rb
1 module OpenGraphHelper
2   require "addressable/uri"
3
4   def opengraph_tags(title = nil, og_image = nil)
5     tags = {
6       "og:site_name" => t("layouts.project_name.title"),
7       "og:title" => title || t("layouts.project_name.title"),
8       "og:type" => "website",
9       "og:image" => og_image_url(og_image),
10       "og:url" => url_for(:only_path => false),
11       "og:description" => t("layouts.intro_text")
12     }
13
14     safe_join(tags.map do |property, content|
15       tag.meta(:property => property, :content => content)
16     end, "\n")
17   end
18
19   private
20
21   def og_image_url(og_image)
22     begin
23       return Addressable::URI.join(root_url, og_image).normalize if og_image
24     rescue Addressable::URI::InvalidURIError
25       # return default image
26     end
27     image_url("osm_logo_256.png")
28   end
29 end