Class: WordpressClient::Client
- Inherits:
-
Object
- Object
- WordpressClient::Client
- Defined in:
- lib/wordpress_client/client.rb
Posts collapse
-
#create_post(attributes) ⇒ Post
Create a new Post with the given attributes in Wordpress and return it.
-
#delete_post(id, force: false) ⇒ Object
Deletes the Post with the given ID.
-
#find_post(id) ⇒ Post
Find the Post with the given ID, or raises an error if not found.
-
#posts(per_page: 10, page: 1) ⇒ PaginatedCollection[Post]
Find Posts matching given parameters.
-
#update_post(id, attributes) ⇒ Post
Update the Post with the given id, setting the supplied attributes in Wordpress and returning an updated Post.
Categories collapse
-
#categories(per_page: 10, page: 1) ⇒ PaginatedCollection[Category]
Find Categories in the Wordpress install.
-
#create_category(attributes) ⇒ Category
Create a new Category with the given attributes.
-
#find_category(id) ⇒ Category
Find Category with the given ID.
-
#update_category(id, attributes) ⇒ Category
Update the Category with the given id, setting the supplied attributes.
Tags collapse
-
#create_tag(attributes) ⇒ Tag
Create a new Tag with the given attributes.
-
#find_tag(id) ⇒ Tag
Find Tag with the given ID.
-
#tags(per_page: 10, page: 1) ⇒ PaginatedCollection[Tag]
Find Tags in the Wordpress install.
-
#update_tag(id, attributes) ⇒ Tag
Update the Tag with the given id, setting the supplied attributes.
Media collapse
-
#find_media(id) ⇒ Media
Find Media with the given ID.
-
#media(per_page: 10, page: 1) ⇒ PaginatedCollection[Media]
Find Media in the Wordpress install.
-
#update_media(id, attributes) ⇒ Media
Update the Media with the given id, setting the supplied attributes.
-
#upload(io, mime_type:, filename:) ⇒ Media
Create a new Media by uploading a IO stream.
-
#upload_file(filename, mime_type:) ⇒ Media
Create a new Media by uploading a file from disk.
Instance Method Summary collapse
-
#initialize(connection) ⇒ Client
constructor
A new instance of Client.
- #inspect ⇒ Object
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.
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
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.
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.
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.
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.
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.
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.
170 171 172 |
# File 'lib/wordpress_client/client.rb', line 170 def find_tag(id) connection.get(Tag, "tags/#{id.to_i}") end |
#inspect ⇒ Object
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.
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.
161 162 163 |
# File 'lib/wordpress_client/client.rb', line 161 def (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.
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.
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
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.
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.
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.
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 |