Class: WordPress
- Inherits:
-
Object
- Object
- WordPress
- Defined in:
- lib/wordpress-api.rb
Class Method Summary collapse
- .get_attachment(source, attachment) ⇒ Object
-
.get_media(source, params = nil, attachment = nil) ⇒ Object
By default gets the 9 most recent media objects from the WordPress site.
- .get_post(source, post) ⇒ Object
-
.get_posts(source, params = nil, post = nil) ⇒ Object
Get Posts from a WordPress source.
-
.get_taxonomies(source, params = nil, taxonomy = nil) ⇒ Object
By default, returns all taxonomies registered in the WordPress database.
- .get_taxonomy(source, taxonomy) ⇒ Object
-
.get_terms(source, taxonomy, term = nil) ⇒ Object
Get terms.
- .prepare_query(params) ⇒ Object
- .query_load_json(source, endpoint, params) ⇒ Object
- .query_url(uri) ⇒ Object
- .set_source(source) ⇒ Object
Instance Method Summary collapse
Class Method Details
.get_attachment(source, attachment) ⇒ Object
96 97 98 |
# File 'lib/wordpress-api.rb', line 96 def self.(source, ) posts = self.query_load_json(source, "media/#{}") end |
.get_media(source, params = nil, attachment = nil) ⇒ Object
By default gets the 9 most recent media objects from the WordPress site. If passed optional parameters, will filter those posts accordingly. Like get_posts, this should be passed as a hash. If a specific attachment is given as the third parameter, it will return only that attachment’s object and the ‘params` hash will be ignored (if passed)
Requires: source (str): the url to the root API endpoint attachment (int, optional): a specific attachment to get params (hash, optional): a hash of parameters to filter by
Note, the hash for the params argument should be formed like this:
params = ‘posts_per_page’=>‘1’,‘order’=>‘ASC’
Even though WordPress requires the “filter” argument, we will prepend that for you in this method.
88 89 90 91 92 93 94 |
# File 'lib/wordpress-api.rb', line 88 def self.get_media(source, params = nil, = nil) if posts = (source, ) else posts = self.query_load_json(source, "media", params) end end |
.get_post(source, post) ⇒ Object
64 65 66 |
# File 'lib/wordpress-api.rb', line 64 def self.get_post(source, post) posts = self.query_load_json(source, "posts/#{post}", params = nil) end |
.get_posts(source, params = nil, post = nil) ⇒ Object
Get Posts from a WordPress source
Requires: source (str): the url to the root API endpoint params (hash): a hash of parameters to filter by
Note, the hash for the params argument should be formed like this:
params = ‘posts_per_page’=>‘1’,‘order’=>‘ASC’
Even though WordPress requires the “filter” argument, we will prepend that for you in this method.
56 57 58 59 60 61 62 |
# File 'lib/wordpress-api.rb', line 56 def self.get_posts(source, params = nil, post = nil) if post.nil? wpposts = self.query_load_json(source, "posts", params) else wpposts = self.get_post(source, post) end end |
.get_taxonomies(source, params = nil, taxonomy = nil) ⇒ Object
By default, returns all taxonomies registered in the WordPress database.
If passed a hash of parameters, will filter the taxonomies accordingly. (See wp-json documentation at wp-api.org/#taxonomies_retrieve-all-taxonomies)
Requires: source (str): the url to the root API endpoint
Optional attachment (int): a specific attachment to get params (hash): a hash of parameters to filter by (currently no filters are documented at wp-api.org)
Note, the hash for the params argument should be formed like this:
params = ‘posts_per_page’=>‘1’,‘order’=>‘ASC’
Even though WordPress requires the “filter” argument, we will prepend that for you in this method.
122 123 124 125 126 127 128 |
# File 'lib/wordpress-api.rb', line 122 def self.get_taxonomies(source, params = nil, taxonomy = nil) if taxonomy get_taxonomy(source, taxonomy) else taxonomies = self.query_load_json(source, "taxonomies") end end |
.get_taxonomy(source, taxonomy) ⇒ Object
130 131 132 |
# File 'lib/wordpress-api.rb', line 130 def self.get_taxonomy(source, taxonomy) taxonomy = self.query_load_json(source, "taxonomies/#{taxonomy}") end |
.get_terms(source, taxonomy, term = nil) ⇒ Object
Get terms
Retrieves all terms associated with a given taxonomy.
Requires: source (str): the url to the root API endpoint taxonomy (str): the taxonomy from which to retrieve terms
Optional: term (str): the slug of the term to retrieve
145 146 147 148 149 150 151 |
# File 'lib/wordpress-api.rb', line 145 def self.get_terms(source, taxonomy, term = nil) if term terms = get_term(source, taxonomy, term) else terms = self.query_load_json(source, "/taxonomies/#{taxonomy}/terms") end end |
.prepare_query(params) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/wordpress-api.rb', line 18 def self.prepare_query(params) args = Hash.new if params.is_a?(Hash) params.each{|x| filter = "filter[#{x[0]}]"; arg = {filter => x[1]}; args = args.merge(arg); return args } else raise RuntimeError, "Something is wrong, perhaps you did not pass a hash." end end |
.query_load_json(source, endpoint, params) ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/wordpress-api.rb', line 32 def self.query_load_json(source, endpoint, params) url = URI("#{source}#{endpoint}/") if params args = prepare_query params url.query = URI.encode_www_form(args) end data = self.query_url(url) content = JSON.load(data) return content end |
.query_url(uri) ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/wordpress-api.rb', line 10 def self.query_url(uri) begin data = Net::HTTP.get(uri) rescue puts "There was an error with the provided URL: #{uri}" end end |
.set_source(source) ⇒ Object
6 7 8 |
# File 'lib/wordpress-api.rb', line 6 def self.set_source(source) @source = source end |
Instance Method Details
#get_term(source, taxonomy, term) ⇒ Object
153 154 155 |
# File 'lib/wordpress-api.rb', line 153 def get_term(source, taxonomy, term) term = self.query_load_json(source, "/taxonomies/#{taxonomy}/terms/#{term}") end |