Skip to content

Instantly share code, notes, and snippets.

@nisevi
Last active April 15, 2018 09:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nisevi/9f3c7838e5df1d609b77f79c87a6fa94 to your computer and use it in GitHub Desktop.
Save nisevi/9f3c7838e5df1d609b77f79c87a6fa94 to your computer and use it in GitHub Desktop.
Module that holds the logic for pulling and saving to database the '.md' files that live in the root path of a GitHub repository.
require 'octokit'
require 'base64'
module GithubStaticPagesJob
extend self
FILE_NAME_REGEX = /\w+[^\.md]/
def client
credentials = {
client_id: Settings.github.client_id,
client_secret: Settings.github.client_secret
}
@client ||= Octokit::Client.new(credentials)
end
def run
begin
content = get_content('agileventures/agileventures')
process_markdown_pages(get_markdown_pages(content))
rescue StandardError => e
ErrorLoggingService.new(e).log("Trying to get the content from the repository may have caused the issue!")
end
end
private
def get_content(repository)
client.contents(repository)
end
def get_markdown_pages(content)
content.select{|page| page if page[:path] =~ /\.md$/i}
end
def process_markdown_pages(md_pages)
md_pages.each do |page|
begin
filename = page[:path]
page_content = client.contents('agileventures/agileventures', path: filename)
markdown = Base64.decode64(page_content[:content])
static_page = StaticPage.find_by_slug(get_slug(filename))
static_page.nil? ? create_static_page(filename, markdown) : update_body(static_page, markdown)
rescue Encoding::UndefinedConversionError => e
ErrorLoggingService.new(e).log("Trying to convert this page: #{filename} caused the issue!")
end
end
end
def create_static_page(filename, markdown)
StaticPage.create(
title: get_title(filename),
body: convert_markdown_to_html(markdown),
slug: get_slug(filename)
)
end
def update_body(static_page, markdown)
static_page.body = convert_markdown_to_html(markdown)
static_page.save!
end
def get_title(filename)
get_slug(filename).tr("-", " ").titleize
end
def get_slug(filename)
FILE_NAME_REGEX.match(filename)[0].downcase.tr("_", "-")
end
def convert_markdown_to_html(markdown)
MarkdownConverter.new(markdown).as_html
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment