Class: HatenablogPublisher::Client

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

Constant Summary collapse

IMAGE_PATTERN =
/[^`]!\[.*\]\((.*)\)/.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Client

Returns a new instance of Client.



10
11
12
13
14
# File 'lib/hatenablog_publisher/client.rb', line 10

def initialize(args)
  @options = HatenablogPublisher::Options.create(args)
  io = HatenablogPublisher::Io.new(@options)
  @context = HatenablogPublisher::Context.new(io)
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



6
7
8
# File 'lib/hatenablog_publisher/client.rb', line 6

def context
  @context
end

Instance Method Details

#generate_bodyObject



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/hatenablog_publisher/client.rb', line 41

def generate_body
  generator = HatenablogPublisher::Generator::Body.new(@context, @options)
  body = generator.generate

  if @options.ad_type && @options.ad_file
    category = HatenablogPublisher::FixedContent::Ad.new(@context.categories, @options)
    body += category.format_body
  end

  body
end

#image_tagsObject



37
38
39
# File 'lib/hatenablog_publisher/client.rb', line 37

def image_tags
  @context.text.scan(IMAGE_PATTERN).flatten.reject { |tag| tag.match('^http') }
end

#publishObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/hatenablog_publisher/client.rb', line 16

def publish
  photolife = HatenablogPublisher::Photolife.new
  dirname = File.dirname(@options.filename)

  image_tags.each do |tag|
    next if @context.posted_image?(tag)

    image = HatenablogPublisher::Image.new(File.join(dirname, tag))
    image_hash = photolife.upload(image)
    @context.add_image_context(image_hash, tag)
    @context.reload_context
  end

  body = generate_body

  entry = HatenablogPublisher::Entry.new(@context, @options)
  entry_hash = entry.post_entry(body)
  @context.add_entry_context(entry_hash)
  @context.reload_context
end