class MediumExport::Publisher

Attributes

api_client[R]
content[R]
shell[R]

Public Class Methods

new(content:, shell:, api_client:) click to toggle source
# File lib/middleman-medium_export/publisher.rb, line 4
def initialize(content:, shell:, api_client:)
  @content = content
  @shell = shell
  @api_client = api_client
end

Public Instance Methods

call() click to toggle source
# File lib/middleman-medium_export/publisher.rb, line 10
def call
  shell.say("Preparing to publish #{content.size} articles") if content.size > 1

  content.each do |article|
    publish(article)
    shell.say(%Q(Published: "#{article.title}"), :green)
  end
end

Private Instance Methods

publish(article) click to toggle source
# File lib/middleman-medium_export/publisher.rb, line 21
def publish(article)
  updated_html = article.local_images.inject(article.html) do |html, image|
    response = api_client.upload_image(image: File.open(image.location))
    url = response['data']['url'] # it may contain errors  {"errors"=>[{"message"=>"Token was invalid.", "code"=>6003}]}
    html.gsub(image.src, url)
  end

  api_client.publish(content: updated_html, tags: article.tags, title: article.title)
end