class Object

Public Instance Methods

register_image(vv) click to toggle source
# File lib/jupyter_to_scrapbox.rb, line 8
def register_image(vv)
  f=Tempfile.new(['image','.png'],Dir.tmpdir,:binary => true)
  f.write Base64.decode64(vv)
  image_path=File.expand_path(f.to_path)
  f.close(false)
  vp image_path
  vputs File.new(image_path, 'rb')
  gyazo_url="https://upload.gyazo.com/api/upload"
  begin
    r = RestClient.post gyazo_url, {
      "access_token" => ENV["GYAZO_TOKEN"],
        :imagedata => File.new(image_path, 'rb')
    }
    return JSON.parse(r)
  rescue => e
    p e
    puts e.backtrace
    puts <<EOS
Failed to register image data.
- Be online.
- Set your ACCESS_TOKEN to the environment variable "GYAZO_TOKEN"
EOS
  ensure
    FileUtils.rm(image_path)
  end
  exit(1)
end