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
@@ -40,7 +40,7 @@ describe "Attachments" do
40
40
  it "uses HTML for the title" do
41
41
  media = find_or_create_attachment
42
42
  updated = client.update_media(media.id, title: "Images & paint")
43
- expect(updated.title_html).to eq "Images & paint"
43
+ expect(updated.title_html).to eq "Images & paint"
44
44
  end
45
45
 
46
46
  def find_or_create_attachment
@@ -71,7 +71,7 @@ describe "Posts (CRUD)" do
71
71
  )
72
72
 
73
73
  expect(post.content_html.strip).to eq '<p class="hello-world">Hello world</p>'
74
- expect(post.title_html.strip).to eq 'HTML test &amp; verify'
74
+ expect(post.title_html.strip).to eq 'HTML test &#038; verify'
75
75
  end
76
76
 
77
77
  it "can move a post to the trash can" do
@@ -14,73 +14,4 @@ describe "Posts (finding)" do
14
14
  expect(posts.map(&:id)).to eq posts.sort_by(&:date).reverse.map(&:id)
15
15
  end
16
16
 
17
- it "can list articles in a specific category" do
18
- category = client.create_category(name: "Filtering time", slug: "filtering")
19
- post = client.create_post(
20
- category_ids: [category.id],
21
- status: "publish",
22
- title: "Some title",
23
- )
24
-
25
- expect(client.posts(category_slug: "filtering").map(&:id)).to eq [post.id]
26
- end
27
-
28
- it "can list articles in a specific tag" do
29
- tag = client.create_tag(name: "Taggged", slug: "tagged")
30
- post = client.create_post(
31
- tag_ids: [tag.id],
32
- status: "publish",
33
- title: "Some title",
34
- )
35
-
36
- expect(client.posts(tag_slug: "tagged").map(&:id)).to eq [post.id]
37
- end
38
-
39
- it "can list articles in a specific tag and category" do
40
- category = client.create_category(name: "Fishing", slug: "fish")
41
- tag = client.create_tag(name: "Soup", slug: "soup")
42
- post = client.create_post(
43
- category_ids: [category.id],
44
- tag_ids: [tag.id],
45
- status: "publish",
46
- title: "Some title",
47
- )
48
-
49
- _post_without_tag = client.create_post(
50
- category_ids: [category.id],
51
- status: "publish",
52
- title: "Some title",
53
- )
54
-
55
- _post_without_category = client.create_post(
56
- tag_ids: [tag.id],
57
- status: "publish",
58
- title: "Some title",
59
- )
60
-
61
- expect(client.posts(category_slug: "fish", tag_slug: "soup").map(&:id)).to eq [post.id]
62
- end
63
-
64
- describe "finding by slug" do
65
- it "finds the matching post" do
66
- post = client.create_post(title: "Oh hai", slug: "oh-hai")
67
- found = client.find_post_by_slug("oh-hai")
68
- expect(found.id).to eq post.id
69
- end
70
-
71
- it "raises NotFoundError when no post can be found" do
72
- expect {
73
- client.find_post_by_slug("clearly-does-not-exist-anywhere")
74
- }.to raise_error(WordpressClient::NotFoundError, /clearly/)
75
- end
76
-
77
- it "finds on the slug even if the title is wildly different" do
78
- post = client.create_post(
79
- title: "Updated title that doesn't match slug",
80
- slug: "original-concise-title",
81
- )
82
- found = client.find_post_by_slug("original-concise-title")
83
- expect(found.id).to eq post.id
84
- end
85
- end
86
17
  end
@@ -4,24 +4,24 @@ describe "Post meta" do
4
4
  setup_integration_client
5
5
 
6
6
  it "can be set on a post" do
7
- post = client.create_post(title: "Metadata creation", meta: {foo: "bar"})
8
- expect(post.meta).to eq("foo" => "bar")
7
+ post = client.create_post(title: "Metadata creation", meta: {"foo" => 7})
8
+ expect(post.meta).to include("foo" => 7)
9
9
  end
10
10
 
11
11
  it "can be updated on a post" do
