Class: Quickpress::Wordpress

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

Overview

Represents an instance of a Wordpress connection. Handles direct calls to the Wordpress API.

Constant Summary collapse

VERY_LARGE_NUMBER =

Yes it is

2**31 - 1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, user, pass) ⇒ Wordpress

Creates a new connection to url with user/pass.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/quickpress/wordpress.rb', line 23

def initialize(url, user, pass)

  # Sanitizing url:
  #
  # If provided with something like "mysite.com/blog"
  # * host will be "mysite.com"
  # * path will be "/blog/xmlrpc.php"

  host  = url
  path  = "/xmlrpc.php"
  paths = url.split '/'

  if paths.size > 1
    host = paths[0]

    path = '/' + paths[1..-1].join('/') + "/xmlrpc.php"
  end

  @client = Rubypress::Client.new(:host => host,
                                  :path => path,
                                  :username => user,
                                  :password => pass)

  # Actually connecting, takes a while
  options = @client.getOptions(:options => ["blog_title",
                                            "blog_tagline",
                                            "blog_url"])

  @title   = options["blog_title"]["value"]
  @tagline = options["blog_tagline"]["value"]
  @url     = options["blog_url"]["value"]

  @categories = []
  terms = @client.getTerms(:taxonomy => "category")
  terms.each do |term|
    @categories << term["name"]
  end
end

Instance Attribute Details

#categoriesObject (readonly)

All categories on blog



14
15
16
# File 'lib/quickpress/wordpress.rb', line 14

def categories
  @categories
end

#optionsObject (readonly)

Blog’s options in a Hash. Need to call get_options first.



17
18
19
# File 'lib/quickpress/wordpress.rb', line 17

def options
  @options
end

#taglineObject (readonly)

Blog subtitle.



12
13
14
# File 'lib/quickpress/wordpress.rb', line 12

def tagline
  @tagline
end

#titleObject (readonly)

Blog title.



11
12
13
# File 'lib/quickpress/wordpress.rb', line 11

def title
  @title
end

#urlObject (readonly)

Blog address



13
14
15
# File 'lib/quickpress/wordpress.rb', line 13

def url
  @url
end

Instance Method Details

#delete_page(id) ⇒ Object

Deletes page with numerical id.



146
147
148
149
150
151
# File 'lib/quickpress/wordpress.rb', line 146

def delete_page id
  @client.deletePost(:post_id => id,
                     :filter => {
                       :post_type => 'page'
                     })
end

#delete_post(id) ⇒ Object

Deletes post with numerical id.



141
142
143
# File 'lib/quickpress/wordpress.rb', line 141

def delete_post id
  @client.delnetePost(:post_id => id)
end

#edit_post(options) ⇒ Object

Edits post/page on the Wordpress site with options.

Format is the same as Wordpress#new_post. Check it out.



93
94
95
96
97
98
99
100
# File 'lib/quickpress/wordpress.rb', line 93

def edit_post options

  @client.editPost(options)
  info = @client.getPost(:post_id => options[:post_id],
                         :fields  => [:link])

  info["link"]
end

#get_all_mediaObject

Returns all media items on the blog



256
257
258
259
260
261
262
263
264
# File 'lib/quickpress/wordpress.rb', line 256

def get_all_media
  lib = @client.getMediaLibrary
  return [] if lib.empty?

  # Getting only the fields we're interested on
  lib.map do |m|
    [m["attachment_id"], m["title"], m["link"]]
  end
end

#get_category_statusObject

Returns categories and how many posts they have. It’s an Array of two elements:

  1. Category name

  2. Post count



225
226
227
228
229
# File 'lib/quickpress/wordpress.rb', line 225

def get_category_status
  status = @client.getTerms(:taxonomy => 'category')

  status.map { |s| [s["name"], s["count"]] } # all we need
end

#get_comment_statusObject

Returns comment counts according to their status. It’s an Array of two elements:

  1. Wordpress’ internal status name

  2. Comment counts on that status



213
214
215
216
217
# File 'lib/quickpress/wordpress.rb', line 213

def get_comment_status
  status = @client.getCommentCount

  status.to_a
end

#get_media(id) ⇒ Object



266
267
268
# File 'lib/quickpress/wordpress.rb', line 266

def get_media id
  @client.getMediaItem(:attachment_id => id)
end

