wp-api 0.1.2 → 0.1.3
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.
- checksums.yaml +4 -4
- data/README.md +11 -1
- data/lib/wp/api/client.rb +23 -3
- data/lib/wp/api/endpoints.rb +8 -0
- data/lib/wp/api/resource.rb +5 -0
- data/lib/wp/api/resources/post.rb +4 -0
- data/lib/wp/api/resources/tag.rb +7 -0
- data/lib/wp/api/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cb4a7bec44ae84c2ca08e6cb7dcb6f646c90f3aa
|
4
|
+
data.tar.gz: dfe857f61cd26b4f2310415d0a53d1a97f59506d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6001d3d7ebb517b4d79fa29de3c2ec3a28f9a613230cce957e35201534edbc49204c9510f495fa1f1f6aa4d6af6507a27e71c729b62b2c6a6106ee097fc7cc7b
|
7
|
+
data.tar.gz: 853132092fe77cea5bf06004d2a6d8377fa3fa498d4459c01f9942331f99c00950adf2881c877029d5b87ab6ad93a034dd6ef0f04dccde04ce4e1188bb075826
|
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
[](https://circleci.com/gh/colinyoung/wp-api)
|
4
4
|
|
5
|
-
Makes it incredibly easy and semantic to access Wordpress blogs that have the
|
5
|
+
Makes it incredibly easy and semantic to access Wordpress blogs that have the [WP REST API plugin](http://wp-api.org/) installed (which is planned for full integration into wordpress core soon.)
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
@@ -23,6 +23,16 @@ client.posts
|
|
23
23
|
|
24
24
|
# List all users
|
25
25
|
client.users
|
26
|
+
|
27
|
+
# List alternate post types
|
28
|
+
client.posts(type: 'custom_posts')
|
29
|
+
|
30
|
+
# Append paramters
|
31
|
+
client.posts(posts_per_page: 1000)
|
32
|
+
|
33
|
+
# Use basic auth (used to access post meta)
|
34
|
+
client = WP::API::Client.new(host: 'yourwpsite.com', scheme: 'https', user: 'api', password: 'apipassword')
|
35
|
+
client.post_meta(1234) # => metadata for post #1234
|
26
36
|
```
|
27
37
|
|
28
38
|
## Author
|
data/lib/wp/api/client.rb
CHANGED
@@ -10,19 +10,34 @@ module WP::API
|
|
10
10
|
|
11
11
|
attr_accessor :host
|
12
12
|
|
13
|
-
|
13
|
+
DIRECT_PARAMS = %w(type context filter)
|
14
|
+
|
15
|
+
def initialize(host:, scheme: 'http', user: nil, password: nil)
|
14
16
|
@scheme = scheme
|
15
17
|
@host = host
|
18
|
+
@user = user
|
19
|
+
@password = password
|
20
|
+
|
16
21
|
fail ':host is required' unless host.is_a?(String) && host.length > 0
|
17
22
|
end
|
18
23
|
|
24
|
+
def inspect
|
25
|
+
to_s.sub(/>$/, '') + " @scheme=\"#{@scheme}\" @host=\"#{@host}\" @user=\"#{@user}\" @password=#{@password.present?}>"
|
26
|
+
end
|
27
|
+
|
19
28
|
protected
|
20
29
|
|
21
30
|
def get(resource, query = {})
|
22
31
|
query = ActiveSupport::HashWithIndifferentAccess.new(query)
|
23
32
|
path = url_for(resource, query)
|
24
|
-
|
25
|
-
if
|
33
|
+
|
34
|
+
response = if authenticate?
|
35
|
+
Client.get(path, basic_auth: { username: @user, password: @password })
|
36
|
+
else
|
37
|
+
Client.get(path)
|
38
|
+
end
|
39
|
+
|
40
|
+
if response.code != 200
|
26
41
|
raise WP::API::ResourceNotFoundError
|
27
42
|
else
|
28
43
|
[ response.parsed_response, response.headers ] # Already parsed.
|
@@ -31,6 +46,10 @@ module WP::API
|
|
31
46
|
|
32
47
|
private
|
33
48
|
|
49
|
+
def authenticate?
|
50
|
+
@user && @password
|
51
|
+
end
|
52
|
+
|
34
53
|
def url_for(fragment, query)
|
35
54
|
url = "#{@scheme}://#{@host}/wp-json/#{fragment}"
|
36
55
|
url << ("?" + params(query)) unless query.empty?
|
@@ -42,6 +61,7 @@ module WP::API
|
|
42
61
|
uri = Addressable::URI.new
|
43
62
|
filter_hash = { page: query.delete('page') || 1 }
|
44
63
|
query.each do |key, value|
|
64
|
+
filter_hash[key] = value if DIRECT_PARAMS.include?(key) || key.include?('[')
|
45
65
|
filter_hash["filter[#{key}]"] = value
|
46
66
|
end
|
47
67
|
uri.query_values = filter_hash
|
data/lib/wp/api/endpoints.rb
CHANGED
@@ -13,6 +13,10 @@ module WP::API
|
|
13
13
|
resource_named('posts', slug)
|
14
14
|
end
|
15
15
|
|
16
|
+
def post_meta(id, query = {})
|
17
|
+
resource_subpath('posts', id, 'meta', query).first
|
18
|
+
end
|
19
|
+
|
16
20
|
def pages(query = {})
|
17
21
|
resources('pages', query)
|
18
22
|
end
|
@@ -38,6 +42,10 @@ module WP::API
|
|
38
42
|
resource_class(res).new *get("#{res}/#{id}", query)
|
39
43
|
end
|
40
44
|
|
45
|
+
def resource_subpath(res, id, subpath, query = {})
|
46
|
+
get("#{res}/#{id}/#{subpath}", query).first
|
47
|
+
end
|
48
|
+
|
41
49
|
def resource_named(res, slug)
|
42
50
|
resources(res, name: slug).first
|
43
51
|
end
|
data/lib/wp/api/resource.rb
CHANGED
@@ -14,6 +14,10 @@ module WP::API
|
|
14
14
|
terms['category'].collect {|cat| WP::API::Category.new(cat) }
|
15
15
|
end
|
16
16
|
|
17
|
+
def tags
|
18
|
+
terms['post_tag'].collect {|cat| WP::API::Tag.new(cat) }
|
19
|
+
end
|
20
|
+
|
17
21
|
def prev
|
18
22
|
item = link_header_items.find {|rel, url| rel == "prev" }
|
19
23
|
item.last if item
|
data/lib/wp/api/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wp-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Colin Young
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-10-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httparty
|
@@ -181,6 +181,7 @@ files:
|
|
181
181
|
- lib/wp/api/resources/category.rb
|
182
182
|
- lib/wp/api/resources/page.rb
|
183
183
|
- lib/wp/api/resources/post.rb
|
184
|
+
- lib/wp/api/resources/tag.rb
|
184
185
|
- lib/wp/api/version.rb
|
185
186
|
- spec/endpoints/post_spec.rb
|
186
187
|
- spec/endpoints/posts_spec.rb
|