Created
June 6, 2018 10:50
-
-
Save svpersteve/8ef2613ec4e501bd8ba9630a7559fe62 to your computer and use it in GitHub Desktop.
Link meta presenter
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 LinkMetaPresenter | |
def initialize(article) # Define article as the object passed to LinkMetaPresenter(@article) in our article_controller so we can access it here | |
@article = article | |
end | |
attr_reader :article # Provide read access to the 'article' variable within this file | |
delegate :twitter_card, to: :article # Define 'twitter_card' as 'article.twitter_card' | |
def card_type | |
twitter_card&.card || 'summary' # Use the card type on the twitter card (if twitter_card exists - safe navigator returns nil if it doesn't), otherwise use 'summary' | |
end | |
def title | |
twitter_card&.title || article.html_title || article.title || I18n.t('happy_bear_software.title') # Use twitter card title, or article html title, or article title, or last resort, the site title | |
end | |
def description | |
twitter_card&.description || article.description || I18n.t('happy_bear_software.description') | |
end | |
def image_url | |
twitter_card&.image_url || 'https://images.ctfassets.net/jdiiwrjwwj2y/47bceGNtJuAAOYQc6gMI2e/2437282b0d09fdacb515e222d7db21f5/Developers-guide-human-networking-Twitter.png' | |
end | |
def creator | |
twitter_card&.creator || authors_twitter_handle || '@happybearsoft' # Wrap some messy code in authors_twitter_handle, a private method, so it's easier to read | |
end | |
def site | |
twitter_card&.site || '@happybearsoft' | |
end | |
def og_type | |
'article' # In whatever case, og_type will always be article | |
end | |
def domain | |
'happybearsoftware.com' | |
end | |
private | |
def authors_twitter_handle | |
'@' + article.author.twitter_url.gsub('https://twitter.com/', '') if article.author&.twitter_url.present? | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment