wp-api 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3a72d49b5150ec356d93679a0327a678dfe1cb2a
4
- data.tar.gz: ac756f4350f9fce2b2dc42f1b77b631401deb884
3
+ metadata.gz: cb4a7bec44ae84c2ca08e6cb7dcb6f646c90f3aa
4
+ data.tar.gz: dfe857f61cd26b4f2310415d0a53d1a97f59506d
5
5
  SHA512:
6
- metadata.gz: 3f56e888f106c21ac212a0454e90b7836ba5cc9a5d3671b91c4512f7b0dd4fae9ca1038099be7271b7c6e2173864f1e323f46611ab62f798b3cb49b6dc13f424
7
- data.tar.gz: d72cdba3386dcffaad68f8375f09e9fa3e29e21ef0b2c8ccdc9139e2a6db2bc65c9d63b158aaac2ee000ff563000e8a9cc72b1c516e85e6af6bbdbdafaf5e6fc
6
+ metadata.gz: 6001d3d7ebb517b4d79fa29de3c2ec3a28f9a613230cce957e35201534edbc49204c9510f495fa1f1f6aa4d6af6507a27e71c729b62b2c6a6106ee097fc7cc7b
7
+ data.tar.gz: 853132092fe77cea5bf06004d2a6d8377fa3fa498d4459c01f9942331f99c00950adf2881c877029d5b87ab6ad93a034dd6ef0f04dccde04ce4e1188bb075826
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  [![Circle CI](https://circleci.com/gh/colinyoung/wp-api.png?style=badge)](https://circleci.com/gh/colinyoung/wp-api)
4
4
 
5
- Makes it incredibly easy and semantic to access Wordpress blogs that have the new, RESTful WP API plugin installed.
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
- def initialize(host:, scheme: 'http')
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
- response = Client.get(path)
25
- if response.empty?
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
@@ -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
@@ -14,6 +14,11 @@ module WP::API
14
14
  attributes['ID']
15
15
  end
16
16
 
17
+ def meta(client = nil)
18
+ return super unless client
19
+ client.post_meta(id)
20
+ end
21
+
17
22
  protected
18
23
 
19
24
  def method_missing(key, new_value = nil)
@@ -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
@@ -0,0 +1,7 @@
1
+ module WP::API
2
+ class Tag < Resource
3
+ def to_param
4
+ slug
5
+ end
6
+ end
7
+ end
@@ -1,5 +1,5 @@
1
1
  module WP
2
2
  module API
3
- VERSION = "0.1.2"
3
+ VERSION = "0.1.3"
4
4
  end
5
5
  end
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.2
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-09-26 00:00:00.000000000 Z
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