Class: Blog::Blogpost

Inherits:
Object
  • Object
show all
Defined in:
lib/nexmo_developer/app/models/blog/blogpost.rb

Constant Summary collapse

CLOUDFRONT_BLOG_URL =
'https://d226lax1qjow5r.cloudfront.net/blog/'.freeze
DEFAULT_VONAGE_LOGO_URL =
'https://s3.eu-west-1.amazonaws.com/developer.vonage.com/vonage-logo-images/vonage_logo_primary_white_1200x600.png'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Blogpost

Returns a new instance of Blogpost.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/nexmo_developer/app/models/blog/blogpost.rb', line 9

def initialize(attributes)
  @title        = attributes['title']
  @description  = attributes['description']
  @thumbnail    = attributes['thumbnail']    || ''
  @published    = attributes['published']    || false
  @published_at = attributes['published_at']
  @updated_at   = attributes['updated_at']
  @canonical    = attributes['canonical']
  @tags         = attributes['tags']         || []
  @link         = attributes['link']         || ''
  @filename     = attributes['filename']
  @locale       = attributes['locale']       || 'en'
  @outdated     = attributes['outdated']     || false
  @spotlight    = attributes['spotlight']    || false

  @author       = Blog::Author.new(attributes['author'] || {})  # TODO: DEFAULT AUTHOR
  @category     = Blog::Category.new(attributes['category'])

  @content        = ''
  @header_img_url = build_bucket_img_url_from_thumbnail

  @replacement_url  = attributes['replacement_url']
end

Instance Attribute Details

#authorObject

Returns the value of attribute author.



2
3
4
# File 'lib/nexmo_developer/app/models/blog/blogpost.rb', line 2

def author
  @author
end

#canonicalObject

Returns the value of attribute canonical.



2
3
4
# File 'lib/nexmo_developer/app/models/blog/blogpost.rb', line 2

def canonical
  @canonical
end

#categoryObject

Returns the value of attribute category.



2
3
4
# File 'lib/nexmo_developer/app/models/blog/blogpost.rb', line 2

def category
  @category
end

#contentObject

Returns the value of attribute content.



2
3
4
# File 'lib/nexmo_developer/app/models/blog/blogpost.rb', line 2

def content
  @content
end

#descriptionObject

Returns the value of attribute description.



2
3
4
# File 'lib/nexmo_developer/app/models/blog/blogpost.rb', line 2

def description
  @description
end

#filenameObject

Returns the value of attribute filename.



2
3
4
# File 'lib/nexmo_developer/app/models/blog/blogpost.rb', line 2

def filename
  @filename
end

#header_img_urlObject

Returns the value of attribute header_img_url.



2
3
4
# File 'lib/nexmo_developer/app/models/blog/blogpost.rb', line 2

def header_img_url
  @header_img_url
end

Returns the value of attribute link.



2
3
4
# File 'lib/nexmo_developer/app/models/blog/blogpost.rb', line 2

def link
  @link
end

#localeObject

Returns the value of attribute locale.



2
3
4
# File 'lib/nexmo_developer/app/models/blog/blogpost.rb', line 2

def locale
  @locale
end

#publishedObject

Returns the value of attribute published.



2
3
4
# File 'lib/nexmo_developer/app/models/blog/blogpost.rb', line 2

def published
  @published
end

#published_atObject

Returns the value of attribute published_at.



2
3
4
# File 'lib/nexmo_developer/app/models/blog/blogpost.rb', line 2

def published_at
  @published_at
end

#slugObject

Returns the value of attribute slug.



2
3
4
# File 'lib/nexmo_developer/app/models/blog/blogpost.rb', line 2

def slug
  @slug
end

#spotlightObject

Returns the value of attribute spotlight.



2
3
4
# File 'lib/nexmo_developer/app/models/blog/blogpost.rb', line 2

def spotlight
  @spotlight
end

#tagsObject

Returns the value of attribute tags.



2
3
4
# File 'lib/nexmo_developer/app/models/blog/blogpost.rb', line 2

def tags
  @tags
end

#thumbnailObject

Returns the value of attribute thumbnail.



2
3
4
# File 'lib/nexmo_developer/app/models/blog/blogpost.rb', line 2

def thumbnail
  @thumbnail
end

#titleObject

Returns the value of attribute title.



2
3
4
# File 'lib/nexmo_developer/app/models/blog/blogpost.rb', line 2

def title
  @title
end

#updated_atObject

Returns the value of attribute updated_at.



