]> git.openstreetmap.org Git - rails.git/blob - app/helpers/open_graph_helper.rb
Set og:image:alt OpenGraph property
[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, og_image_alt = 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:url" => url_for(:only_path => false),
10       "og:description" => t("layouts.intro_text")
11     }.merge(
12       opengraph_image_properties(og_image, og_image_alt)
13     )
14
15     safe_join(tags.map do |property, content|
16       tag.meta(:property => property, :content => content)
17     end, "\n")
18   end
19
20   private
21
22   def opengraph_image_properties(og_image, og_image_alt)
23     begin
24       if og_image
25         properties = {}
26         properties["og:image"] = Addressable::URI.join(root_url, og_image).normalize
27         properties["og:image:alt"] = og_image_alt if og_image_alt
28         return properties
29       end
30     rescue Addressable::URI::InvalidURIError
31       # return default image
32     end
33     {
34       "og:image" => image_url("osm_logo_256.png"),
35       "og:image:alt" => t("layouts.logo.alt_text")
36     }
37   end
38 end