Class: Skriptorium::GitHubPages::Destination
- Inherits:
-
BaseDestination
- Object
- BaseDestination
- Skriptorium::GitHubPages::Destination
- Defined in:
- lib/skriptorium/github_pages/destination.rb
Instance Attribute Summary
Attributes inherited from BaseDestination
Instance Method Summary collapse
- #exists?(article) ⇒ Boolean
-
#initialize(config) ⇒ Destination
constructor
A new instance of Destination.
- #publish(article) ⇒ Object
Methods inherited from BaseDestination
Constructor Details
#initialize(config) ⇒ Destination
Returns a new instance of Destination.
7 8 9 10 11 12 13 |
# File 'lib/skriptorium/github_pages/destination.rb', line 7 def initialize(config) super @repo = config['repo'] @token = config['token'] @branch = config['branch'] || 'master' @path = config['path'] || '_posts' end |
Instance Method Details
#exists?(article) ⇒ Boolean
15 16 17 18 19 20 21 |
# File 'lib/skriptorium/github_pages/destination.rb', line 15 def exists?(article) filename = generate_filename(article) with_client(url: "https://api.github.com/repos/#{@repo}/contents/#{@path}/#{filename}") do |client| client.get('').success? end end |
#publish(article) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/skriptorium/github_pages/destination.rb', line 23 def publish(article) filename = generate_filename(article) content = article.to_markdown(layout: 'post', date: article.published_at.strftime('%Y-%m-%d %H:%M:%S %z')) with_client(url: "https://api.github.com/repos/#{@repo}/contents/#{@path}/#{filename}") do |client| payload = { message: "Add post: #{article.title}", content: Base64.strict_encode64(content), branch: @branch } response = client.put('', JSON.generate(payload)) # Если файл уже существует — GitHub требует sha для обновления if !response.success? && response.status == 422 get_resp = client.get('') if get_resp.success? sha = JSON.parse(get_resp.body)['sha'] payload[:sha] = sha payload[:message] = "Update post: #{article.title}" response = client.put('', JSON.generate(payload)) end end raise "GitHub publish error: #{response.status} - #{response.body}" unless response.success? puts " Published to GitHub Pages: #{JSON.parse(response.body)['html_url']}" end end |