#get_optionsObject

Retrieves as much metadata about the blog as it can.

Returns an array of 3-element arrays:

  1. Wordpress’ internal option name. You must use it to set options. See set_options.

  2. Human-readable description of the option.

  3. Current value.

The values are detailed here: codex.wordpress.org/Option_Reference



165
166
167
168
169
# File 'lib/quickpress/wordpress.rb', line 165

def get_options
  options = @client.getOptions

  options.map { |o| [o[0], o[1]["desc"], o[1]["value"]] }
end

#get_page(id) ⇒ Object

Returns page with numerical id. It’s a Hash with attributes/values.



121
122
123
124
125
126
# File 'lib/quickpress/wordpress.rb', line 121

def get_page id
  @client.getPost(:post_id => id,
                  :filter => {
                    :post_type => 'page'
                  })
end

#get_pages(ammount = 0) ⇒ Object

Returns ammount pages. If ammount is zero, will return all posts. FIXME when getting by ammount it is ordered by the opposite



131
132
133
134
135
136
137
138
# File 'lib/quickpress/wordpress.rb', line 131

def get_pages(ammount=0)
  ammount = VERY_LARGE_NUMBER if ammount.zero?

  @client.getPosts(:filter => {
                     :number => ammount,
                     :post_type => 'page'
                   })
end

#get_post(id) ⇒ Object

Returns post with numerical id. It’s a Hash with attributes/values.



105
106
107
# File 'lib/quickpress/wordpress.rb', line 105

def get_post id
  @client.getPost(:post_id => id)
end

#get_posts(ammount = 0) ⇒ Object

Returns ammount posts. If ammount is zero, will return all posts. FIXME when getting by ammount it is ordered by the opposite



112
113
114
115
116
# File 'lib/quickpress/wordpress.rb', line 112

def get_posts(ammount=0)
  ammount = VERY_LARGE_NUMBER if ammount.zero?

  @client.getPosts(:filter => { :number => ammount })
end

#get_usersObject

Returns all users currently registered on the blog.

It’s an Array of two-element Arrays:

  1. Wordpress’ internal info name

  2. It’s value



195
196
197
198
199
200
201
202
203
204
205
# File 'lib/quickpress/wordpress.rb', line 195

def get_users
  users = @client.getUsers

  # Replacing XML-RPC's ugly DateTime class
  # with Ruby's Time
  users.each do |u|
    u["registered"] = u["registered"].to_time
  end

  users.map { |u| u.to_a }
end

#new_media(filename) ⇒ Object

Uploads filename to Wordpress, returning it’s ID, URL and unique filename inside Wordpress.



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/quickpress/wordpress.rb', line 234

def new_media filename

  content = XMLRPC::Base64.new(File.read(filename))
  if content.encoded.empty?
    fail "File '#{filename}' is empty"
  end

  mime = MimeMagic.by_path filename
  if mime.nil?
    fail "Unknown MIME type for '#{filename}'"
  end

  file = @client.uploadFile(:data => {
                              :name => File.basename(filename),
                              :bits => content,
                              :type => mime.type
                            })

  return file['id'], file['url'], file['file']
end

#new_post(options) ⇒ Object

Sends a post/page to the Wordpress site with options.

options is a Hash with the following fields:

  • :post_date => Ruby Time Object (or [] for Time.now)

  • :post_title => String

  • :post_content => String

  • :post_status => ‘publish’/‘draft’/‘private’

  • :post_type => ‘post’(default) / ‘page’

To Wordpress, Posts and Pages are the same thing. The only thing that makes them different is the option :post_type.



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/quickpress/wordpress.rb', line 76

def new_post options
  # Sending post
  id = @client.newPost(:content => options)

  # Getting link for it
  info = @client.getPost(:post_id => id,
                         :fields  => [:link])
  link = info["link"]

  return id, link
end

#set_options(new_options) ⇒ Object

Sets the blog’s options according to new_options hash. It points to an array with two elements:

  1. Wordpress’ internal option name. See get_options.

  2. It’s new value. See link on get_options for possible values.

Returns the new options, the same way as get_options.



182
183
184
185
186
187
# File 'lib/quickpress/wordpress.rb', line 182

def set_options new_options
  options = @client.setOptions

  options.map { |o| [o[0], o[1]["desc"], o[1]["value"]] }

end