Class: AuthorParser

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

Constant Summary collapse

PATH_TO_AUTHORS =
"#{Rails.configuration.blog_path}/authors/".freeze

Class Method Summary collapse

Class Method Details

.asset_exist?(path) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
# File 'lib/nexmo_developer/app/services/author_parser.rb', line 44

def self.asset_exist?(path)
  if Rails.configuration.assets.compile
    Rails.application.precompiled_assets.include? path
  else
    Rails.application.assets_manifest.assets[path].present?
  end
end

.fetch_all_authorsObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/nexmo_developer/app/services/author_parser.rb', line 6

def self.fetch_all_authors
  authors = {}

  authors_folder_path = Dir.glob("#{PATH_TO_AUTHORS}*.json")

  authors_folder_path.each do |filename|
    document = File.read(filename)

    author_hash = YAML.safe_load(document)

    short_name = File.basename(filename, '.json')

    author_hash.merge!({ 'short_name' => short_name })

    authors[short_name.to_sym] = author_hash
  end

  authors
end

.fetch_all_blogposts_from(author) ⇒ Object



38
39
40
41
42
# File 'lib/nexmo_developer/app/services/author_parser.rb', line 38

def self.fetch_all_blogposts_from(author)
  blogposts_hash = JSON.parse(File.read(BlogpostParser::PATH_TO_INDEX))

  blogposts_hash.select { |blogpost| blogpost['author'] == author }
end

.fetch_author(name) ⇒ Object



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

def self.fetch_author(name)
  author_json_path = "#{PATH_TO_AUTHORS}#{name}.json"

  return unless File.exist?(author_json_path)

  file = File.read(author_json_path)

  short_name = File.basename(author_json_path, '.json')

  JSON.parse(file).merge!({ 'short_name' => short_name })
end