Class: HatenablogPublisher::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/hatenablog_publisher/context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ Context

Returns a new instance of Context.



11
12
13
14
15
# File 'lib/hatenablog_publisher/context.rb', line 11

def initialize(io)
  @io = io

  read_context
end

Instance Attribute Details

#categoriesObject (readonly)

Returns the value of attribute categories.



9
10
11
# File 'lib/hatenablog_publisher/context.rb', line 9

def categories
  @categories
end

#hatenaObject (readonly)

Returns the value of attribute hatena.



9
10
11
# File 'lib/hatenablog_publisher/context.rb', line 9

def hatena
  @hatena
end

#textObject (readonly)

Returns the value of attribute text.



9
10
11
# File 'lib/hatenablog_publisher/context.rb', line 9

def text
  @text
end

#titleObject (readonly)

Returns the value of attribute title.



9
10
11
# File 'lib/hatenablog_publisher/context.rb', line 9

def title
  @title
end

#updatedObject (readonly)

Returns the value of attribute updated.



9
10
11
# File 'lib/hatenablog_publisher/context.rb', line 9

def updated
  @updated
end

Instance Method Details

#add_entry_context(entry) ⇒ Object



43
44
45
46
47
# File 'lib/hatenablog_publisher/context.rb', line 43

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) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/hatenablog_publisher/context.rb', line 22

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) ⇒ Object



35
36
37
# File 'lib/hatenablog_publisher/context.rb', line 35

def image_syntax(tag)
  @hatena.dig(:image, tag.to_sym, :syntax)
end

#metadataObject



53
54
55
56
57
58
59
60
# File 'lib/hatenablog_publisher/context.rb', line 53

def 
  {
    title: @title,
    category: @categories,
    updated: @updated,
    hatena: @hatena
  }
end

#posted_image?(tag) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/hatenablog_publisher/context.rb', line 39

def posted_image?(tag)
  image_syntax(tag).present?
end

#reload_contextObject



17
18
19
20
# File 'lib/hatenablog_publisher/context.rb', line 17

def reload_context
  write_context
  read_context
end

#time_to_s(str) ⇒ Object



49
50
51
# File 'lib/hatenablog_publisher/context.rb', line 49

def time_to_s(str)
  Time.parse(str).strftime('%Y-%m-%dT%H:%M:%S')
end