Last active
August 29, 2015 14:05
-
-
Save KamilLelonek/c180151ceeed536c3912 to your computer and use it in GitHub Desktop.
Facebook OpenGraph metadata generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(env) -> | |
if env.ENV is 'production' | |
{ | |
API_ENDPOINT: '' | |
FACEBOOK_ID: '' | |
FACEBOOK_NAMESPACE: '' | |
FACEBOOK_OBJECT: '' | |
} | |
else | |
{ | |
API_ENDPOINT: '' | |
FACEBOOK_ID: '' | |
FACEBOOK_NAMESPACE: '' | |
FACEBOOK_OBJECT: '' | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@buildGiftMetadata() | |
buildGiftMetadata: => | |
@_metadataGenerator().injectHeadPrefix() | |
@_metadataGenerator().generateMetaElements('title', 'http://placehold.it/100x100') | |
_metadataGenerator: => | |
@metadataGenerator ||= new (require 'metadata_generator')() | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MetadataGenerator | |
constructor: -> | |
config = require 'config' | |
@facebookAppId = config.FACEBOOK_ID | |
@facebookNamespace = config.FACEBOOK_NAMESPACE | |
facebookObject = config.FACEBOOK_OBJECT | |
injectHeadPrefix: => | |
@_head().setAttribute 'prefix', @_prefix() | |
_prefix: => | |
"og: http://ogp.me/ns# fb: http://ogp.me/ns/fb# #{@facebookNamespace}: http://ogp.me/ns/fb/#{@facebookNamespace}#" | |
generateMetaElements: (title, image) => | |
facebookAppIdElement = @_buildMetaTag 'fb:app_id', @facebookAppId | |
openGraphType = @_buildMetaTag 'og:type', "#{@facebookNamespace}:#{@facebookObject}" | |
openGraphUrl = @_buildMetaTag 'og:url', document.URL | |
openGraphTitle = @_buildMetaTag 'og:title', title | |
openGraphImage = @_buildMetaTag 'og:image', image | |
@_addMetaTags facebookAppIdElement, openGraphType, openGraphUrl, openGraphTitle, openGraphImage | |
_buildMetaTag: (property, content) => | |
document.createElement('meta').tap (metaTag) -> | |
metaTag.setAttribute 'property', property | |
metaTag.setAttribute 'content', content | |
_addMetaTags: (elements...) => | |
fragment = document.createDocumentFragment() | |
elements.forEach (element) -> | |
fragment.appendChild element | |
@_appendToHead fragment | |
_appendToHead: (element) => | |
@_head().appendChild element | |
_head: => | |
@headElement ||= document.querySelector 'head' | |
module.exports = MetadataGenerator |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
brunch.io + jsenv