wordpress_client 1.0.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +124 -69
  3. data/Changelog.md +4 -0
  4. data/README.md +36 -13
  5. data/lib/wordpress_client.rb +0 -3
  6. data/lib/wordpress_client/client.rb +20 -74
  7. data/lib/wordpress_client/connection.rb +9 -10
  8. data/lib/wordpress_client/media.rb +15 -1
  9. data/lib/wordpress_client/media_parser.rb +1 -0
  10. data/lib/wordpress_client/post.rb +16 -36
  11. data/lib/wordpress_client/post_parser.rb +9 -37
  12. data/lib/wordpress_client/term.rb +1 -1
  13. data/lib/wordpress_client/version.rb +1 -1
  14. data/spec/client_spec.rb +17 -181
  15. data/spec/connection_spec.rb +8 -11
  16. data/spec/docker/Dockerfile +8 -8
  17. data/spec/docker/README.md +19 -9
  18. data/spec/docker/dbdump.sql.gz +0 -0
  19. data/spec/docker/restore-dbdump.sh +2 -2
  20. data/spec/fixtures/post-with-metadata.json +99 -1
  21. data/spec/fixtures/simple-post.json +324 -1
  22. data/spec/integration/attachments_crud_spec.rb +1 -1
  23. data/spec/integration/posts_crud_spec.rb +1 -1
  24. data/spec/integration/posts_finding_spec.rb +0 -69
  25. data/spec/integration/posts_metadata_spec.rb +11 -11
  26. data/spec/integration/posts_with_attachments_spec.rb +20 -6
  27. data/spec/media_spec.rb +13 -0
  28. data/spec/post_spec.rb +5 -31
  29. data/spec/support/docker_runner.rb +25 -10
  30. data/spec/support/wordpress_server.rb +15 -7
  31. data/wordpress_client.gemspec +11 -12
  32. metadata +5 -15
  33. data/lib/wordpress_client/replace_metadata.rb +0 -82
  34. data/lib/wordpress_client/replace_terms.rb +0 -63
  35. data/spec/fixtures/post-with-forbidden-metadata.json +0 -1
  36. data/spec/integration/category_assignment_spec.rb +0 -29
  37. data/spec/integration/tag_assignment_spec.rb +0 -29
  38. data/spec/replace_metadata_spec.rb +0 -56
  39. data/spec/replace_terms_spec.rb +0 -51
