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`.



22
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
# File 'lib/quickpress/wordpress.rb', line 22

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



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

def categories
  @categories
end

#optionsObject (readonly)

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



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

def options
  @options
end

#taglineObject (readonly)

Blog subtitle.



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

def tagline
  @tagline
end

#titleObject (readonly)

Blog title.



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

def title
  @title
end

#urlObject (readonly)

Blog address



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

def url
  @url
end

Instance Method Details

#delete_page(id) ⇒ Object

Deletes page with numerical ‘id`.



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

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

#delete_post(id) ⇒ Object

Deletes post with numerical ‘id`.



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

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.



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

def edit_post options

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

  info["link"]
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



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

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



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

def get_comment_status
  status = @client.getCommentCount

  status.to_a
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



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

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.



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

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



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

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.



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

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



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

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



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

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_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`.



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

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`.



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

def set_options new_options
  options = @client.setOptions

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

end