Class: AsciiPress::WordPressSyncer

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

Instance Method Summary collapse

Constructor Details

#initialize(hostname, username, password, post_type, renderer, options = {}) ⇒ WordPressSyncer

Creates a synchronizer object which can be used to synchronize a set of asciidoc files to WordPress posts

Parameters:

  • hostname (String)

    Hostname for WordPress blog

  • username (String)

    Wordpress username

  • password (String)

    Wordpress password

  • post_type (String)

    Wordpress post type to synchronize posts with

  • renderer (Renderer)

    Renderer object which will be used to process asciidoctor files

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :logger (Logger)

    Logger to be used for informational output. Defaults to AsciiPress.logger

  • :filter_proc (Proc)

    Proc which is given an AsciiDoctor::Document object and returns true or false to decide if a document should be synchronized

  • :delete_not_found (Boolean)

    Should posts on the WordPress server which don’t match any documents locally get deleted?

  • :generate_tags (Boolean)

    Should asciidoctor tags be synchronized to WordPress? (defaults to false)

  • :post_status (String)

    The status to assign to posts when they are synchronized. Defaults to ‘draft’. See the rubypress documentation



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/ascii_press.rb', line 137

def initialize(hostname, username, password, post_type, renderer, options = {})
  @hostname = hostname
  @wp_client = Rubypress::Client.new(host: @hostname, username: username, password: password)
  @post_type = post_type
  @logger = options[:logger] || AsciiPress.logger
  @renderer = renderer || Renderer.new
  @filter_proc = options[:filter_proc] || Proc.new { true }
  @delete_not_found = options[:delete_not_found]
  @generate_tags = options[:generate_tags]
  @options = options

  all_pages = @wp_client.getPosts(filter: {post_type: @post_type, number: 1000})
  @all_pages_by_post_name = all_pages.index_by {|post| post['post_name'] }
  log :info, "Got #{@all_pages_by_post_name.size} pages from the database"
end

Instance Method Details

#sync(adoc_file_paths, custom_fields = {}) ⇒ Object

Parameters:

  • adoc_file_path (Array <String>)

    Paths of the asciidoctor files to synchronize

  • custom_fields (Hash) (defaults to: {})

    Custom fields for WordPress.



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/ascii_press.rb', line 155

def sync(adoc_file_paths, custom_fields = {})
  synced_post_names = []

  adoc_file_paths.each do |adoc_file_path|
    synced_post_names << sync_file_path(adoc_file_path, custom_fields)
  end

  if @delete_not_found
    (@all_pages_by_post_name.keys - synced_post_names).each do |post_name_to_delete|
      post_id = @all_pages_by_post_name[post_name_to_delete]['post_id']

      log :info, "Deleting missing post_name: #{post_name_to_delete} (post ##{post_id})"

      send_message(:deletePost, blog_id: @hostname, post_id: post_id)
    end

  end
end