class HatenablogPublisher::Context

Attributes

categories[R]
hatena[R]
text[R]
title[R]
updated[R]

Public Class Methods

new(io) click to toggle source
# File lib/hatenablog_publisher/context.rb, line 10
def initialize(io)
  @io = io

  read_context
end

Public Instance Methods

add_entry_context(entry) click to toggle source
# File lib/hatenablog_publisher/context.rb, line 42
def add_entry_context(entry)
  @hatena ||= {}
  @hatena[:id] = entry['id'].first.split('-').last
  @updated = time_to_s(entry['updated'].first)
end
add_image_context(image, tag) click to toggle source
# File lib/hatenablog_publisher/context.rb, line 21
def add_image_context(image, tag)
  # APIのレスポンスをそのまま使用すると記事上でフォトライフへのリンクになってしまうため、管理画面から画像投稿した結果と合わせた(image -> plain)
  syntax = "[#{image['syntax'].first}]".gsub(/:image\]/,':plain]')

  @hatena ||= {}
  @hatena[:image] ||= {}
  @hatena[:image][tag.to_sym] = {
    syntax: syntax,
    id: image['id'].first,
    image_url: image['imageurl'].first
  }
end
image_syntax(tag) click to toggle source
# File lib/hatenablog_publisher/context.rb, line 34
def image_syntax(tag)
  @hatena.dig(:image, tag.to_sym, :syntax)
end
metadata() click to toggle source
# File lib/hatenablog_publisher/context.rb, line 52
def metadata
  {
    title: @title,
    category: @categories,
    updated: @updated,
    hatena: @hatena
  }
end
posted_image?(tag) click to toggle source
# File lib/hatenablog_publisher/context.rb, line 38
def posted_image?(tag)
  image_syntax(tag).present?
end
reload_context() click to toggle source
# File lib/hatenablog_publisher/context.rb, line 16
def reload_context
  write_context
  read_context
end
time_to_s(str) click to toggle source
# File lib/hatenablog_publisher/context.rb, line 48
def time_to_s(str)
  Time.parse(str).strftime('%Y-%m-%dT%H:%M:%S')
end

Private Instance Methods

read_context() click to toggle source
# File lib/hatenablog_publisher/context.rb, line 67
def read_context
  data, text = @io.read

  updated = data[:updated].to_s.empty? ? '' : time_to_s(data[:updated])

  @text = text
  @categories = data[:category]
  @updated = updated
  @title = data[:title]
  @hatena = data[:hatena] || {}
end
write_context() click to toggle source
# File lib/hatenablog_publisher/context.rb, line 63
def write_context
  @io.write(metadata, @text)
end