wordpress-client 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -30,5 +30,14 @@ module Wordpress
30
30
  def logger
31
31
  @logger ||= Logger.new(STDOUT)
32
32
  end
33
+
34
+ def configure(&block)
35
+ block.call(configuration)
36
+ configuration
37
+ end
38
+
39
+ def configuration
40
+ @configuration ||= Configuration.new
41
+ end
33
42
  end
34
43
  end
@@ -1,5 +1,8 @@
1
1
  require 'wordpress/errors'
2
2
  require 'wordpress/request'
3
+ Dir.glob(File.expand_path('../api/*.rb', __FILE__)) do |path|
4
+ require path
5
+ end
3
6
 
4
7
  module Wordpress
5
8
  module API
@@ -7,39 +10,44 @@ module Wordpress
7
10
  base.send(:include, InstanceMethods)
8
11
  base.extend(ClassMethods)
9
12
  base.class_eval do
10
- Dir.glob(File.expand_path('../api/*.rb', __FILE__)) do |path|
11
- eval(File.open(path).read)
12
- end
13
+ include Posts
14
+ include Comments
15
+ include Sites
16
+ include Me
17
+ include Tests
18
+ include Follow
19
+ include Taxonomy
20
+ include Notifications
21
+ include FreshlyPressed
13
22
  end
14
23
  end
15
24
 
16
25
  module ClassMethods
17
- def register_api(name, method, path, valid_keys, response)
18
- arg_names = path.scan(/\$([\w]+)/).map{ |a| a[0] }
19
-
20
- @registered_apis ||= {}
21
- @registered_apis[name] = {:method => method, :path => path, :arg_names => arg_names, :valid_keys => valid_keys, :response => response}
22
-
23
- remove_method name if method_defined?(name)
24
- define_method name do |*args|
25
- params = args[-1].is_a?(Hash) ? args.pop : {}
26
+ end
26
27
 
27
- params.keys.each do |key|
28
- raise Wordpress::ArgumentError, "#{key} is not valid for this method." unless valid_keys.include?(key)
29
- end
28
+ module InstanceMethods
29
+ def exec_api(request, &response)
30
+ json = call(request, :bearer_token_request => true)
30
31
 
31
- path = path.dup
32
- arg_names.each do |key|
33
- path.gsub!(/\$#{key}/, args.shift.to_s)
34
- end
35
- data = args.shift || {}
32
+ if !json['error'].nil?
33
+ raise Wordpress::ResponseError, json
34
+ end
35
+
36
+ if response.nil?
37
+ Wordpress::OpenStruct.new(json)
38
+ else
39
+ instance_exec(json, &response)
40
+ end
41
+ end
36
42
 
37
- json = call_api(Wordpress::Request.new(method, path, params, data))
43
+ private
38
44
 
39
- instance_exec(json, &response)
45
+ def validate_keys!(params, valid_keys)
46
+ params.keys.each do |key|
47
+ raise Wordpress::ArgumentError, "#{key} is not valid for this method." unless valid_keys.include?(key)
40
48
  end
41
49
  end
42
-
50
+
43
51
  def default_keys
44
52
  [:http_envelope, :pretty, :meta, :callback].freeze
45
53
  end
@@ -47,22 +55,6 @@ module Wordpress
47
55
  def search_keys
48
56
  [:number, :offset, :page, :order, :order_by, :after, :before].freeze
49
57
  end
50
-
51
- def registered_apis
52
- (@registered_apis || {}).freeze
53
- end
54
- end
55
-
56
- module InstanceMethods
57
- def call_api(request)
58
- json = call(request, :bearer_token_request => true)
59
-
60
- if !json['error'].nil?
61
- raise Wordpress::ResponseError, json
62
- end
63
-
64
- json
65
- end
66
58
  end
67
59
  end
68
60
  end
@@ -1,26 +1,48 @@
1
1
  require "wordpress/object/comments"
2
2
  require "wordpress/object/comment"
3
3
 
4
- register_api :get_comments, :get, "/rest/v1/sites/$site/comments",
5
- [:context, :type, :status] + default_keys + search_keys,
6
- lambda{ |json| Wordpress::Object::Comments.new(self, json) }
4
+ module Wordpress::API
5
+ module Comments
6
+ def get_comments(site, params = {})
7
+ validate_keys! params, [:context, :type, :status] + default_keys + search_keys
8
+ exec_api(Wordpress::Request.new(:get, "/rest/v1/sites/#{site}/comments", params)) do |json|
9
+ Wordpress::Object::Comments.new(self, json)
10
+ end
11
+ end
7
12
 
8
- register_api :get_post_comments, :get, "/rest/v1/sites/$site/posts/$post_id/replies",
9
- [:context] + default_keys,
10
- lambda{ |json| Wordpress::Object::Comments.new(self, json) }
13
+ def get_post_comments(site, post_id, params = {})
14
+ validate_keys! params, [:context] + default_keys
15
+ exec_api(Wordpress::Request.new(:get, "/rest/v1/sites/#{site}/posts/#{post_id}/replies", params)) do |json|
16
+ Wordpress::Object::Comments.new(self, json)
17
+ end
18
+ end
11
19
 
12
- register_api :get_comment, :get, "/rest/v1/sites/$site/comments/$comment_id",
13
- [:context] + default_keys,
14
- lambda{ |json| Wordpress::Object::Comment.new(self, json) }
20
+ def get_comment(site, comment_id, params = {})
21
+ validate_keys! params, [:context] + default_keys
22
+ exec_api(Wordpress::Request.new(:get, "/rest/v1/sites/#{site}/comments/#{comment_id}", params)) do |json|
23
+ Wordpress::Object::Comment.new(self, json)
24
+ end
25
+ end
15
26
 
16
- register_api :update_comment, :post, "/rest/v1/sites/$site/comments/$comment_id",
17
- [:context] + default_keys,
18
- lambda{ |json| Wordpress::Object::Comment.new(self, json) }
27
+ def update_comment(site, comment_id, data, params = {})
28
+ validate_keys! params, [:context] + default_keys
29
+ exec_api(Wordpress::Request.new(:post, "/rest/v1/sites/#{site}/comments/#{comment_id}", params, data)) do |json|
30
+ Wordpress::Object::Comment.new(self, json)
31
+ end
32
+ end
19
33
 
20
- register_api :create_comment, :post, "/rest/v1/sites/$site/comments/$site_id/replies/new",
21
- [:context] + default_keys,
22
- lambda{ |json| Wordpress::Object::Comment.new(self, json) }
34
+ def create_comment(site, post_id, data, params = {})
35
+ validate_keys! params, [:context] + default_keys
36
+ exec_api(Wordpress::Request.new(:post, "/rest/v1/sites/#{site}/posts/#{post_id}/replies/new", params, data)) do |json|
37
+ Wordpress::Object::Comment.new(self, json)
38
+ end
39
+ end
23
40
 
24
- register_api :delete_comment, :post, "/rest/v1/sites/$site/comments/$comment_id/delete",
25
- [:context] + default_keys,
26
- lambda{ |json| Wordpress::Object::Comment.new(self, json) }
41
+ def delete_comment(site, comment_id, data, params = {})
42
+ validate_keys! params, [:context] + default_keys
43
+ exec_api(Wordpress::Request.new(:post, "/rest/v1/sites/#{site}/comments/#{comment_id}/delete", params, data)) do |json|
44
+ Wordpress::Object::Comment.new(self, json)
45
+ end
46
+ end
47
+ end
48
+ end
@@ -1,19 +1,20 @@
1
1
  require "wordpress/ostruct"
2
2
 
3
- register_api :create_follow, :post, "/rest/v1/sites/$site/follows/new",
4
- [:context] + default_keys,
5
- lambda{ |json|
6
- Wordpress::OpenStruct.new(json)
7
- }
3
+ module Wordpress::API
4
+ module Follow
5
+ def create_follow(site, data, params = {})
6
+ validate_keys! params, [:context] + default_keys
7
+ exec_api(Wordpress::Request.new(:post, "/rest/v1/sites/#{site}/follows/new", params, data))
8
+ end
8
9
 
9
- register_api :delete_follow, :post, "/rest/v1/sites/$site/follows/mine/delete",
10
- [:context] + default_keys,
11
- lambda{ |json|
12
- Wordpress::OpenStruct.new(json)
13
- }
10
+ def delete_follow(site, data, params = {})
11
+ validate_keys! params, [:context] + default_keys
12
+ exec_api(Wordpress::Request.new(:post, "/rest/v1/sites/#{site}/follows/mine/delete", params, data))
13
+ end
14
14
 
15
- register_api :get_follow, :get, "/rest/v1/sites/$site/follows/mine",
16
- [:context] + default_keys,
17
- lambda{ |json|
18
- Wordpress::OpenStruct.new(json)
19
- }
15
+ def get_follow(site, params = {})
16
+ validate_keys! params, [:context] + default_keys
17
+ exec_api(Wordpress::Request.new(:get, "/rest/v1/sites/#{site}/follows/mine", params))
18
+ end
19
+ end
20
+ end
@@ -1,7 +1,10 @@
1
1
  require "wordpress/ostruct"
2
2
 
3
- register_api :freshly_pressed, :get, "/rest/v1/freshly-pressed",
4
- [:number, :after, :before, :content_width, :thumb_width, :thumb_height] + default_keys,
5
- lambda{ |json|
6
- Wordpress::OpenStruct.new(json)
7
- }
3
+ module Wordpress::API
4
+ module FreshlyPressed
5
+ def freshly_pressed(params = {})
6
+ validate_keys! params, [:number, :after, :before, :content_width, :thumb_width, :thumb_height] + default_keys
7
+ exec_api(Wordpress::Request.new(:get, "/rest/v1/freshly-pressed", params))
8
+ end
9
+ end
10
+ end
@@ -1,10 +1,18 @@
1
1
  require "wordpress/ostruct"
2
2
  require "wordpress/object/user"
3
3
 
4
- register_api :me, :get, "/rest/v1/me",
5
- default_keys,
6
- lambda{ |json| Wordpress::Object::User.new(self, json) }
4
+ module Wordpress::API
5
+ module Me
6
+ def me(params = {})
7
+ validate_keys! params, default_keys
8
+ exec_api(Wordpress::Request.new(:get, "/rest/v1/me", params)) do |json|
9
+ Wordpress::Object::User.new(self, json)
10
+ end
11
+ end
7
12
 
8
- register_api :get_my_likes, :get, "/rest/v1/me/likes",
9
- default_keys,
10
- lambda{ |json| Wordpress::OpenStruct.new(json) }
13
+ def get_my_likes(params = {})
14
+ validate_keys! params, default_keys
15
+ exec_api(Wordpress::Request.new(:get, "/rest/v1/me/likes", params))
16
+ end
17
+ end
18
+ end
@@ -1,26 +1,25 @@
1
1
  require "wordpress/ostruct"
2
2
 
3
- register_api :get_notifications, :get, "/rest/v1/notifications",
4
- [:ids, :fields, :type, :unread, :read, :number, :since, :before, :num_note_items, :trap] + default_keys,
5
- lambda{ |json|
6
- Wordpress::OpenStruct.new(json)
7
- }
3
+ module Wordpress::API
4
+ module Notifications
5
+ def get_notifications(params = {})
6
+ validate_keys! params, [:ids, :fields, :type, :unread, :read, :number, :since, :before, :num_note_items, :trap] + default_keys
7
+ exec_api(Wordpress::Request.new(:get, "/rest/v1/notifications", params))
8
+ end
8
9
 
9
- register_api :get_notification, :get, "/rest/v1/notifications/$note_id",
10
- [:context, :fields, :num_note_items] + default_keys,
11
- lambda{ |json|
12
- Wordpress::OpenStruct.new(json)
13
- }
10
+ def get_notification(note_id, params = {})
11
+ validate_keys! params, [:context, :fields, :num_note_items] + default_keys
12
+ exec_api(Wordpress::Request.new(:get, "/rest/v1/notifications/#{note_id}", params))
13
+ end
14
14
 
15
- register_api :seen_notification, :post, "/rest/v1/notifications/seen",
16
- [:context] + default_keys,
17
- lambda{ |json|
18
- Wordpress::OpenStruct.new(json)
19
- }
20
-
21
- register_api :read_notification, :post, "/rest/v1/notifications/read",
22
- [:context] + default_keys,
23
- lambda{ |json|
24
- Wordpress::OpenStruct.new(json)
25
- }
15
+ def see_notifications(data, params = {})
16
+ validate_keys! params, [:context] + default_keys
17
+ exec_api(Wordpress::Request.new(:post, "/rest/v1/notifications/seen", params, data))
18
+ end
26
19
 
20
+ def read_notifications(data, params = {})
21
+ validate_keys! params, [:context] + default_keys
22
+ exec_api(Wordpress::Request.new(:post, "/rest/v1/notifications/read", params, data))
23
+ end
24
+ end
25
+ end
@@ -2,50 +2,78 @@ require "wordpress/ostruct"
2
2
  require "wordpress/object/posts"
3
3
  require "wordpress/object/post"
4
4
 
5
- register_api :get_posts, :get, "/rest/v1/sites/$site/posts",
6
- [:context, :tag, :category, :type, :status, :sticky, :author, :search, :meta_key, :meta_value] + default_keys + search_keys,
7
- lambda{ |json| Wordpress::Object::Posts.new(self, json) }
5
+ module Wordpress::API
6
+ module Posts
7
+ def get_posts(site, params = {})
8
+ validate_keys! params, [:context, :tag, :category, :type, :status, :sticky, :author, :search, :meta_key, :meta_value] + default_keys + search_keys
9
+ exec_api(Wordpress::Request.new(:get, "/rest/v1/sites/#{site}/posts", params)) do |json|
10
+ Wordpress::Object::Posts.new(self, json)
11
+ end
12
+ end
8
13
 
9
- register_api :get_post, :get, "/rest/v1/sites/$site/posts/$post_id",
10
- [:context] + default_keys,
11
- lambda{ |json| Wordpress::Object::Post.new(self, json) }
14
+ def get_post(site, post_id, params = {})
15
+ validate_keys! params, [:context] + default_keys
16
+ exec_api(Wordpress::Request.new(:get, "/rest/v1/sites/#{site}/posts/#{post_id}", params)) do |json|
17
+ Wordpress::Object::Post.new(self, json)
18
+ end
19
+ end
12
20
 
13
- register_api :update_post, :post, "/rest/v1/sites/$site/posts/$post_id",
14
- [:context] + default_keys,
15
- lambda{ |json| Wordpress::Object::Post.new(self, json) }
21
+ def update_post(site, post_id, data, params = {})
22
+ validate_keys! params, [:context] + default_keys
23
+ exec_api(Wordpress::Request.new(:post, "/rest/v1/sites/#{site}/posts/#{post_id}", params, data)) do |json|
24
+ Wordpress::Object::Post.new(self, json)
25
+ end
26
+ end
16
27
 
17
- register_api :get_post_by_slug, :get, "/rest/v1/sites/$site/posts/slug:$post_slug",
18
- [:context] + default_keys,
19
- lambda{ |json| Wordpress::Object::Post.new(self, json) }
28
+ def get_post_by_slug(site, slug, params = {})
29
+ validate_keys! params, [:context] + default_keys
30
+ exec_api(Wordpress::Request.new(:get, "/rest/v1/sites/#{site}/posts/slug:#{slug}", params)) do |json|
31
+ Wordpress::Object::Post.new(self, json)
32
+ end
33
+ end
20
34
 
21
- register_api :create_post, :post, "/rest/v1/sites/$site/posts/new",
22
- [:context] + default_keys,
23
- lambda{ |json| Wordpress::Object::Post.new(self, json) }
35
+ def create_post(site, data, params = {})
36
+ validate_keys! params, [:context] + default_keys
37
+ exec_api(Wordpress::Request.new(:post, "/rest/v1/sites/#{site}/posts/new", params, data)) do |json|
38
+ Wordpress::Object::Post.new(self, json)
39
+ end
40
+ end
24
41
 
25
- register_api :delete_post, :post, "/rest/v1/sites/$site/posts/$post_id/delete",
26
- [:context] + default_keys,
27
- lambda{ |json| Wordpress::Object::Post.new(self, json) }
42
+ def delete_post(site, post_id, data, params = {})
43
+ validate_keys! params, [:context] + default_keys
44
+ exec_api(Wordpress::Request.new(:post, "/rest/v1/sites/#{site}/posts/#{post_id}/delete", params, data)) do |json|
45
+ Wordpress::Object::Post.new(self, json)
46
+ end
47
+ end
28
48
 
29
- register_api :get_likes, :get, "/rest/v1/sites/$site/posts/$post_id/likes",
30
- default_keys,
31
- lambda{ |json| Wordpress::OpenStruct.new(json) }
49
+ def get_likes(site, post_id, params = {})
50
+ validate_keys! params, default_keys
51
+ exec_api(Wordpress::Request.new(:get, "/rest/v1/sites/#{site}/posts/#{post_id}/likes", params))
52
+ end
32
53
 
33
- register_api :create_like, :post, "/rest/v1/sites/$site/posts/$post_id/likes/new",
34
- default_keys,
35
- lambda{ |json| Wordpress::OpenStruct.new(json) }
54
+ def create_like(site, post_id, data, params = {})
55
+ validate_keys! params, default_keys
56
+ exec_api(Wordpress::Request.new(:post, "/rest/v1/sites/#{site}/posts/#{post_id}/likes/new", params, data))
57
+ end
36
58
 
37
- register_api :delete_like, :post, "/rest/v1/sites/$site/posts/$post_id/likes/mine/delete",
38
- default_keys,
39
- lambda{ |json| Wordpress::OpenStruct.new(json) }
59
+ def delete_like(site, post_id, data, params = {})
60
+ validate_keys! params, default_keys
61
+ exec_api(Wordpress::Request.new(:post, "/rest/v1/sites/#{site}/posts/#{post_id}/likes/mine/delete", params, data))
62
+ end
40
63
 
41
- register_api :get_like_status, :get, "/rest/v1/sites/$site/posts/$post_id/likes/mine",
42
- default_keys,
43
- lambda{ |json| Wordpress::OpenStruct.new(json) }
64
+ def get_like_status(site, post_id, params = {})
65
+ validate_keys! params, default_keys
66
+ exec_api(Wordpress::Request.new(:get, "/rest/v1/sites/#{site}/posts/#{post_id}/likes/mine", params))
67
+ end
44
68
 
45
- register_api :get_reblog_status, :get, "/rest/v1/sites/$site/posts/$post_id/reblogs/mine",
46
- default_keys,
47
- lambda{ |json| Wordpress::OpenStruct.new(json) }
69
+ def get_reblog_status(site, post_id, params = {})
70
+ validate_keys! params, default_keys
71
+ exec_api(Wordpress::Request.new(:get, "/rest/v1/sites/#{site}/posts/#{post_id}/reblogs/mine", params))
72
+ end
48
73
 
49
- register_api :create_reblog, :post, "/rest/v1/sites/$site/posts/$post_id/reblogs/new",
50
- default_keys,
51
- lambda{ |json| Wordpress::OpenStruct.new(json) }
74
+ def create_reblog(site, post_id, data, params = {})
75
+ validate_keys! params, default_keys
76
+ exec_api(Wordpress::Request.new(:post, "/rest/v1/sites/#{site}/posts/#{post_id}/reblogs/new", params, data))
77
+ end
78
+ end
79
+ end
@@ -1,5 +1,12 @@
1
1
  require "wordpress/object/site"
2
2
 
3
- register_api :get_site, :get, "/rest/v1/sites/$site",
4
- default_keys,
5
- lambda{ |json| Wordpress::Object::Site.new(self, json) }
3
+ module Wordpress::API
4
+ module Sites
5
+ def get_site(site, params = {})
6
+ validate_keys! params, default_keys
7
+ exec_api(Wordpress::Request.new(:get, "/rest/v1/sites/#{site}", params)) do |json|
8
+ Wordpress::Object::Site.new(self, json)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -1,50 +1,45 @@
1
1
  require "wordpress/ostruct"
2
2
 
3
- register_api :get_category, :get, "/rest/v1/sites/$site/categories/slug:$category",
4
- [:context] + default_keys,
5
- lambda{ |json|
6
- Wordpress::OpenStruct.new(json)
7
- }
8
-
9
- register_api :update_category, :post, "/rest/v1/sites/$site/categories/slug:$category",
10
- [:context] + default_keys,
11
- lambda{ |json|
12
- Wordpress::OpenStruct.new(json)
13
- }
14
-
15
- register_api :get_tag, :get, "/rest/v1/sites/$site/tags/slug:$tag",
16
- [:context] + default_keys,
17
- lambda{ |json|
18
- Wordpress::OpenStruct.new(json)
19
- }
20
-
21
- register_api :update_tag, :post, "/rest/v1/sites/$site/tags/slug:$tag",
22
- [:context] + default_keys,
23
- lambda{ |json|
24
- Wordpress::OpenStruct.new(json)
25
- }
26
-
27
- register_api :create_category, :post, "/rest/v1/sites/$site/categories/new",
28
- [:context] + default_keys,
29
- lambda{ |json|
30
- Wordpress::OpenStruct.new(json)
31
- }
32
-
33
- register_api :create_tag, :post, "/rest/v1/sites/$site/tags/new",
34
- [:context] + default_keys,
35
- lambda{ |json|
36
- Wordpress::OpenStruct.new(json)
37
- }
38
-
39
- register_api :delete_category, :post, "/rest/v1/sites/$site/categories/slug:$category/delete",
40
- [:context] + default_keys,
41
- lambda{ |json|
42
- Wordpress::OpenStruct.new(json)
43
- }
44
-
45
- register_api :delete_tag, :post, "/rest/v1/sites/$site/tags/slug:$tag/delete",
46
- [:context] + default_keys,
47
- lambda{ |json|
48
- Wordpress::OpenStruct.new(json)
49
- }
50
-
3
+ module Wordpress::API
4
+ module Taxonomy
5
+ def get_category(site, slug, params = {})
6
+ validate_keys! params, [:context] + default_keys
7
+ exec_api(Wordpress::Request.new(:get, "/rest/v1/sites/#{site}/categories/slug:#{slug}", params))
8
+ end
9
+
10
+ def update_category(site, slug, data, params = {})
11
+ validate_keys! params, [:context] + default_keys
12
+ exec_api(Wordpress::Request.new(:post, "/rest/v1/sites/#{site}/categories/slug:#{slug}", params, data))
13
+ end
14
+
15
+ def get_tag(site, slug, params = {})
16
+ validate_keys! params, [:context] + default_keys
17
+ exec_api(Wordpress::Request.new(:get, "/rest/v1/sites/#{site}/tags/slug:#{slug}", params))
18
+ end
19
+
20
+ def update_tag(site, slug, data, params = {})
21
+ validate_keys! params, [:context] + default_keys
22
+ exec_api(Wordpress::Request.new(:post, "/rest/v1/sites/#{site}/tags/slug:#{slug}", params, data))
23
+ end
24
+
25
+ def create_category(site, data, params = {})
26
+ validate_keys! params, [:context] + default_keys
27
+ exec_api(Wordpress::Request.new(:post, "/rest/v1/sites/#{site}/categories/new", params, data))
28
+ end
29
+
30
+ def create_tag(site, data, params = {})
31
+ validate_keys! params, [:context] + default_keys
32
+ exec_api(Wordpress::Request.new(:post, "/rest/v1/sites/#{site}/tags/new", params, data))
33
+ end
34
+
35
+ def delete_category(site, slug, data, params = {})
36
+ validate_keys! params, [:context] + default_keys
37
+ exec_api(Wordpress::Request.new(:post, "/rest/v1/sites/#{site}/categories/slug:#{slug}/delete", params, data))
38
+ end
39
+
40
+ def delete_tag(site, slug, data, params = {})
41
+ validate_keys! params, [:context] + default_keys
42
+ exec_api(Wordpress::Request.new(:post, "/rest/v1/sites/#{site}/tags/slug:#{slug}/delete", params, data))
43
+ end
44
+ end
45
+ end
@@ -1,11 +1,25 @@
1
1
  require "wordpress/object/test"
2
2
 
3
- test_keys = [:id, :default_string, :default_int, :boolean_whitelist_defaults_to_false, :boolean_whitelist_defaults_to_true, :string_whitelist_defaults_to_foo, :url, :datetime]
3
+ module Wordpress::API
4
+ module Tests
5
+ def get_test(id, params = {})
6
+ validate_keys! params, test_keys
7
+ exec_api(Wordpress::Request.new(:get, "/rest/v1/test/#{id}", params)) do |json|
8
+ Wordpress::Object::Test.new(self, json)
9
+ end
10
+ end
4
11
 
5
- register_api :get_test, :get, "/rest/v1/test/$id",
6
- test_keys,
7
- lambda{ |json| Wordpress::Object::Test.new(self, json) }
12
+ def post_test(id, data, params = {})
13
+ validate_keys! params, test_keys
14
+ exec_api(Wordpress::Request.new(:post, "/rest/v1/test/#{id}", params, data)) do |json|
15
+ Wordpress::Object::Test.new(self, json)
16
+ end
17
+ end
8
18
 
9
- register_api :post_test, :post, "/rest/v1/test/$id",
10
- test_keys,
11
- lambda{ |json| Wordpress::Object::Test.new(self, json) }
19
+ private
20
+
21
+ def test_keys
22
+ [:id, :default_string, :default_int, :boolean_whitelist_defaults_to_false, :boolean_whitelist_defaults_to_true, :string_whitelist_defaults_to_foo, :url, :datetime].freeze
23
+ end
24
+ end
25
+ end
@@ -1,10 +1,11 @@
1
1
  require 'multi_json'
2
2
  require 'faraday'
3
3
  require 'faraday_middleware/gzip'
4
- require 'wordpress/base'
5
- require 'wordpress/request'
6
- require 'wordpress/api'
7
- require 'wordpress/errors'
4
+ require_relative 'base'
5
+ require_relative 'request'
6
+ require_relative 'api'
7
+ require_relative 'errors'
8
+ require_relative 'configuration'
8
9
 
9
10
  module Wordpress
10
11
  class Client < Base
@@ -1,4 +1,6 @@
1
+ require 'wordpress/ostruct'
2
+
1
3
  module Wordpress
2
- class Configuration
4
+ class Configuration < OpenStruct
3
5
  end
4
6
  end
@@ -24,8 +24,9 @@ module Wordpress::Object
24
24
  def reload
25
25
  if meta && meta.links && meta.links.self
26
26
  request = Wordpress::Request.new(:get, meta.links.self)
27
- json = client.call(request)
28
- self.assign(json)
27
+ client.exec_api(request) do |json|
28
+ self.assign(json)
29
+ end
29
30
  end
30
31
  end
31
32
 
@@ -2,7 +2,7 @@ module Wordpress
2
2
  class Version
3
3
  MAJOR = 0 unless defined? Wordpress::Version::MAJOR
4
4
  MINOR = 2 unless defined? Wordpress::Version::MINOR
5
- PATCH = 0 unless defined? Wordpress::Version::PATCH
5
+ PATCH = 1 unless defined? Wordpress::Version::PATCH
6
6
  PRE = nil unless defined? Wordpress::Version::PRE
7
7
 
8
8
  class << self
@@ -6,27 +6,64 @@ describe Wordpress::API do
6
6
  Wordpress::Client.new
7
7
  end
8
8
 
9
- # Get methods
9
+ describe "\#exec_api" do
10
+ it "should raise error for error response" do
11
+ client.stub(:call).and_return({'error' => 'something wrong!'})
12
+ expect {
13
+ client.exec_api(nil)
14
+ }.to raise_error(Wordpress::ResponseError)
15
+ end
16
+ end
17
+
10
18
  {
19
+ # Me
11
20
  'me' => {:args => [], :method => :get, :url => '/rest/v1/me', :response => Wordpress::Object::User},
12
21
  'get_my_likes' => {:args => [], :method => :get, :url => '/rest/v1/me/likes', :response => Wordpress::OpenStruct},
22
+ # Posts
13
23
  'get_posts' => {:args => ['sitename'], :method => :get, :url => '/rest/v1/sites/sitename/posts', :response => Wordpress::Object::Posts},
14
24
  'get_post' => {:args => ['sitename', 1], :method => :get, :url => '/rest/v1/sites/sitename/posts/1', :response => Wordpress::Object::Post},
25
+ 'update_post' => {:args => ['sitename', 1, {}], :method => :post, :url => '/rest/v1/sites/sitename/posts/1', :response => Wordpress::Object::Post},
15
26
  'get_post_by_slug' => {:args => ['sitename', 1], :method => :get, :url => '/rest/v1/sites/sitename/posts/slug:1', :response => Wordpress::Object::Post},
27
+ 'create_post' => {:args => ['sitename', {}], :method => :post, :url => '/rest/v1/sites/sitename/posts/new', :response => Wordpress::Object::Post},
28
+ 'delete_post' => {:args => ['sitename', 1, {}], :method => :post, :url => '/rest/v1/sites/sitename/posts/1/delete', :response => Wordpress::Object::Post},
16
29
  'get_likes' => {:args => ['sitename', 1], :method => :get, :url => '/rest/v1/sites/sitename/posts/1/likes', :response => Wordpress::OpenStruct},
30
+ 'create_like' => {:args => ['sitename', 1, {}], :method => :post, :url => '/rest/v1/sites/sitename/posts/1/likes/new', :response => Wordpress::OpenStruct},
31
+ 'delete_like' => {:args => ['sitename', 1, {}], :method => :post, :url => '/rest/v1/sites/sitename/posts/1/likes/mine/delete', :response => Wordpress::OpenStruct},
17
32
  'get_like_status' => {:args => ['sitename', 1], :method => :get, :url => '/rest/v1/sites/sitename/posts/1/likes/mine', :response => Wordpress::OpenStruct},
18
33
  'get_reblog_status' => {:args => ['sitename', 1], :method => :get, :url => '/rest/v1/sites/sitename/posts/1/reblogs/mine', :response => Wordpress::OpenStruct},
34
+ 'create_reblog' => {:args => ['sitename', 1, {}], :method => :post, :url => '/rest/v1/sites/sitename/posts/1/reblogs/new', :response => Wordpress::OpenStruct},
35
+ # Sites
19
36
  'get_site' => {:args => ['sitename'], :method => :get, :url => '/rest/v1/sites/sitename', :response => Wordpress::Object::Site},
37
+ # Comments
20
38
  'get_comments' => {:args => ['sitename'], :method => :get, :url => '/rest/v1/sites/sitename/comments', :response => Wordpress::Object::Comments},
21
39
  'get_post_comments' => {:args => ['sitename', 1], :method => :get, :url => '/rest/v1/sites/sitename/posts/1/replies', :response => Wordpress::Object::Comments},
22
40
  'get_comment' => {:args => ['sitename', 1], :method => :get, :url => '/rest/v1/sites/sitename/comments/1', :response => Wordpress::Object::Comment},
41
+ 'update_comment' => {:args => ['sitename', 1, {}], :method => :post, :url => '/rest/v1/sites/sitename/comments/1', :response => Wordpress::Object::Comment},
42
+ 'create_comment' => {:args => ['sitename', 1, {}], :method => :post, :url => '/rest/v1/sites/sitename/posts/1/replies/new', :response => Wordpress::Object::Comment},
43
+ 'delete_comment' => {:args => ['sitename', 1, {}], :method => :post, :url => '/rest/v1/sites/sitename/comments/1/delete', :response => Wordpress::Object::Comment},
44
+ # Tests
23
45
  'get_test' => {:args => [1], :method => :get, :url => '/rest/v1/test/1', :response => Wordpress::Object::Test},
46
+ 'post_test' => {:args => [1, {}], :method => :post, :url => '/rest/v1/test/1', :response => Wordpress::Object::Test},
47
+ # Taxonomy
24
48
  'get_category' => {:args => ['sitename', 'cat_name'], :method => :get, :url => '/rest/v1/sites/sitename/categories/slug:cat_name', :response => Wordpress::OpenStruct},
49
+ 'update_category' => {:args => ['sitename', 'cat_name', {}], :method => :post, :url => '/rest/v1/sites/sitename/categories/slug:cat_name', :response => Wordpress::OpenStruct},
25
50
  'get_tag' => {:args => ['sitename', 'tag_name'], :method => :get, :url => '/rest/v1/sites/sitename/tags/slug:tag_name', :response => Wordpress::OpenStruct},
51
+ 'update_tag' => {:args => ['sitename', 'tag_name', {}], :method => :post, :url => '/rest/v1/sites/sitename/tags/slug:tag_name', :response => Wordpress::OpenStruct},
52
+ 'create_category' => {:args => ['sitename', {}], :method => :post, :url => '/rest/v1/sites/sitename/categories/new', :response => Wordpress::OpenStruct},
53
+ 'create_tag' => {:args => ['sitename', {}], :method => :post, :url => '/rest/v1/sites/sitename/tags/new', :response => Wordpress::OpenStruct},
54
+ 'delete_category' => {:args => ['sitename', 'cat_name', {}], :method => :post, :url => '/rest/v1/sites/sitename/categories/slug:cat_name/delete', :response => Wordpress::OpenStruct},
55
+ 'delete_tag' => {:args => ['sitename', 'tag_name', {}], :method => :post, :url => '/rest/v1/sites/sitename/tags/slug:tag_name/delete', :response => Wordpress::OpenStruct},
56
+ # Follow
57
+ 'create_follow' => {:args => ['sitename', {}], :method => :post, :url => '/rest/v1/sites/sitename/follows/new', :response => Wordpress::OpenStruct},
58
+ 'delete_follow' => {:args => ['sitename', {}], :method => :post, :url => '/rest/v1/sites/sitename/follows/mine/delete', :response => Wordpress::OpenStruct},
26
59
  'get_follow' => {:args => ['sitename'], :method => :get, :url => '/rest/v1/sites/sitename/follows/mine', :response => Wordpress::OpenStruct},
60
+ # FreshlyPressed
27
61
  'freshly_pressed' => {:method => :get, :url => '/rest/v1/freshly-pressed', :response => Wordpress::OpenStruct},
62
+ # Notifications
28
63
  'get_notifications' => {:method => :get, :url => '/rest/v1/notifications', :response => Wordpress::OpenStruct},
29
64
  'get_notification' => {:args => [1], :method => :get, :url => '/rest/v1/notifications/1', :response => Wordpress::OpenStruct},
65
+ 'see_notifications' => {:args => [{}], :method => :post, :url => '/rest/v1/notifications/seen', :response => Wordpress::OpenStruct},
66
+ 'read_notifications' => {:args => [{}], :method => :post, :url => '/rest/v1/notifications/read', :response => Wordpress::OpenStruct},
30
67
  }.each do |method, params|
31
68
  describe "\##{method}" do
32
69
  it "should return #{params[:response].name}" do
@@ -35,6 +35,26 @@ describe Wordpress::Client do
35
35
  request = Wordpress::Request.new(:post, 'url', {}, {})
36
36
  client.call(request)
37
37
  end
38
+
39
+ it "should raise error for null response" do
40
+ request = Wordpress::Request.new(:get, 'url', {}, {})
41
+ Faraday::Connection.any_instance.stub(:get).and_return(Faraday::Response.new)
42
+
43
+ Faraday::Response.any_instance.stub(:body).and_return('null')
44
+ expect {
45
+ client.call(request)
46
+ }.to raise_error(Wordpress::ResponseError)
47
+ end
48
+
49
+ it "should raise error for invalid response" do
50
+ request = Wordpress::Request.new(:get, 'url', {}, {})
51
+ Faraday::Connection.any_instance.stub(:get).and_return(Faraday::Response.new)
52
+
53
+ Faraday::Response.any_instance.stub(:body).and_return('/$%@#')
54
+ expect {
55
+ client.call(request)
56
+ }.to raise_error(Wordpress::ResponseError)
57
+ end
38
58
  end
39
59
 
40
60
  context "with bearer_token_request option" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wordpress-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-12-27 00:00:00.000000000 Z
12
+ date: 2013-12-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday