]> git.openstreetmap.org Git - rails.git/blob - app/helpers/open_graph_helper.rb
Rename _blocks partial template to _page
[rails.git] / app / helpers / open_graph_helper.rb
1 module OpenGraphHelper
2   require "addressable/uri"
3
4   def opengraph_tags(title, properties)
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" => properties["og:description"] || t("layouts.intro_text")
11     }.merge(
12       opengraph_image_properties(properties)
13     ).merge(
14       properties.slice("article:published_time")
15     )
16
17     safe_join(tags.map do |property, content|
18       tag.meta(:property => property, :content => content)
19     end, "\n")
20   end
21
22   private
23
24   def opengraph_image_properties(properties)
25     begin
26       if properties["og:image"]
27         image_properties = {}
28         image_properties["og:image"] = Addressable::URI.join(root_url, properties["og:image"]).normalize
29         image_properties["og:image:alt"] = properties["og:image:alt"] if properties["og:image:alt"]
30         return image_properties
31       end
32     rescue Addressable::URI::InvalidURIError
33       # return default image
34     end
35     {
36       "og:image" => image_url("osm_logo_256.png"),
37       "og:image:alt" => t("layouts.logo.alt_text")
38     }
39   end
40 end