12
- post = client.create_post(title: "Metadata creation", meta: {before: "now"})
13
- post = client.update_post(post.id, meta: {"before" => "then", "after" => "now"})
14
- expect(post.meta).to eq("before" => "then", "after" => "now")
12
+ post = client.create_post(title: "Metadata creation", meta: {"foo" => 1})
13
+ post = client.update_post(post.id, meta: {"foo" => 2, "bar" => true})
14
+ expect(post.meta).to include("foo" => 2, "bar" => true)
15
15
  end
16
16
 
17
17
  it "can remove metadata for a post" do
18
- post = client.create_post(title: "Metadata creation", meta: {one: "1", two: "2", three: "3"})
19
- post = client.update_post(post.id, meta: {three: "3", two: nil})
20
- expect(post.meta).to eq("three" => "3")
18
+ post = client.create_post(title: "Metadata creation", meta: {foo: 1, bar: true, baz: "foobar"})
19
+ post = client.update_post(post.id, meta: {foo: 5, baz: nil})
20
+ expect(post.meta).to eq("foo" => 5, "bar" => true, "baz" => "")
21
21
  end
22
22
 
23
- it "does not contain HTML data" do
24
- post = client.create_post(title: "Metadata HTML", meta: {foo: "bar&baz"})
25
- expect(post.meta).to eq("foo" => "bar&baz")
23
+ it "returns unescaped HTML" do
24
+ post = client.create_post(title: "Metadata HTML", meta: {baz: "larry&curly<moe>"})
25
+ expect(post.meta).to eq("baz" => "larry&curly<moe>", "foo" => 0, "bar" => false)
26
26
  end
27
27
  end
@@ -3,19 +3,33 @@ require "spec_helper"
3
3
  describe "Posts with attachments" do
4
4
  setup_integration_client
5
5
 
6
- it "exposes featured image as a Media instance" do
7
- media = find_or_upload_media
8
- post = client.create_post(title: "With media", featured_image: media.id)
6
+ it "exposes featured media as a Media instance" do
7
+ media = find_or_upload_image
8
+ post = client.create_post(title: "With image", featured_media: media.id)
9
+ expect(post.featured_media).to be_instance_of(WordpressClient::Media)
10
+ expect(post.featured_media.id).to eq media.id
11
+ expect(post.featured_media.slug).to eq media.slug
12
+ expect(post.featured_media.guid).to eq media.guid
13
+ expect(post.featured_media.source_url).to eq media.source_url
14
+ expect(post.featured_media_id).to eq media.id
15
+ end
9
16
 
17
+ it "exposes featured media as featured image if Media is an image" do
18
+ media = find_or_upload_image
19
+ post = client.create_post(title: "With image", featured_media: media.id)
10
20
  expect(post.featured_image).to be_instance_of(WordpressClient::Media)
21
+ expect(post.featured_image.id).to eq media.id
11
22
  expect(post.featured_image.slug).to eq media.slug
12
23
  expect(post.featured_image.guid).to eq media.guid
13
24
  expect(post.featured_image.source_url).to eq media.source_url
14
- expect(post.featured_image_id).to eq media.id
15
25
  end
16
26
 
17
- def find_or_upload_media
18
- client.media(per_page: 1).first ||
27
+ def find_or_upload_image
28
+ find_media_of_type("image") ||
19
29
  client.upload_file(fixture_path("thoughtful.jpg"), mime_type: "image/jpeg")
20
30
  end
31
+
32
+ def find_media_of_type(type)
33
+ client.media(per_page: 10).detect { |media| media.media_type == type }
34
+ end
21
35
  end
@@ -8,6 +8,7 @@ module WordpressClient
8
8
  media = Media.parse(fixture)
9
9
 
10
10
  expect(media.id).to eq 5
11
+ expect(media.media_type).to eq "image"
11
12
  expect(media.title_html).to eq "thoughtful"
12
13
  expect(media.slug).to eq "thoughtful"
13
14
  expect(media.description).to eq ""
@@ -59,5 +60,17 @@ module WordpressClient
59
60
  expect(media.updated_at).to eq Time.local(2001, 1, 1, 12, 0, 0)
60
61
  end
61
62
  end
63
+
64
+ describe "#as_image" do
65
+ it "returns self if media_type is image" do
66
+ media = Media.parse(fixture)
67
+ expect(media.as_image).to eq(media)
68
+ end
69
+
70
+ it "returns nil when media_type is not image" do
71
+ media = Media.parse(fixture.merge("media_type" => "video"))
72
+ expect(media.as_image).to be_nil
73
+ end
74
+ end
62
75
  end
63
76
  end
@@ -33,8 +33,8 @@ module WordpressClient
33
33
 
34
34
  expect(post.categories).to eq [
35
35
  Category.new(
36
- id: 1, name_html: "Uncategorized", slug: "uncategorized"
37
- )
36
+ id: 1, name_html: "Uncategorized", slug: "uncategorized",
37
+ ),
38
38
  ]
39
39
 
40
40
  expect(post.category_ids).to eq [1]
@@ -45,7 +45,7 @@ module WordpressClient
45
45
 
46
46
  expect(post.tags).to eq [
47
47
  Tag.new(
48
- id: 2, name_html: "Foo", slug: "foo"
48
+ id: 2, name_html: "winamp", slug: "winamp",
49
49
  )
50
50
  ]
51
51
 
@@ -54,10 +54,8 @@ module WordpressClient
54
54
 
55
55
  it "can have a Media as featured image" do
56
56
  media = instance_double(Media, id: 12)
57
- post = Post.new(featured_image: media)
58
-
59
- expect(post.featured_image).to eq media
60
- expect(post.featured_image_id).to eq 12
57
+ post = Post.new(featured_media: media)
58
+ expect(post.featured_media).to eq media
61
59
  end
62
60
 
63
61
  describe "dates" do
@@ -86,29 +84,5 @@ module WordpressClient
86
84
  end
87
85
  end
88
86
 
89
- describe "metadata" do
90
- it "is parsed into a hash" do
91
- post = Post.parse(json_fixture("post-with-metadata.json"))
92
- expect(post.meta).to eq "foo" => "bar"
93
- end
94
-
95
- it "raises UnauthorizedError when post it is forbidden" do
96
- expect {
97
- Post.parse(json_fixture("post-with-forbidden-metadata.json"))
98
- }.to raise_error(UnauthorizedError)
99
- end
100
-
101
- it "keeps track of the ID of each metadata key" do
102
- post = Post.parse(json_fixture("post-with-metadata.json"))
103
- expect(post.meta_id_for("foo")).to eq 2
104
- end
105
-
106
- it "raises ArgumentError when asked for the meta ID of a meta key not present" do
107
- post = Post.parse(json_fixture("post-with-metadata.json"))
108
- expect {
109
- post.meta_id_for("clearly unreal")
110
- }.to raise_error(ArgumentError, /clearly unreal/)
111
- end
112
- end
113
87
  end
114
88
  end
@@ -38,16 +38,31 @@ module DockerRunner
38
38
  end
39
39
  end
40
40
 
41
- def purge_container(id)
42
- output = `docker kill #{id.shellescape}; docker rm #{id.shellescape} `
43
-
44
- unless $?.success?
45
- message = "Could not clean up docker image #{id}. Output was:\n#{output}.\n"
46
- if ENV["CIRCLECI"]
47
- puts message
48
- else
49
- raise message
50
- end
41
+ def kill_container(id)
42
+ output = `docker kill #{id.shellescape} 2>&1`
43
+ raise_on_failure(
44
+ action: "kill",
45
+ id: id,
46
+ exit_status: $?,
47
+ output: output,
48
+ )
49
+ end
50
+
51
+ def remove_container(id)
52
+ output = `docker rm #{id.shellescape} 2>&1`
53
+ raise_on_failure(
54
+ action: "remove",
55
+ id: id,
56
+ exit_status: $?,
57
+ output: output,
58
+ )
59
+ end
60
+
61
+ private
62
+ def raise_on_failure(action:, exit_status:, output:, id:)
63
+ unless exit_status.success?
64
+ message = "Could not #{action} docker image #{id}. Output was:\n#{output}.\n"
65
+ raise message
51
66
  end
52
67
  end
53
68
  end
@@ -1,5 +1,7 @@
1
1
  require_relative "docker_runner"
2
2
 
3
+ # rubocop:disable Rails/TimeZone
4
+
3
5
  class WordpressServer
4
6
  include Singleton
5
7
 
@@ -47,9 +49,10 @@ class WordpressServer
47
49
 
48
50
  begin
49
51
  wait_for_container_to_start
50
- rescue
52
+ rescue => error
53
+ puts "Could not start container: #{error}. Cleaning up."
51
54
  purge_container
52
- raise $!
55
+ raise error
53
56
  end
54
57
  end
55
58
 
@@ -57,7 +60,7 @@ class WordpressServer
57
60
  unless DockerRunner.docker_installed?
58
61
  STDERR.puts(
59
62
  "It does not look like you have docker installed. " \
60
- "Please install docker so you can run integration tests."
63
+ "Please install docker so you can run integration tests.",
61
64
  )
62
65
  fail "No docker installed"
63
66
  end
@@ -73,21 +76,26 @@ class WordpressServer
73
76
  DockerRunner.run_container(
74
77
  DOCKER_IMAGE_NAME,
75
78
  port: port,
76
- environment: {wordpress_host: host_with_port}
79
+ environment: {wordpress_host: host_with_port},
77
80
  )
78
81
  end
79
82
 
80
83
  def purge_container
81
84
  if @running
82
- DockerRunner.purge_container(container_id)
83
- @running = true
85
+ DockerRunner.kill_container(container_id)
86
+ @running = false
87
+
88
+ # CircleCI does not allow `docker rm`.
89
+ unless ENV["CIRCLECI"]
90
+ DockerRunner.remove_container(container_id)
91
+ end
84
92
  end
85
93
  end
86
94
 
87
95
  def wait_for_container_to_start
88
96
  # Try to connect to the webserver in a loop until we successfully connect,
89
97
  # the container process dies, or the timeout is reached.
90
- timeout = 60
98
+ timeout = 10
91
99
  start = Time.now
92
100
 
93
101
  loop do
@@ -1,21 +1,20 @@
1
- # coding: utf-8
2
1
  lib = File.expand_path('../lib', __FILE__)
3
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
3
  require 'wordpress_client/version'
5
4
 
6
5
  Gem::Specification.new do |spec|
7
- spec.name = "wordpress_client"
8
- spec.version = WordpressClient::VERSION
9
- spec.authors = ["Magnus Bergmark", "Rebecca Meritz"]
10
- spec.email = ["magnus.bergmark@gmail.com", "rebecca@meritz.com"]
11
- spec.summary = "A simple client to the Wordpress API."
12
- spec.description = "A simple client to the Wordpress API."
13
- spec.homepage = ""
14
- spec.license = "MIT"
6
+ spec.name = "wordpress_client"
7
+ spec.version = WordpressClient::VERSION
8
+ spec.authors = ["Magnus Bergmark", "Rebecca Meritz", "Hans Maaherra"]
9
+ spec.email = ["magnus.bergmark@gmail.com", "rebecca@meritz.com", "hans.maaherra@gmail.com"]
10
+ spec.summary = "A simple client to the Wordpress API."
11
+ spec.description = "A simple client to the Wordpress API."
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
15
14
 
16
- spec.files = `git ls-files -z`.split("\x0")
17
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
- spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
18
  spec.require_paths = ["lib"]
20
19
 
21
20
  spec.add_dependency "faraday", "~> 0.9"
metadata CHANGED
@@ -1,15 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wordpress_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Magnus Bergmark
8
8
  - Rebecca Meritz
9
+ - Hans Maaherra
9
10
  autorequire:
10
11
  bindir: bin
11
12
  cert_chain: []
12
- date: 2016-05-18 00:00:00.000000000 Z
13
+ date: 2017-11-08 00:00:00.000000000 Z
13
14
  dependencies:
14
15
  - !ruby/object:Gem::Dependency
15
16
  name: faraday
@@ -99,6 +100,7 @@ description: A simple client to the Wordpress API.
99
100
  email:
100
101
  - magnus.bergmark@gmail.com
101
102
  - rebecca@meritz.com
103
+ - hans.maaherra@gmail.com
102
104
  executables: []
103
105
  extensions: []
104
106
  extra_rdoc_files: []
@@ -126,8 +128,6 @@ files:
126
128
  - lib/wordpress_client/paginated_collection.rb
127
129
  - lib/wordpress_client/post.rb
128
130
  - lib/wordpress_client/post_parser.rb
129
- - lib/wordpress_client/replace_metadata.rb
130
- - lib/wordpress_client/replace_terms.rb
131
131
  - lib/wordpress_client/rest_parser.rb
132
132
  - lib/wordpress_client/tag.rb
133
133
  - lib/wordpress_client/term.rb
@@ -143,7 +143,6 @@ files:
143
143
  - spec/fixtures/category.json
144
144
  - spec/fixtures/image-media.json
145
145
  - spec/fixtures/invalid-post-id.json
146
- - spec/fixtures/post-with-forbidden-metadata.json
147
146
  - spec/fixtures/post-with-metadata.json
148
147
  - spec/fixtures/simple-post.json
149
148
  - spec/fixtures/tag.json
@@ -151,18 +150,14 @@ files:
151
150
  - spec/fixtures/validation-error.json
152
151
  - spec/integration/attachments_crud_spec.rb
153
152
  - spec/integration/categories_spec.rb
154
- - spec/integration/category_assignment_spec.rb
155
153
  - spec/integration/posts_crud_spec.rb
156
154
  - spec/integration/posts_finding_spec.rb
157
155
  - spec/integration/posts_metadata_spec.rb
158
156
  - spec/integration/posts_with_attachments_spec.rb
159
- - spec/integration/tag_assignment_spec.rb
160
157
  - spec/integration/tags_spec.rb
161
158
  - spec/media_spec.rb
162
159
  - spec/paginated_collection_spec.rb
163
160
  - spec/post_spec.rb
164
- - spec/replace_metadata_spec.rb
165
- - spec/replace_terms_spec.rb
166
161
  - spec/shared_examples/term_examples.rb
167
162
  - spec/spec_helper.rb
168
163
  - spec/support/docker_runner.rb
@@ -191,7 +186,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
191
186
  version: '0'
192
187
  requirements: []
193
188
  rubyforge_project:
194
- rubygems_version: 2.4.8
189
+ rubygems_version: 2.6.13
195
190
  signing_key:
196
191
  specification_version: 4
197
192
  summary: A simple client to the Wordpress API.
@@ -207,7 +202,6 @@ test_files:
207
202
  - spec/fixtures/category.json
208
203
  - spec/fixtures/image-media.json
209
204
  - spec/fixtures/invalid-post-id.json
210
- - spec/fixtures/post-with-forbidden-metadata.json
211
205
  - spec/fixtures/post-with-metadata.json
212
206
  - spec/fixtures/simple-post.json
213
207
  - spec/fixtures/tag.json
@@ -215,18 +209,14 @@ test_files:
215
209
  - spec/fixtures/validation-error.json
216
210
  - spec/integration/attachments_crud_spec.rb
217
211
  - spec/integration/categories_spec.rb
218
- - spec/integration/category_assignment_spec.rb
219
212
  - spec/integration/posts_crud_spec.rb
220
213
  - spec/integration/posts_finding_spec.rb
221
214
  - spec/integration/posts_metadata_spec.rb
222
215
  - spec/integration/posts_with_attachments_spec.rb
223
- - spec/integration/tag_assignment_spec.rb
224
216
  - spec/integration/tags_spec.rb
225
217
  - spec/media_spec.rb
226
218
  - spec/paginated_collection_spec.rb
227
219
  - spec/post_spec.rb
228
- - spec/replace_metadata_spec.rb
229
- - spec/replace_terms_spec.rb
230
220
  - spec/shared_examples/term_examples.rb
231
221
  - spec/spec_helper.rb
232
222
  - spec/support/docker_runner.rb