lib/nexmo_developer/app/models/blog/blogpost.rb in station-0.1.3 vs lib/nexmo_developer/app/models/blog/blogpost.rb in station-0.1.4

- old
+ new

@@ -1,12 +1,12 @@ class Blog::Blogpost attr_accessor :title, :description, :thumbnail, :author, :published, :published_at, :updated_at, :category, :tags, :link, :locale, :slug, :spotlight, :filename, :content, :header_img_url - CLOUDFRONT_BLOG_URL = 'https://d226lax1qjow5r.cloudfront.net/blog/'.freeze - DEFAULT_HEADER_IMG_URL = 'https://s3.eu-west-1.amazonaws.com/developer.vonage.com/vonage-logo-images/vonage-wide-logo.png'.freeze + 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-wide-logo.png'.freeze def initialize(attributes) @title = attributes['title'] @description = attributes['description'] @thumbnail = attributes['thumbnail'] || '' @@ -22,11 +22,11 @@ @author = Blog::Author.new(attributes['author'] || {}) # TODO: DEFAULT AUTHOR @category = Blog::Category.new(attributes['category']) @content = '' - @header_img_url = build_header_img_url + @header_img_url = build_bucket_img_url_from_thumbnail @replacement_url = attributes['replacement_url'] end def self.build_blogpost_from_path(path, locale) @@ -34,28 +34,34 @@ path = "#{Rails.configuration.blog_path}/blogposts/#{locale}/#{path}.md" default_not_found_page(path) unless File.exist?(path) - document = File.read(path) + document = File.read(path).gsub('/content/blog/') do |match| # gsub Netlify img urls + "#{Blog::Blogpost::CLOUDFRONT_BLOG_URL}blogposts/#{match.gsub('/content/blog/', '')}" + end blogpost = new(BlogpostParser.build_show_with_locale(path, locale)) blogpost.content = Nexmo::Markdown::Renderer.new({}).call(document) blogpost end - def build_header_img_url + def build_bucket_img_url_from_thumbnail require 'net/http' require 'addressable' - url = Addressable::URI.parse("#{Blog::Blogpost::CLOUDFRONT_BLOG_URL}blogposts/#{thumbnail.gsub('/content/blog/', '')}") + @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' - url + @thumbnail else - DEFAULT_HEADER_IMG_URL + DEFAULT_VONAGE_LOGO_URL end end end def self.default_not_found_page(path)