Class: WordpressClient::Client

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

Posts collapse

Categories collapse

Tags collapse

Media collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Client

Returns a new instance of Client.



3
4
5
# File 'lib/wordpress_client/client.rb', line 3

def initialize(connection)
  @connection = connection
end

Instance Method Details

#categories(per_page: 10, page: 1) ⇒ PaginatedCollection[Category]

Find Categories in the Wordpress install.



110
111
112
# File 'lib/wordpress_client/client.rb', line 110

def categories(per_page: 10, page: 1)
  connection.get_multiple(Category, "categories", page: page, per_page: per_page)
end

#create_category(attributes) ⇒ Category

Create a new WordpressClient::Category with the given attributes.

Parameters:

  • attributes (Hash<Symbol,Object>)

    attribute list, containing parameters accepted by the API.

Options Hash (attributes):

  • name (String)

    Name of the category (required).

  • slug (String)

    Slug of the category (optional).

  • description (String)

    Description of the category (optional).

Returns:

Raises:

See Also:



135
136
137
# File 'lib/wordpress_client/client.rb', line 135

def create_category(attributes)
  connection.create(Category, "categories", attributes)
end

#create_post(attributes) ⇒ Post

Create a new Post with the given attributes in Wordpress and return it.

In addition to / the accepted parameters of the API, this method also takes the following keys:

  • :meta

  • :category_ids

  • :tag_ids

Parameters:

  • attributes (Hash<Symbol,Object>)

    attribute list, containing accepted parameters or the custom parameters listed above.

Options Hash (attributes):

  • meta (Hash<String,String>)

    Hash of meta values.

  • category_ids (Array<Fixnum>)

    List of category IDs the Post should belong to.

  • tag_ids (Array<Fixnum>)

    List of tag IDs the Post should have.

Returns:

Raises:

See Also:



58
59
60
# File 'lib/wordpress_client/client.rb', line 58

def create_post(attributes)
  connection.create(Post, "posts", attributes, redirect_params: {_embed: nil})
end

#create_tag(attributes) ⇒ Tag

Create a new Tag with the given attributes.

Parameters:

  • attributes (Hash<Symbol,Object>)

    attribute list, containing parameters accepted by the API.

Options Hash (attributes):

  • name (String)

    Name of the tag (required).

  • slug (String)

    Slug of the tag (optional).

  • description (String)

    Description of the tag (optional).

Returns:

  • (Tag)

    the new Tag

Raises:

See Also:



186
187
188
# File 'lib/wordpress_client/client.rb', line 186

def create_tag(attributes)
  connection.create(Tag, "tags", attributes)
end

#delete_post(id, force: false) ⇒ Object

Deletes the Post with the given ID.

Parameters:

  • id (Fixnum)

    The Post ID.

  • force (Boolean) (defaults to: false)

    When false, the Post will be put in the “Trash” of Wordpress. true causes the Post to be irrevocably deleted.

Returns:

  • true always



101
102
103
# File 'lib/wordpress_client/client.rb', line 101

def delete_post(id, force: false)
  connection.delete("posts/#{id.to_i}", {"force" => force})
end

#find_category(id) ⇒ Category

Find WordpressClient::Category with the given ID.

Returns:

Raises:



119
120
121
# File 'lib/wordpress_client/client.rb', line 119

def find_category(id)
  connection.get(Category, "categories/#{id.to_i}")
end

#find_media(id) ⇒ Media

Find Media with the given ID.

Returns:

Raises:



221
222
223
# File 'lib/wordpress_client/client.rb', line 221

def find_media(id)
  connection.get(Media, "media/#{id.to_i}")
end

#find_post(id) ⇒ Post

Find the Post with the given ID, or raises an error if not found.

Returns:

Raises:



33
34
35
# File 'lib/wordpress_client/client.rb', line 33

def find_post(id)
  connection.get(Post, "posts/#{id.to_i}", _embed: nil)
end

#find_tag(id) ⇒ Tag

Find Tag with the given ID.

Returns:

Raises:



170
171
172
# File 'lib/wordpress_client/client.rb', line 170

def find_tag(id)
  connection.get(Tag, "tags/#{id.to_i}")
end

#inspectObject



291
292
293
# File 'lib/wordpress_client/client.rb', line 291

def inspect
  "#<WordpressClient::Client #{connection.inspect}>"
end

#media(per_page: 10, page: 1) ⇒ PaginatedCollection[Media]

Find Media in the Wordpress install.



212
213
214
# File 'lib/wordpress_client/client.rb', line 212

def media(per_page: 10, page: 1)
  connection.get_multiple(Media, "media", page: page, per_page: per_page)
end

#posts(per_page: 10, page: 1) ⇒ PaginatedCollection[Post]

Find Posts matching given parameters.

Examples:

Finding 5 posts

posts = client.posts(per_page: 5)

Parameters:

  • page (Fixnum) (defaults to: 1)

    Current page for pagination. Defaults to 1.

  • per_page (Fixnum) (defaults to: 10)

    Posts per page. Defaults to 10.

