Guest User

Untitled

a guest
Apr 21st, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. require 'octokit'
  2. require 'base64'
  3.  
  4. module GithubStaticPagesJob
  5. extend self
  6.  
  7. FILE_NAME_REGEX = /\w+[^\.md]/
  8.  
  9. def client
  10. credentials = {
  11. client_id: Settings.github.client_id,
  12. client_secret: Settings.github.client_secret
  13. }
  14. @client ||= Octokit::Client.new(credentials)
  15. end
  16.  
  17. def run
  18. begin
  19. content = get_content('agileventures/agileventures')
  20. process_markdown_pages(get_markdown_pages(content))
  21. rescue StandardError => e
  22. ErrorLoggingService.new(e).log("Trying to get the content from the repository may have caused the issue!")
  23. end
  24. end
  25.  
  26. private
  27.  
  28. def get_content(repository)
  29. client.contents(repository)
  30. end
  31.  
  32. def get_markdown_pages(content)
  33. content.select{|page| page if page[:path] =~ /\.md$/i}
  34. end
  35.  
  36. def process_markdown_pages(md_pages)
  37. md_pages.each do |page|
  38. begin
  39. filename = page[:path]
  40. page_content = client.contents('agileventures/agileventures', path: filename)
  41. markdown = Base64.decode64(page_content[:content])
  42. static_page = StaticPage.find_by_slug(get_slug(filename))
  43. static_page.nil? ? create_static_page(filename, markdown) : update_body(static_page, markdown)
  44. rescue Encoding::UndefinedConversionError => e
  45. ErrorLoggingService.new(e).log("Trying to convert this page: #{filename} caused the issue!")
  46. end
  47. end
  48. end
  49.  
  50. def create_static_page(filename, markdown)
  51. StaticPage.create(
  52. title: get_title(filename),
  53. body: convert_markdown_to_html(markdown),
  54. slug: get_slug(filename)
  55. )
  56. end
  57.  
  58. def update_body(static_page, markdown)
  59. static_page.body = convert_markdown_to_html(markdown)
  60. static_page.save!
  61. end
  62.  
  63. def get_title(filename)
  64. get_slug(filename).tr("-", " ").titleize
  65. end
  66.  
  67. def get_slug(filename)
  68. FILE_NAME_REGEX.match(filename)[0].downcase.tr("_", "-")
  69. end
  70.  
  71. def convert_markdown_to_html(markdown)
  72. MarkdownConverter.new(markdown).as_html
  73. end
  74. end
Add Comment
Please, Sign In to add comment