Class: AsciiPress::WordPressSyncer

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of WordPressSyncer.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/ascii_press.rb', line 86

def initialize(blog_id, username, password, renderer, options = {})
  @blog_id = blog_id
  @wp_client = Rubypress::Client.new(host: @blog_id, username: username, password: password)
  @post_type = options[:post_type] || 'developer'
  @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]

  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



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/ascii_press.rb', line 101

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: @blog_id, post_id: post_id)
    end

  end
end

#sync_file_path(adoc_file_path, custom_fields = {}) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/ascii_press.rb', line 120

def sync_file_path(adoc_file_path, custom_fields = {})
  rendering = @renderer.render(adoc_file_path)

  return if !@filter_proc.call(rendering.doc)

  if !(slug = rendering.attribute_value(:slug))
    log :warn, "WARNING: COULD NOT POST DUE TO NO SLUG FOR: #{adoc_file_path}"
    return
  end

  title = rendering.doc.doctitle
  html = rendering.html

  log :info, "Syncing to WordPress: #{title} (slug: #{slug})"

  # log :info, "data: #{rendering.data.inspect}"

  content = {
              post_type:     @post_type,
              post_date:     Time.now - 60*60*24*30,
              post_content:  html,
              post_title:    title,
              post_name:     slug,
              post_status:   'publish',
              custom_fields: custom_fields
            }

  content[:terms_names] = {post_tag: rendering.tags} if @generate_tags

  if page = @all_pages_by_post_name[slug]
    if page['custom_fields']
      content[:custom_fields].each do |f|
        found = page['custom_fields'].find { |field| field['key'] == f[:key] }
        f['id'] = found['id'] if found
      end
    end

    post_id = page['post_id'].to_i

    log :info, "Editing Post ##{post_id} on _#{@blog_id}_ custom-field #{content[:custom_fields].inspect}"

    send_message(:editPost, blog_id: @blog_id, post_id: post_id, content: content)
  else
    log :info, "Making a new post for '#{title}' on _#{@blog_id}_"

    send_message(:newPost, blog_id: @blog_id, content: content)
  end

  slug
end