wordpress_client 0.0.1 → 2.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.circleci/config.yml +30 -0
- data/.codeclimate.yml +23 -0
- data/.gitignore +1 -0
- data/.rubocop.yml +147 -90
- data/.yardopts +1 -0
- data/Changelog.md +16 -0
- data/Gemfile +6 -1
- data/README.md +48 -14
- data/Rakefile +37 -0
- data/lib/wordpress_client/category.rb +2 -0
- data/lib/wordpress_client/client.rb +235 -79
- data/lib/wordpress_client/connection.rb +10 -10
- data/lib/wordpress_client/errors.rb +41 -6
- data/lib/wordpress_client/media.rb +49 -1
- data/lib/wordpress_client/media_parser.rb +3 -0
- data/lib/wordpress_client/paginated_collection.rb +61 -4
- data/lib/wordpress_client/post.rb +63 -16
- data/lib/wordpress_client/post_parser.rb +12 -39
- data/lib/wordpress_client/rest_parser.rb +4 -0
- data/lib/wordpress_client/tag.rb +2 -0
- data/lib/wordpress_client/term.rb +30 -0
- data/lib/wordpress_client/version.rb +5 -1
- data/lib/wordpress_client.rb +21 -5
- data/spec/client_spec.rb +17 -181
- data/spec/connection_spec.rb +15 -14
- data/spec/docker/Dockerfile +35 -10
- data/spec/docker/README.md +53 -16
- data/spec/docker/dbdump.sql.gz +0 -0
- data/spec/docker/restore-dbdump.sh +2 -2
- data/spec/docker/yum.repos.d/CentOS-Base.repo +25 -0
- data/spec/fixtures/image-media.json +1 -1
- data/spec/fixtures/post-with-metadata.json +99 -1
- data/spec/fixtures/simple-post.json +324 -1
- data/spec/integration/attachments_crud_spec.rb +1 -1
- data/spec/integration/posts_crud_spec.rb +1 -1
- data/spec/integration/posts_finding_spec.rb +0 -69
- data/spec/integration/posts_metadata_spec.rb +11 -11
- data/spec/integration/posts_with_attachments_spec.rb +20 -6
- data/spec/media_spec.rb +14 -0
- data/spec/post_spec.rb +5 -31
- data/spec/spec_helper.rb +1 -0
- data/spec/support/docker_runner.rb +33 -13
- data/spec/support/wordpress_server.rb +112 -74
- data/wordpress_client.gemspec +17 -17
- metadata +43 -31
- data/.hound.yml +0 -2
- data/.ruby-version +0 -1
- data/circle.yml +0 -3
- data/lib/wordpress_client/replace_metadata.rb +0 -81
- data/lib/wordpress_client/replace_terms.rb +0 -62
- data/spec/fixtures/post-with-forbidden-metadata.json +0 -1
- data/spec/integration/category_assignment_spec.rb +0 -29
- data/spec/integration/tag_assignment_spec.rb +0 -29
- data/spec/replace_metadata_spec.rb +0 -56
- data/spec/replace_terms_spec.rb +0 -51
@@ -1,81 +0,0 @@
|
|
1
|
-
require "set"
|
2
|
-
|
3
|
-
module WordpressClient
|
4
|
-
class ReplaceMetadata
|
5
|
-
def self.apply(connection, post, meta)
|
6
|
-
instance = new(connection, post, meta)
|
7
|
-
instance.apply
|
8
|
-
instance.number_of_changes
|
9
|
-
end
|
10
|
-
|
11
|
-
attr_reader :number_of_changes
|
12
|
-
|
13
|
-
def initialize(connection, post, meta)
|
14
|
-
@connection = connection
|
15
|
-
@post = post
|
16
|
-
@existing_meta = post.meta
|
17
|
-
@new_meta = stringify_keys(meta)
|
18
|
-
@number_of_changes = 0
|
19
|
-
end
|
20
|
-
|
21
|
-
def apply
|
22
|
-
all_keys.each do |key|
|
23
|
-
action = determine_action(key)
|
24
|
-
send(action, key, new_meta[key])
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
private
|
29
|
-
attr_reader :connection, :post, :new_meta, :existing_meta
|
30
|
-
|
31
|
-
def meta_id(key)
|
32
|
-
post.meta_id_for(key)
|
33
|
-
end
|
34
|
-
|
35
|
-
def all_keys
|
36
|
-
(new_meta.keys + existing_meta.keys).to_set
|
37
|
-
end
|
38
|
-
|
39
|
-
def determine_action(key)
|
40
|
-
old_value = existing_meta[key]
|
41
|
-
new_value = new_meta[key]
|
42
|
-
|
43
|
-
if old_value.nil? && !new_value.nil?
|
44
|
-
:add
|
45
|
-
elsif old_value == new_value
|
46
|
-
:keep
|
47
|
-
elsif new_value.nil?
|
48
|
-
:remove
|
49
|
-
else
|
50
|
-
:replace
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
def add(key, value)
|
55
|
-
connection.create_without_response("posts/#{post.id}/meta", key: key, value: value)
|
56
|
-
@number_of_changes += 1
|
57
|
-
end
|
58
|
-
|
59
|
-
def remove(key, *)
|
60
|
-
connection.delete("posts/#{post.id}/meta/#{meta_id(key)}", force: true)
|
61
|
-
@number_of_changes += 1
|
62
|
-
end
|
63
|
-
|
64
|
-
def replace(key, value)
|
65
|
-
connection.patch_without_response(
|
66
|
-
"posts/#{post.id}/meta/#{meta_id(key)}", key: key, value: value
|
67
|
-
)
|
68
|
-
@number_of_changes += 1
|
69
|
-
end
|
70
|
-
|
71
|
-
def keep(*)
|
72
|
-
# Do nothing. This method is here to satisfy every action of #determine_action.
|
73
|
-
end
|
74
|
-
|
75
|
-
def stringify_keys(hash)
|
76
|
-
hash.each_with_object({}) do |(key, value), new_hash|
|
77
|
-
new_hash[key.to_s] = value
|
78
|
-
end
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
@@ -1,62 +0,0 @@
|
|
1
|
-
require "set"
|
2
|
-
|
3
|
-
module WordpressClient
|
4
|
-
class ReplaceTerms
|
5
|
-
def self.apply_categories(connection, post, category_ids)
|
6
|
-
instance = new(
|
7
|
-
connection,
|
8
|
-
post.id,
|
9
|
-
post.category_ids,
|
10
|
-
category_ids
|
11
|
-
)
|
12
|
-
instance.replace("category")
|
13
|
-
instance.number_of_changes
|
14
|
-
end
|
15
|
-
|
16
|
-
def self.apply_tags(connection, post, tag_ids)
|
17
|
-
instance = new(
|
18
|
-
connection,
|
19
|
-
post.id,
|
20
|
-
post.tag_ids,
|
21
|
-
tag_ids
|
22
|
-
)
|
23
|
-
instance.replace("tag")
|
24
|
-
instance.number_of_changes
|
25
|
-
end
|
26
|
-
|
27
|
-
def initialize(connection, post_id, existing_ids, new_ids)
|
28
|
-
@connection = connection
|
29
|
-
@post_id = post_id
|
30
|
-
@existing_ids = existing_ids.to_set
|
31
|
-
@wanted_ids = new_ids.to_set
|
32
|
-
end
|
33
|
-
|
34
|
-
def replace(type)
|
35
|
-
ids_to_add.each { |id| add_term_id(id, type) }
|
36
|
-
ids_to_remove.each { |id| remove_term_id(id, type) }
|
37
|
-
end
|
38
|
-
|
39
|
-
def number_of_changes
|
40
|
-
ids_to_add.size + ids_to_remove.size
|
41
|
-
end
|
42
|
-
|
43
|
-
private
|
44
|
-
attr_reader :connection, :post_id, :wanted_ids, :existing_ids
|
45
|
-
|
46
|
-
def ids_to_add
|
47
|
-
wanted_ids - existing_ids
|
48
|
-
end
|
49
|
-
|
50
|
-
def ids_to_remove
|
51
|
-
existing_ids - wanted_ids
|
52
|
-
end
|
53
|
-
|
54
|
-
def add_term_id(id, type)
|
55
|
-
connection.create_without_response("posts/#{post_id}/terms/#{type}/#{id}", {})
|
56
|
-
end
|
57
|
-
|
58
|
-
def remove_term_id(id, type)
|
59
|
-
connection.delete("posts/#{post_id}/terms/#{type}/#{id}", force: true)
|
60
|
-
end
|
61
|
-
end
|
62
|
-
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"}],"https:\/\/api.w.org\/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'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}}]],"https:\/\/api.w.org\/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
|
data/spec/replace_terms_spec.rb
DELETED
@@ -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
|