2
3
4
# File 'lib/nexmo_developer/app/models/blog/blogpost.rb', line 2

def updated_at
  @updated_at
end

Class Method Details



100
101
102
# File 'lib/nexmo_developer/app/models/blog/blogpost.rb', line 100

def self.banner_text
  "<h3 class='Vlt-title--icon'>Vonage API Account</h3><p>To complete this tutorial, you will need a <a href='https://dashboard.nexmo.com/sign-up' target='_blank'>Vonage API account</a>. If you don’t have one already, you can sign up today and start building with free credit. Once you have an account, you can find your API Key and API Secret at the top of the <a href='https://dashboard.nexmo.com/sign-up' target='_blank'>Vonage API Dashboard</a>.</p>"
end

.build_blogpost_from_path(path, locale) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/nexmo_developer/app/models/blog/blogpost.rb', line 33

def self.build_blogpost_from_path(path, locale)
  return if path.blank?

  path = "#{Rails.configuration.blog_path}/blogposts/#{locale}/#{path}.md"

  default_not_found_page(path) unless File.exist?(path)

  document = clean_document_from_netlify(path)

  blogpost = new(BlogpostParser.build_show_with_locale(path, locale))
  blogpost.content = Nexmo::Markdown::Renderer.new({}).call(document)

  blogpost
end

.build_image_tagObject



96
97
98
# File 'lib/nexmo_developer/app/models/blog/blogpost.rb', line 96

def self.build_image_tag
  "<p></p><figure><img src='#{s3_banner_image}' alt='Screenshot of new Meetings API session in progress'><figcaption class='Vlt-center'><em>Start developing in minutes with free credits on us. No credit card required!</em></figcaption></figure><p></p>"
end

.clean_document_from_netlify(path) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/nexmo_developer/app/models/blog/blogpost.rb', line 72

def self.clean_document_from_netlify(path)
  # Netlify - Imgage urls to S3 Bucket
  document = File.read(path).gsub('/content/blog/') { |match| "#{Blog::Blogpost::CLOUDFRONT_BLOG_URL}blogposts/#{match.gsub('/content/blog/', '')}" }

  # Netlify - SIGN UP Banner
  document = document.gsub(%r{<sign-up></sign-up>}, "#{banner_text}#{build_image_tag}")

  # Netlify - SIGN UP Banner + Number
  document = document.gsub(%r{<sign-up number></sign-up>}) do |_match|
    "#{banner_text}<p>This tutorial also uses a virtual phone number. To purchase one, go to <em>Numbers > Buy Numbers</em> and search for one that meets your needs.</p>#{build_image_tag}"
  end

  # Netlify - Embedded YOUTUBE Video
  document.gsub(%r{<youtube id="(\w+)"></youtube>}) do |match|
    youtube_id = match[/(?<=").*(?=")/]
    "<center class='video'><br><iframe width='448' height='252' src='https://www.youtube-nocookie.com/embed/#{youtube_id}' frameborder='0' allow='accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture' allowfullscreen></iframe><br><br></center>"
  end
end

.default_not_found_page(path) ⇒ Object



68
69
70
# File 'lib/nexmo_developer/app/models/blog/blogpost.rb', line 68

def self.default_not_found_page(path)
  "<h1>No such blog</h1><p>#{path}</p>"
end

.s3_banner_imageObject

NETLIFY Helpers



92
93
94
# File 'lib/nexmo_developer/app/models/blog/blogpost.rb', line 92

def self.s3_banner_image
  'https://s3.eu-west-1.amazonaws.com/developer.vonage.com/blog/signup_banner/sign_up_banner.png'
end

Instance Method Details

#build_bucket_img_url_from_thumbnailObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/nexmo_developer/app/models/blog/blogpost.rb', line 48

def build_bucket_img_url_from_thumbnail
  require 'net/http'
  require 'addressable'

  return DEFAULT_VONAGE_LOGO_URL if @thumbnail.empty?

  @thumbnail = @thumbnail.gsub('/content/blog/') do |match| # gsub Netlify img urls
    "#{Blog::Blogpost::CLOUDFRONT_BLOG_URL}blogposts/#{match.gsub('/content/blog/', '')}"
  end

  url = Addressable::URI.parse(@thumbnail)
  Net::HTTP.start(url.host, url.port, use_ssl: true) do |http|
    if http.head(url.request_uri)['Content-Type'].start_with? 'image'
      @thumbnail
    else
      DEFAULT_VONAGE_LOGO_URL
    end
  end
end