@@ -1,82 +0,0 @@
1
- require "set"
2
-
3
- module WordpressClient
4
- # @private
5
- class ReplaceMetadata
6
- def self.apply(connection, post, meta)
7
- instance = new(connection, post, meta)
8
- instance.apply
9
- instance.number_of_changes
10
- end
11
-
12
- attr_reader :number_of_changes
13
-
14
- def initialize(connection, post, meta)
15
- @connection = connection
16
- @post = post
17
- @existing_meta = post.meta
18
- @new_meta = stringify_keys(meta)
19
- @number_of_changes = 0
20
- end
21
-
22
- def apply
23
- all_keys.each do |key|
24
- action = determine_action(key)
25
- send(action, key, new_meta[key])
26
- end
27
- end
28
-
29
- private
30
- attr_reader :connection, :post, :new_meta, :existing_meta
31
-
32
- def meta_id(key)
33
- post.meta_id_for(key)
34
- end
35
-
36
- def all_keys
37
- (new_meta.keys + existing_meta.keys).to_set
38
- end
39
-
40
- def determine_action(key)
41
- old_value = existing_meta[key]
42
- new_value = new_meta[key]
43
-
44
- if old_value.nil? && !new_value.nil?
45
- :add
46
- elsif old_value == new_value
47
- :keep
48
- elsif new_value.nil?
49
- :remove
50
- else
51
- :replace
52
- end
53
- end
54
-
55
- def add(key, value)
56
- connection.create_without_response("posts/#{post.id}/meta", key: key, value: value)
57
- @number_of_changes += 1
58
- end
59
-
60
- def remove(key, *)
61
- connection.delete("posts/#{post.id}/meta/#{meta_id(key)}", force: true)
62
- @number_of_changes += 1
63
- end
64
-
65
- def replace(key, value)
66
- connection.patch_without_response(
67
- "posts/#{post.id}/meta/#{meta_id(key)}", key: key, value: value
68
- )
69
- @number_of_changes += 1
70
- end
71
-
72
- def keep(*)
73
- # Do nothing. This method is here to satisfy every action of #determine_action.
74
- end
75
-
76
- def stringify_keys(hash)
77
- hash.each_with_object({}) do |(key, value), new_hash|
78
- new_hash[key.to_s] = value
79
- end
80
- end
81
- end
82
- end
@@ -1,63 +0,0 @@
1
- require "set"
2
-
3
- module WordpressClient
4
- # @private
5
- class ReplaceTerms
6
- def self.apply_categories(connection, post, category_ids)
7
- instance = new(
8
- connection,
9
- post.id,
10
- post.category_ids,
11
- category_ids
12
- )
13
- instance.replace("category")
14
- instance.number_of_changes
15
- end
16
-
17
- def self.apply_tags(connection, post, tag_ids)
18
- instance = new(
19
- connection,
20
- post.id,
21
- post.tag_ids,
22
- tag_ids
23
- )
24
- instance.replace("tag")
25
- instance.number_of_changes
26
- end
27
-
28
- def initialize(connection, post_id, existing_ids, new_ids)
29
- @connection = connection
30
- @post_id = post_id
31
- @existing_ids = existing_ids.to_set
32
- @wanted_ids = new_ids.to_set
33
- end
34
-
35
- def replace(type)
36
- ids_to_add.each { |id| add_term_id(id, type) }
37
- ids_to_remove.each { |id| remove_term_id(id, type) }
38
- end
39
-
40
- def number_of_changes
41
- ids_to_add.size + ids_to_remove.size
42
- end
43
-
44
- private
45
- attr_reader :connection, :post_id, :wanted_ids, :existing_ids
46
-
47
- def ids_to_add
48
- wanted_ids - existing_ids
49
- end
50
-
51
- def ids_to_remove
52
- existing_ids - wanted_ids
53
- end
54
-
55
- def add_term_id(id, type)
56
- connection.create_without_response("posts/#{post_id}/terms/#{type}/#{id}", {})
57
- end
58
-
59
- def remove_term_id(id, type)
60
- connection.delete("posts/#{post_id}/terms/#{type}/#{id}", force: true)
61
- end
62
- end
63
- end
@@ -1 +0,0 @@
1
- {"id":4,"date":null,"guid":{"rendered":"http:\/\/192.168.99.100:8181\/?p=4"},"modified":null,"modified_gmt":null,"slug":"","type":"post","link":"http:\/\/192.168.99.100:8181\/?p=4","title":{"rendered":"Metadata creation"},"content":{"rendered":""},"excerpt":{"rendered":""},"author":1,"featured_image":0,"comment_status":"open","ping_status":"open","sticky":false,"format":"standard","_links":{"self":[{"href":"http:\/\/192.168.99.100:8181\/wp-json\/wp\/v2\/posts\/4"}],"collection":[{"href":"http:\/\/192.168.99.100:8181\/wp-json\/wp\/v2\/posts"}],"author":[{"embeddable":true,"href":"http:\/\/192.168.99.100:8181\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/192.168.99.100:8181\/wp-json\/wp\/v2\/comments?post_id=4"}],"version-history":[{"href":"http:\/\/192.168.99.100:8181\/wp-json\/wp\/v2\/posts\/4\/revisions"}],"https:\/\/api.w.org\/attachment":[{"embeddable":true,"href":"http:\/\/192.168.99.100:8181\/wp-json\/wp\/v2\/media?post_parent=4"}],"https:\/\/api.w.org\/term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/192.168.99.100:8181\/wp-json\/wp\/v2\/posts\/4\/terms\/category"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/192.168.99.100:8181\/wp-json\/wp\/v2\/posts\/4\/terms\/tag"},{"taxonomy":"post_format","embeddable":true,"href":"http:\/\/192.168.99.100:8181\/wp-json\/wp\/v2\/posts\/4\/terms\/post_format"}],"wp:meta":[{"embeddable":true,"href":"http:\/\/192.168.99.100:8181\/wp-json\/wp\/v2\/posts\/4\/meta"}]},"_embedded":{"author":[{"avatar_urls":{"24":"http:\/\/2.gravatar.com\/avatar\/55502f40dc8b7c769880b10874abc9d0?s=24&d=mm&r=g","48":"http:\/\/2.gravatar.com\/avatar\/55502f40dc8b7c769880b10874abc9d0?s=48&d=mm&r=g","96":"http:\/\/2.gravatar.com\/avatar\/55502f40dc8b7c769880b10874abc9d0?s=96&d=mm&r=g"},"description":"","id":1,"link":"http:\/\/192.168.99.100:8181\/author\/test\/","name":"test","url":"","_links":{"self":[{"href":"http:\/\/192.168.99.100:8181\/wp-json\/wp\/v2\/users\/1"}],"collection":[{"href":"http:\/\/192.168.99.100:8181\/wp-json\/wp\/v2\/users"}]}}],"replies":[[{"id":1,"parent":0,"author":0,"author_name":"Mr WordPress","author_url":"https:\/\/wordpress.org\/","author_avatar_urls":{"24":"http:\/\/1.gravatar.com\/avatar\/?s=24&d=mm&r=g","48":"http:\/\/1.gravatar.com\/avatar\/?s=48&d=mm&r=g","96":"http:\/\/2.gravatar.com\/avatar\/?s=96&d=mm&r=g"},"date":"2015-11-03T07:47:41","content":{"rendered":"<p>Hi, this is a comment.<br \/>\nTo delete a comment, just log in and view the post&#039;s comments. There you will have the option to edit or delete them.<\/p>\n"},"link":"http:\/\/192.168.99.100:8181\/2015\/11\/03\/hello-world\/#comment-1","type":"comment","_links":{"self":[{"href":"http:\/\/192.168.99.100:8181\/wp-json\/wp\/v2\/comments\/1"}],"collection":[{"href":"http:\/\/192.168.99.100:8181\/wp-json\/wp\/v2\/comments"}],"up":[{"embeddable":true,"post_type":"post","href":"http:\/\/192.168.99.100:8181\/wp-json\/wp\/v2\/posts\/1"}]}}]],"https:\/\/api.w.org\/term":[[{"id":1,"link":"http:\/\/192.168.99.100:8181\/category\/uncategorized\/","name":"Uncategorized","slug":"uncategorized","taxonomy":"category","_links":{"self":[{"href":"http:\/\/192.168.99.100:8181\/wp-json\/wp\/v2\/terms\/category\/1"}],"collection":[{"href":"http:\/\/192.168.99.100:8181\/wp-json\/wp\/v2\/terms\/category"}]}}],[],[{"code":"rest_no_route","message":"No route was found matching the URL and request method","data":{"status":404}}]],"wp:meta":[[{"code":"rest_forbidden","message":"Sorry, you cannot view the meta for this post.","data":{"status":403}}]]}}
@@ -1,29 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe "Category assignment" do
4
- setup_integration_client
5
- let(:existing_post) { find_existing_post }
6
-
7
- it "can be set for multiple categories when creating a post" do
8
- category = client.create_category(name: "Assignment test 1")
9
- post = client.create_post(category_ids: [category.id], title: "Assignment test")
10
-
11
- expect(post.categories).to eq [category]
12
- end
13
-
14
- it "can be changed when updating a post" do
15
- category = client.create_category(name: "Assignment test 2")
16
- post = find_existing_post
17
-
18
- client.update_post(post.id, category_ids: [category.id])
19
-
20
- post = client.find_post(post.id)
21
- expect(post.categories).to eq [category]
22
- end
23
-
24
- def find_existing_post
25
- posts = client.posts(per_page: 1)
26
- expect(posts).to_not be_empty
27
- posts.first
28
- end
29
- end
@@ -1,29 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe "Tag assignment" do
4
- setup_integration_client
5
- let(:existing_post) { find_existing_post }
6
-
7
- it "can be set for multiple tags when creating a post" do
8
- tag = client.create_tag(name: "Assignment test 1")
9
- post = client.create_post(tag_ids: [tag.id], title: "Assignment test")
10
-
11
- expect(post.tags).to eq [tag]
12
- end
13
-
14
- it "can be changed when updating a post" do
15
- tag = client.create_tag(name: "Assignment test 2")
16
- post = find_existing_post
17
-
18
- client.update_post(post.id, tag_ids: [tag.id])
19
-
20
- post = client.find_post(post.id)
21
- expect(post.tags).to eq [tag]
22
- end
23
-
24
- def find_existing_post
25
- posts = client.posts(per_page: 1)
26
- expect(posts).to_not be_empty
27
- posts.first
28
- end
29
- end
@@ -1,56 +0,0 @@
1
- require "spec_helper"
2
-
3
- module WordpressClient
4
- describe ReplaceMetadata do
5
- it "does nothing if the new metadata is equal to the existing one" do
6
- post = instance_double(Post, id: 5, meta: {"existing" => "1"})
7
-
8
- # Note: connection double does not accept any message.
9
- connection = instance_double(Connection)
10
-
11
- ReplaceMetadata.apply(connection, post, existing: "1")
12
- end
13
-
14
- it "adds missing metadata" do
15
- connection = instance_double(Connection)
16
- post = instance_double(Post, id: 5, meta: {"existing" => "1"})
17
-
18
- expect(connection).to receive(:create_without_response).with(
19
- "posts/5/meta", key: "new", value: "2"
20
- )
21
-
22
- ReplaceMetadata.apply(connection, post, existing: "1", new: "2")
23
- end
24
-
25
- it "replaces changed metadata" do
26
- connection = instance_double(Connection)
27
- post = instance_double(Post, id: 5, meta: {"change_me" => "1"})
28
-
29
- expect(post).to receive(:meta_id_for).with("change_me").and_return(13)
30
-
31
- expect(connection).to receive(:patch_without_response).with(
32
- "posts/5/meta/13", key: "change_me", value: "2"
33
- )
34
-
35
- ReplaceMetadata.apply(connection, post, change_me: "2")
36
- end
37
-
38
- it "removes extra metadata" do
39
- connection = instance_double(Connection)
40
- post = instance_double(Post, id: 5, meta: {"old" => "1", "new" => "2"})
41
-
42
- expect(post).to receive(:meta_id_for).with("old").and_return(45)
43
- expect(connection).to receive(:delete).with("posts/5/meta/45", force: true)
44
-
45
- ReplaceMetadata.apply(connection, post, new: "2")
46
- end
47
-
48
- it "returns the number of changes" do
49
- connection = instance_double(Connection).as_null_object
50
- post = instance_double(Post, id: 5, meta: {"old" => "1", "change" => "2"}).as_null_object
51
-
52
- result = ReplaceMetadata.apply(connection, post, change: "3", extra: "4")
53
- expect(result).to eq 3
54
- end
55
- end
56
- end
@@ -1,51 +0,0 @@
1
- require "spec_helper"
2
-
3
- module WordpressClient
4
- describe ReplaceTerms do
5
- it "adds missing categories" do
6
- connection = double(Connection)
7
- post = double(Post, id: 40, category_ids: [1])
8
-
9
- expect(connection).to receive(:create_without_response).with("posts/40/terms/category/5", {})
10
-
11
- ReplaceTerms.apply_categories(connection, post, [1, 5])
12
- end
13
-
14
- it "removes extra categories" do
15
- connection = double(Connection)
16
- post = double(Post, id: 40, category_ids: [8, 9, 10])
17
-
18
- expect(connection).to receive(:delete).with("posts/40/terms/category/8", force: true)
19
- expect(connection).to receive(:delete).with("posts/40/terms/category/9", force: true)
20
-
21
- ReplaceTerms.apply_categories(connection, post, [10])
22
- end
23
-
24
- it "adds missing tags" do
25
- connection = double(Connection)
26
- post = double(Post, id: 40, tag_ids: [1])
27
-
28
- expect(connection).to receive(:create_without_response).with("posts/40/terms/tag/5", {})
29
-
30
- ReplaceTerms.apply_tags(connection, post, [1, 5])
31
- end
32
-
33
- it "removes extra tags" do
34
- connection = double(Connection)
35
- post = double(Post, id: 40, tag_ids: [8, 9, 10])
36
-
37
- expect(connection).to receive(:delete).with("posts/40/terms/tag/8", force: true)
38
- expect(connection).to receive(:delete).with("posts/40/terms/tag/9", force: true)
39
-
40
- ReplaceTerms.apply_tags(connection, post, [10])
41
- end
42
-
43
- it "returns the amount of changes made" do
44
- connection = double(Connection).as_null_object
45
- post = double(Post, id: 40, tag_ids: [8, 9, 10])
46
-
47
- result = ReplaceTerms.apply_tags(connection, post, [10, 11])
48
- expect(result).to eq 3
49
- end
50
- end
51
- end