Class: BlogpostParser

Inherits:
Object
  • Object
show all
Defined in:
lib/nexmo_developer/app/services/blogpost_parser.rb

Constant Summary collapse

PATH_TO_INDEX =
"#{ENV['BLOG_PATH']}/blogposts/blogposts_info.json".freeze

Class Method Summary collapse

Class Method Details

.buildObject



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/nexmo_developer/app/services/blogpost_parser.rb', line 17

def self.build
  all_blogposts = []

  ['en', 'cn', 'it'].each { |locale| all_blogposts += build_index_with_locale(locale) }

  #   Sort by DATE
  all_blogposts = all_blogposts.sort_by { |k| k['published_at'] }.reverse

  #   Write content
  File.write(PATH_TO_INDEX, JSON.pretty_generate(all_blogposts))
end

.build_custom_attributes(frontmatter, filename, locale) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/nexmo_developer/app/services/blogpost_parser.rb', line 38

def self.build_custom_attributes(frontmatter, filename, locale)
  frontmatter.except!('comments', 'redirect', 'old_categories')

  filename = File.basename(filename, '.md')
  year, month, day = frontmatter['published_at'].strftime('%y %m %d').split

  {
    'locale' => locale,
    'link' => "blog/#{locale == 'en' ? '' : "#{locale}/"}#{year}/#{month}/#{day}/#{filename}",
    'filename' => File.basename(filename, '.md'),
    'author' => AuthorParser.fetch_author(frontmatter['author']),
    'category' => CategoryParser.fetch_category(frontmatter['category']),
  }
end

.build_index_with_locale(locale = 'en') ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/nexmo_developer/app/services/blogpost_parser.rb', line 29

def self.build_index_with_locale(locale = 'en')
  blogposts_locale_path = Dir.glob("#{ENV['BLOG_PATH']}/blogposts/#{locale}/*.md")

  # TODO: - fix the issue with this blogpost
  blogposts_locale_path -= ["#{Rails.configuration.blog_path}/blogposts/en/add-video-capabilities-to-zendesk-with-vonage-video-api.md"]

  blogposts_locale_path.map { |filename| build_show_with_locale(filename, locale) }
end

.build_show_with_locale(filename, locale = 'en') ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/nexmo_developer/app/services/blogpost_parser.rb', line 53

def self.build_show_with_locale(filename, locale = 'en')
  document = File.read(filename).gsub('/content/blog/') do |match| # gsub Netlify img urls
    "#{Blog::Blogpost::CLOUDFRONT_BLOG_URL}blogposts/#{match.gsub('/content/blog/', '')}"
  end

  frontmatter = YAML.safe_load(document, [Time])

  frontmatter.merge!(build_custom_attributes(frontmatter, filename, locale))
end

.fetch_allObject



6
7
8
9
10
11
# File 'lib/nexmo_developer/app/services/blogpost_parser.rb', line 6

def self.fetch_all
  # Add Rescue from error
  file = File.read(PATH_TO_INDEX)

  JSON.parse(file)
end

.fetch_all_publishedObject



13
14
15
# File 'lib/nexmo_developer/app/services/blogpost_parser.rb', line 13

def self.fetch_all_published
  fetch_all.select { |b| b['published'] && !b['outdated'] }
end