Returns:



18
19
20
21
22
23
24
25
26
# File 'lib/wordpress_client/client.rb', line 18

def posts(per_page: 10, page: 1)
  connection.get_multiple(
    Post,
    "posts",
    per_page: per_page,
    page: page,
    _embed: nil,
  )
end

#tags(per_page: 10, page: 1) ⇒ PaginatedCollection[Tag]

Find Tags in the Wordpress install.

Returns:



161
162
163
# File 'lib/wordpress_client/client.rb', line 161

def tags(per_page: 10, page: 1)
  connection.get_multiple(Tag, "tags", page: page, per_page: per_page)
end

#update_category(id, attributes) ⇒ Category

Update the WordpressClient::Category with the given id, setting the supplied attributes.

Parameters:

  • attributes (Hash<Symbol,Object>)

    attribute list, containing parameters accepted by the API.

Options Hash (attributes):

  • name (String)

    Name of the category.

  • slug (String)

    Slug of the category.

  • description (String)

    Description of the category.

Returns:

Raises:

See Also:



152
153
154
# File 'lib/wordpress_client/client.rb', line 152

def update_category(id, attributes)
  connection.put(Category, "categories/#{id.to_i}", attributes)
end

#update_media(id, attributes) ⇒ Media

Update the Media with the given id, setting the supplied attributes.

Parameters:

  • attributes (Hash<Symbol,Object>)

    attribute list, containing parameters accepted by the API.

Returns:

  • (Media)

    The updated Media

Raises:

See Also:



285
286
287
# File 'lib/wordpress_client/client.rb', line 285

def update_media(id, attributes)
  connection.put(Media, "media/#{id.to_i}", attributes)
end

#update_post(id, attributes) ⇒ Post

Update the Post with the given id, setting the supplied attributes in Wordpress and returning an updated Post.

In addition to / the accepted parameters of the API, this method also takes the following keys:

  • :meta

  • :category_ids

  • :tag_ids

Examples:

Changing the title of a Post

new_post = client.update_post(post.id, title: "A better title")
new_post.title_html #=> "A better title"

Parameters:

  • id (Fixnum)

    ID of the post to update.

  • attributes (Hash<Symbol,Object>)

    attribute list, containing accepted parameters or the custom parameters listed above.

Options Hash (attributes):

  • meta (Hash<String,String>)

    Hash of meta values.

  • category_ids (Array<Fixnum>)

    List of category IDs the Post should belong to.

  • tag_ids (Array<Fixnum>)

    List of tag IDs the Post should have.

Returns:

Raises:

See Also:



90
91
92
# File 'lib/wordpress_client/client.rb', line 90

def update_post(id, attributes)
  connection.put(Post, "posts/#{id.to_i}", attributes)
end

#update_tag(id, attributes) ⇒ Tag

Update the Tag with the given id, setting the supplied attributes.

Parameters:

  • attributes (Hash<Symbol,Object>)

    attribute list, containing parameters accepted by the API.

Options Hash (attributes):

  • name (String)

    Name of the tag.

  • slug (String)

    Slug of the tag.

  • description (String)

    Description of the tag.

Returns:

  • (Tag)

    the updated Tag

Raises:

See Also:



203
204
205
# File 'lib/wordpress_client/client.rb', line 203

def update_tag(id, attributes)
  connection.put(Tag, "tags/#{id.to_i}", attributes)
end

#upload(io, mime_type:, filename:) ⇒ Media

Create a new Media by uploading a IO stream.

You need to provide both MIME type and filename for Wordpress to accept the file.

Examples:

Uploading a JPEG from a request

media = client.upload(
  request.body_stream, filename: "foo.jpg", mime_type: "image/jpeg"
)

Parameters:

  • io (IO-like object)

    IO stream (for example an open file) that will be the body of the media.

  • mime_type (String)

    the MIME type of the IO stream

  • filename (String)

    the filename that Wordpress should see. Requires a file extension to make Wordpress happy.

Returns:

  • (Media)

    the new Media

Raises:

See Also:



245
246
247
# File 'lib/wordpress_client/client.rb', line 245

def upload(io, mime_type:, filename:)
  connection.upload(Media, "media", io, mime_type: mime_type, filename: filename)
end

#upload_file(filename, mime_type:) ⇒ Media

Create a new Media by uploading a file from disk.

You need to provide MIME type for Wordpress to accept the file. The filename that Wordpress sees will automatically be derived from the passed path.

Examples:

Uploading a JPEG from disk

media = client.upload_file(
  "assets/ocean.jpg", mime_type: "image/jpeg"
)

Parameters:

  • filename (String)

    a path to a readable file.

  • mime_type (String)

    the MIME type of the file.

Returns:

  • (Media)

    the new Media

Raises:

See Also:



268
269
270
271
272
273
# File 'lib/wordpress_client/client.rb', line 268

def upload_file(filename, mime_type:)
  path = filename.to_s
  File.open(path, 'r') do |file|
    upload(file, mime_type: mime_type, filename: File.basename(path))
  end
end