wp-api-client 0.1.0 → 0.2.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.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/Gemfile +3 -0
- data/README.md +73 -9
- data/lib/wp_api_client.rb +20 -1
- data/lib/wp_api_client/client.rb +3 -19
- data/lib/wp_api_client/collection.rb +7 -8
- data/lib/wp_api_client/configuration.rb +44 -0
- data/lib/wp_api_client/connection.rb +19 -5
- data/lib/wp_api_client/entities/base.rb +36 -0
- data/lib/wp_api_client/entities/image.rb +15 -0
- data/lib/wp_api_client/entities/meta.rb +19 -0
- data/lib/wp_api_client/entities/post.rb +16 -12
- data/lib/wp_api_client/entities/taxonomy.rb +2 -2
- data/lib/wp_api_client/entities/term.rb +4 -6
- data/lib/wp_api_client/entities/types.rb +11 -0
- data/lib/wp_api_client/relationship.rb +110 -0
- data/lib/wp_api_client/version.rb +1 -1
- data/wp_api_client.gemspec +2 -0
- metadata +37 -3
- data/lib/wp_api_client/entities/base_entity.rb +0 -25
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a7a1b56612823b783b538e87dc5caf6ed4e00781
|
4
|
+
data.tar.gz: 103661f8e6887d36dd16d2c09f0f20f89958928e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b714dee47f9db903f43ae53d755164e8c92f791a2c531e99ad52c72c016765f304575b46b07e42629bbc0fb699f786114b6c207626c87098f467fc80fbf01ee4
|
7
|
+
data.tar.gz: bbe61828ae974866f0ece28763ce654a2d34fdaf1b3b2c18b20ea552815d8e2dcb13dce8f7fae7ecc121c5a854f1516515e1165f088614a81e33578c8a2c7680
|
data/.gitignore
CHANGED
data/Gemfile
ADDED
data/README.md
CHANGED
@@ -2,9 +2,9 @@
|
|
2
2
|
|
3
3
|
This unambitious client provides read-only access for WP-API v2.
|
4
4
|
|
5
|
-
It
|
5
|
+
It supports authentication via OAuth.
|
6
6
|
|
7
|
-
It does not support comments or POST requests.
|
7
|
+
It does not support comments, users or POST requests.
|
8
8
|
|
9
9
|
It requires **Ruby 2.3** and is tested against **WP-API 2.0-beta12**.
|
10
10
|
|
@@ -26,8 +26,12 @@ require 'wp_api_client'
|
|
26
26
|
|
27
27
|
```ruby
|
28
28
|
# create a client
|
29
|
-
|
30
|
-
|
29
|
+
|
30
|
+
WpApiClient.configure do |api_client|
|
31
|
+
api_client.endpoint = 'http://example.com/wp-json/wp/v2'
|
32
|
+
end
|
33
|
+
|
34
|
+
@api = WpApiClient.get_client
|
31
35
|
|
32
36
|
# get some posts
|
33
37
|
posts = @api.get('custom_post_type/') # or "posts/" etc
|
@@ -51,7 +55,7 @@ term.taxonomy
|
|
51
55
|
term.posts
|
52
56
|
# => #<WpApiClient::Collection:0x007fd65d07d588 @resources=[#<WpApiClient::Entities::Post...
|
53
57
|
|
54
|
-
# term.posts.first.terms.first.taxonomy
|
58
|
+
# term.posts("custom_post_type").first.terms("category").first.taxonomy... etc etc etc
|
55
59
|
```
|
56
60
|
|
57
61
|
#### Pagination
|
@@ -72,6 +76,54 @@ page_after_that = @api.get(next_page.page_after_that)
|
|
72
76
|
# => #<WpApiClient::Collection:0x00bbcafe938827 @resources=[#<WpApiClient::Entities::Post...
|
73
77
|
```
|
74
78
|
|
79
|
+
#### Defining relationships
|
80
|
+
|
81
|
+
The [REST API docs](http://v2.wp-api.org/extending/linking/) invite you to define
|
82
|
+
custom relationships to go alongside "http://api.w.org/term" etc.
|
83
|
+
|
84
|
+
For example, let's say you have a `person` post type and a post-to-post relation
|
85
|
+
defined through meta and exposed in the REST API like this:
|
86
|
+
|
87
|
+
```php
|
88
|
+
add_filter( 'rest_prepare_king', function( $data, $king ) {
|
89
|
+
if( $king->queen ) {
|
90
|
+
$data->add_link(
|
91
|
+
'http://api.myuniqueuri.com/marriage',
|
92
|
+
rest_url( '/wp/v2/person/'.$king->queen ),
|
93
|
+
['embeddable' => true]
|
94
|
+
);
|
95
|
+
}
|
96
|
+
return $data;
|
97
|
+
}, 10, 2);
|
98
|
+
```
|
99
|
+
|
100
|
+
This will cause the `http://api.myuniqueuri.com/marriage` relation to be reflected
|
101
|
+
in your `_links` property when you call up the King from the REST API.
|
102
|
+
|
103
|
+
But you'll get an error if you try to query this relationship using the client.
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
king = @api.get('person/1')
|
107
|
+
queen = king.relations("http://api.myuniqueuri.com/marriage").first
|
108
|
+
# => throws WpApiClient::RelationNotDefined
|
109
|
+
```
|
110
|
+
|
111
|
+
The solution is to register the relationship on configuration:
|
112
|
+
|
113
|
+
```ruby
|
114
|
+
WpApiClient.configure do |c|
|
115
|
+
c.define_mapping("http://api.myuniqueuri.com/marriage", :post)
|
116
|
+
end
|
117
|
+
|
118
|
+
...
|
119
|
+
|
120
|
+
king = @api.get('person/1')
|
121
|
+
queen = king.relations("http://api.myuniqueuri.com/marriage").first
|
122
|
+
# => #<WpApiClient::Entities::Post:0x007fed42b3e458 @resource={"id"=>2...
|
123
|
+
```
|
124
|
+
|
125
|
+
There is currently support for `:post_type`, `:post`, `:term` and `:meta` (key/value) relations.
|
126
|
+
|
75
127
|
#### Loading a taxonomy via a slug
|
76
128
|
|
77
129
|
WP-API returns an array even if there's only one result, so you need to be careful here
|
@@ -82,11 +134,25 @@ taxonomy_name = term.taxonomy.name
|
|
82
134
|
posts = term.posts
|
83
135
|
```
|
84
136
|
|
137
|
+
#### OAuth
|
138
|
+
|
139
|
+
Provide a symbol-keyed hash of `token`, `token_secret`, `consumer_key` and `consumer_secret` on configuration.
|
140
|
+
|
141
|
+
```ruby
|
142
|
+
WpApiClient.configure do |api_client|
|
143
|
+
api_client.oauth_credentials = oauth_credentials_hash
|
144
|
+
end
|
145
|
+
|
146
|
+
client = WpApiClient.get_client
|
147
|
+
```
|
148
|
+
|
85
149
|
## Testing and compatibility
|
86
150
|
|
87
151
|
This library comes with VCR cassettes recorded against a local WP installation
|
88
152
|
running WP-API v2-beta12. It is not tested with other versions.
|
89
153
|
|
154
|
+
If you want to make your own VCR cassettes, use [these scripts](https://github.com/duncanjbrown/WP-REST-Test).
|
155
|
+
|
90
156
|
To run the tests, invoke `rspec`.
|
91
157
|
|
92
158
|
## Structure
|
@@ -119,12 +185,10 @@ next_page = @api.get(posts.next_page)
|
|
119
185
|
# => #<WpApiClient::Collection:0x00bbcafe938827 @resources=[#<WpApiClient::Entities::Post...
|
120
186
|
```
|
121
187
|
|
122
|
-
#### `WpApiClient::Entities::
|
188
|
+
#### `WpApiClient::Entities::Base`
|
123
189
|
|
124
|
-
Base class for `Post`, `Term` and `Taxonomy`, so far. Not all methods are implemented.
|
190
|
+
Base class for `Post`, `Term`, `Image` and `Taxonomy`, so far. Not all methods are implemented.
|
125
191
|
|
126
192
|
## Other
|
127
193
|
|
128
|
-
This library puts `?_embed` on every request to save on HTTP requests right now.
|
129
|
-
|
130
194
|
Thanks [WP-API](https://github.com/WP-API/WP-API)!
|
data/lib/wp_api_client.rb
CHANGED
@@ -1,10 +1,29 @@
|
|
1
1
|
require "wp_api_client/version"
|
2
|
+
require "wp_api_client/configuration"
|
3
|
+
|
4
|
+
require "wp_api_client/entities/base"
|
2
5
|
|
3
|
-
require "wp_api_client/entities/base_entity"
|
4
6
|
require "wp_api_client/entities/post"
|
7
|
+
require "wp_api_client/entities/meta"
|
5
8
|
require "wp_api_client/entities/taxonomy"
|
6
9
|
require "wp_api_client/entities/term"
|
10
|
+
require "wp_api_client/entities/image"
|
11
|
+
require "wp_api_client/entities/types"
|
7
12
|
|
8
13
|
require "wp_api_client/client"
|
9
14
|
require "wp_api_client/connection"
|
10
15
|
require "wp_api_client/collection"
|
16
|
+
require "wp_api_client/relationship"
|
17
|
+
|
18
|
+
module WpApiClient
|
19
|
+
def self.get_client
|
20
|
+
@client ||= Client.new(Connection.new(configuration))
|
21
|
+
end
|
22
|
+
|
23
|
+
# for tests
|
24
|
+
def self.reset!
|
25
|
+
@client = nil
|
26
|
+
end
|
27
|
+
|
28
|
+
class RelationNotDefined < StandardError; end
|
29
|
+
end
|
data/lib/wp_api_client/client.rb
CHANGED
@@ -1,12 +1,6 @@
|
|
1
1
|
module WpApiClient
|
2
2
|
class Client
|
3
3
|
|
4
|
-
Types = [
|
5
|
-
WpApiClient::Entities::Post,
|
6
|
-
WpApiClient::Entities::Term,
|
7
|
-
WpApiClient::Entities::Taxonomy
|
8
|
-
]
|
9
|
-
|
10
4
|
def initialize(connection)
|
11
5
|
@connection = connection
|
12
6
|
end
|
@@ -26,21 +20,11 @@ module WpApiClient
|
|
26
20
|
|
27
21
|
# Take the API response and figure out what it is
|
28
22
|
def native_representation_of(response_body)
|
23
|
+
# Do we have a collection of objects?
|
29
24
|
if response_body.is_a? Array
|
30
|
-
|
31
|
-
object = response_body.first
|
32
|
-
else
|
33
|
-
collection = false
|
34
|
-
object = response_body
|
35
|
-
end
|
36
|
-
|
37
|
-
type = Types.find { |type| type.represents?(object) }
|
38
|
-
|
39
|
-
if collection
|
40
|
-
resources = response_body.map! { |object| type.new(object, self) }
|
41
|
-
WpApiClient::Collection.new(resources, @headers)
|
25
|
+
WpApiClient::Collection.new(response_body, @headers)
|
42
26
|
else
|
43
|
-
|
27
|
+
WpApiClient::Entities::Base.build(response_body)
|
44
28
|
end
|
45
29
|
end
|
46
30
|
end
|
@@ -4,10 +4,13 @@ module WpApiClient
|
|
4
4
|
|
5
5
|
attr_accessor :resources, :total_available
|
6
6
|
|
7
|
-
def initialize(resources, headers)
|
8
|
-
|
9
|
-
@
|
10
|
-
|
7
|
+
def initialize(resources, headers = nil)
|
8
|
+
resources = [resources] unless resources.is_a? Array
|
9
|
+
@resources = resources.map { |object| WpApiClient::Entities::Base.build(object) }
|
10
|
+
if headers
|
11
|
+
@links = parse_link_header(headers['Link'])
|
12
|
+
@total_available = headers['X-WP-TOTAL'].to_i
|
13
|
+
end
|
11
14
|
end
|
12
15
|
|
13
16
|
def each(&block)
|
@@ -22,10 +25,6 @@ module WpApiClient
|
|
22
25
|
@links[:prev] && @links[:prev]
|
23
26
|
end
|
24
27
|
|
25
|
-
def method_missing(sym)
|
26
|
-
@resources.send(sym)
|
27
|
-
end
|
28
|
-
|
29
28
|
private
|
30
29
|
|
31
30
|
# https://www.snip2code.com/Snippet/71914/Parse-link-headers-from-Github-API-in-Ru
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module WpApiClient
|
2
|
+
|
3
|
+
class << self
|
4
|
+
attr_writer :configuration
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.configuration
|
8
|
+
@configuration ||= Configuration.new
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.configure
|
12
|
+
yield(configuration)
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.reset
|
16
|
+
@configuration = Configuration.new
|
17
|
+
end
|
18
|
+
|
19
|
+
class Configuration
|
20
|
+
attr_accessor :endpoint
|
21
|
+
attr_accessor :embed
|
22
|
+
attr_accessor :oauth_credentials
|
23
|
+
attr_accessor :debug
|
24
|
+
attr_accessor :cache
|
25
|
+
|
26
|
+
def initialize
|
27
|
+
@endpoint = 'http://localhost:8080/wp-json/wp/v2'
|
28
|
+
@embed = true
|
29
|
+
end
|
30
|
+
|
31
|
+
def define_mapping(relation, type)
|
32
|
+
WpApiClient::Relationship.define(relation, type)
|
33
|
+
end
|
34
|
+
|
35
|
+
def request_params
|
36
|
+
params = {}
|
37
|
+
if @embed
|
38
|
+
params[:_embed] = true
|
39
|
+
end
|
40
|
+
params
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
@@ -1,15 +1,29 @@
|
|
1
1
|
require 'faraday'
|
2
2
|
require 'faraday_middleware'
|
3
|
+
require 'faraday-http-cache'
|
3
4
|
|
4
5
|
module WpApiClient
|
5
6
|
class Connection
|
6
7
|
|
7
8
|
attr_accessor :headers
|
8
9
|
|
9
|
-
def initialize(
|
10
|
-
@
|
11
|
-
|
12
|
-
|
10
|
+
def initialize(configuration)
|
11
|
+
@configuration = configuration
|
12
|
+
@conn = Faraday.new(url: configuration.endpoint) do |faraday|
|
13
|
+
|
14
|
+
if configuration.oauth_credentials
|
15
|
+
faraday.use FaradayMiddleware::OAuth, configuration.oauth_credentials
|
16
|
+
end
|
17
|
+
|
18
|
+
if configuration.debug
|
19
|
+
faraday.response :logger
|
20
|
+
end
|
21
|
+
|
22
|
+
if configuration.cache
|
23
|
+
faraday.use :http_cache, store: configuration.cache
|
24
|
+
end
|
25
|
+
|
26
|
+
faraday.use Faraday::Response::RaiseError
|
13
27
|
faraday.response :json, :content_type => /\bjson$/
|
14
28
|
faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
|
15
29
|
end
|
@@ -17,7 +31,7 @@ module WpApiClient
|
|
17
31
|
|
18
32
|
# translate requests into wp-api urls
|
19
33
|
def get(url, params = {})
|
20
|
-
@conn.get url, params.merge(
|
34
|
+
@conn.get url, params.merge(@configuration.request_params)
|
21
35
|
end
|
22
36
|
end
|
23
37
|
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'open-uri'
|
2
|
+
|
3
|
+
module WpApiClient
|
4
|
+
module Entities
|
5
|
+
class Base
|
6
|
+
attr_reader :resource
|
7
|
+
|
8
|
+
def self.build(resource)
|
9
|
+
raise Exception if resource.nil?
|
10
|
+
type = WpApiClient::Entities::Types.find { |type| type.represents?(resource) }
|
11
|
+
type.new(resource)
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize(resource)
|
15
|
+
unless resource.is_a? Hash
|
16
|
+
raise ArgumentError.new('Tried to initialize a WP-API resource with something other than a Hash')
|
17
|
+
end
|
18
|
+
@resource = resource
|
19
|
+
end
|
20
|
+
|
21
|
+
def links
|
22
|
+
resource["_links"]
|
23
|
+
end
|
24
|
+
|
25
|
+
def relations(relation, relation_to_return = nil)
|
26
|
+
relationship = Relationship.new(@resource, relation)
|
27
|
+
relations = relationship.get_relations
|
28
|
+
if relation_to_return
|
29
|
+
relations[relation_to_return]
|
30
|
+
else
|
31
|
+
relations
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module WpApiClient
|
2
|
+
module Entities
|
3
|
+
class Image < Base
|
4
|
+
alias :image :resource
|
5
|
+
|
6
|
+
def self.represents?(json)
|
7
|
+
json["media_type"] and json["media_type"] == 'image'
|
8
|
+
end
|
9
|
+
|
10
|
+
def sizes(size = :full)
|
11
|
+
image.dig("media_details", "sizes", size.to_s, "source_url")
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module WpApiClient
|
2
|
+
module Entities
|
3
|
+
class Meta < Base
|
4
|
+
alias :meta :resource
|
5
|
+
|
6
|
+
def self.represents?(json)
|
7
|
+
json["key"] and json["value"]
|
8
|
+
end
|
9
|
+
|
10
|
+
def key
|
11
|
+
meta["key"]
|
12
|
+
end
|
13
|
+
|
14
|
+
def value
|
15
|
+
meta["value"]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module WpApiClient
|
2
2
|
module Entities
|
3
|
-
class Post <
|
3
|
+
class Post < Base
|
4
4
|
alias :post :resource
|
5
5
|
|
6
6
|
def self.represents?(json)
|
@@ -11,28 +11,32 @@ module WpApiClient
|
|
11
11
|
post["title"]["rendered"]
|
12
12
|
end
|
13
13
|
|
14
|
+
def slug
|
15
|
+
post["slug"]
|
16
|
+
end
|
17
|
+
|
14
18
|
def date
|
15
|
-
Time.
|
19
|
+
Time.parse(post["date_gmt"]) if post["date_gmt"]
|
16
20
|
end
|
17
21
|
|
18
22
|
def content
|
19
23
|
post["content"]["rendered"]
|
20
24
|
end
|
21
25
|
|
26
|
+
def excerpt
|
27
|
+
post["excerpt"]["rendered"]
|
28
|
+
end
|
29
|
+
|
22
30
|
def id
|
23
31
|
post["id"]
|
24
32
|
end
|
25
33
|
|
26
|
-
def terms
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
end
|
33
|
-
end
|
34
|
-
terms
|
35
|
-
end
|
34
|
+
def terms(taxonomy = nil)
|
35
|
+
relations("https://api.w.org/term", taxonomy)
|
36
|
+
end
|
37
|
+
|
38
|
+
def meta(key = nil)
|
39
|
+
@meta ||= relations("https://api.w.org/meta", key)
|
36
40
|
end
|
37
41
|
end
|
38
42
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module WpApiClient
|
2
2
|
module Entities
|
3
|
-
class Taxonomy <
|
3
|
+
class Taxonomy < Base
|
4
4
|
alias :taxonomy :resource
|
5
5
|
|
6
6
|
def self.represents?(json)
|
@@ -12,7 +12,7 @@ module WpApiClient
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def terms
|
15
|
-
|
15
|
+
relations("https://api.w.org/items")
|
16
16
|
end
|
17
17
|
end
|
18
18
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module WpApiClient
|
2
2
|
module Entities
|
3
|
-
class Term <
|
3
|
+
class Term < Base
|
4
4
|
alias :term :resource
|
5
5
|
|
6
6
|
def self.represents?(json)
|
@@ -8,13 +8,11 @@ module WpApiClient
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def taxonomy
|
11
|
-
|
11
|
+
WpApiClient.get_client.get(links["about"].first["href"])
|
12
12
|
end
|
13
13
|
|
14
|
-
def posts(post_type =
|
15
|
-
|
16
|
-
link = post_type_links.find { |link| link["href"] =~ /wp\/v2\/#{post_type}/ }
|
17
|
-
@api.get(link["href"]) if link
|
14
|
+
def posts(post_type = nil)
|
15
|
+
relations("http://api.w.org/v2/post_type", post_type)
|
18
16
|
end
|
19
17
|
|
20
18
|
def name
|
@@ -0,0 +1,110 @@
|
|
1
|
+
require 'open-uri'
|
2
|
+
|
3
|
+
module WpApiClient
|
4
|
+
class Relationship
|
5
|
+
|
6
|
+
class << self
|
7
|
+
attr_writer :mappings
|
8
|
+
|
9
|
+
def mappings
|
10
|
+
@mappings ||= default_mappings
|
11
|
+
end
|
12
|
+
|
13
|
+
def define(relation, type)
|
14
|
+
mappings[relation] = type
|
15
|
+
end
|
16
|
+
|
17
|
+
def default_mappings
|
18
|
+
{
|
19
|
+
"https://api.w.org/term" => :term,
|
20
|
+
"https://api.w.org/items" => :terms,
|
21
|
+
"http://api.w.org/v2/post_type" => :post_type,
|
22
|
+
"https://api.w.org/meta" => :meta,
|
23
|
+
"https://api.w.org/featuredmedia" => :post
|
24
|
+
}
|
25
|
+
end
|
26
|
+
|
27
|
+
def term(r)
|
28
|
+
relations = {}
|
29
|
+
r.resource["_links"][r.relation].each_with_index do |link, position|
|
30
|
+
relations.merge! Hash[link["taxonomy"], r.load_relation(r.relation, position)]
|
31
|
+
end
|
32
|
+
relations
|
33
|
+
end
|
34
|
+
|
35
|
+
def terms(r)
|
36
|
+
r.load_relation(r.relation, 0)
|
37
|
+
end
|
38
|
+
|
39
|
+
def post_type(r)
|
40
|
+
relations = {}
|
41
|
+
r.resource["_links"][r.relation].each_with_index do |link, position|
|
42
|
+
# get the post type out of the linked URL.
|
43
|
+
post_type = URI.parse(link["href"]).path.split('wp/v2/').pop.split('/').first
|
44
|
+
relations.merge! Hash[post_type, r.load_relation(r.relation, position)]
|
45
|
+
end
|
46
|
+
relations
|
47
|
+
end
|
48
|
+
|
49
|
+
def post(r)
|
50
|
+
r.load_relation(r.relation)
|
51
|
+
end
|
52
|
+
|
53
|
+
def meta(r)
|
54
|
+
relations = {}
|
55
|
+
meta = WpApiClient.get_client.get(r.resource["_links"][r.relation].first["href"])
|
56
|
+
meta.map do |m|
|
57
|
+
relations.merge! Hash[m.key, m.value]
|
58
|
+
end
|
59
|
+
relations
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
attr_reader :resource
|
64
|
+
attr_reader :relation
|
65
|
+
|
66
|
+
def initialize(resource, relation)
|
67
|
+
@resource = resource
|
68
|
+
@relation = relation
|
69
|
+
end
|
70
|
+
|
71
|
+
def get_relations
|
72
|
+
mapping = self.class.mappings[@relation]
|
73
|
+
if !mapping
|
74
|
+
raise WpApiClient::RelationNotDefined.new %{
|
75
|
+
=> The relation "#{@relation}" is not defined.
|
76
|
+
|
77
|
+
To add a new relation, define it at configuration. For example, to define this
|
78
|
+
relation as one that links to a post object, you would do the following.
|
79
|
+
|
80
|
+
WpApiClient.configure do |c|
|
81
|
+
c.define_mapping(#{@relation}, :post)
|
82
|
+
end
|
83
|
+
|
84
|
+
The currently defined relations are:
|
85
|
+
|
86
|
+
#{self.class.mappings.keys.join("\n") }
|
87
|
+
|
88
|
+
Available mappings are :post, :term, and :meta.}
|
89
|
+
end
|
90
|
+
|
91
|
+
# Only try to fetch the relation if there are any links to it
|
92
|
+
self.class.send(mapping, self) if resource["_links"][relation]
|
93
|
+
end
|
94
|
+
|
95
|
+
# try to load an embedded object; call out to the API if not
|
96
|
+
def load_relation(relationship, position = nil)
|
97
|
+
if objects = @resource.dig("_embedded", relationship)
|
98
|
+
location = position ? objects[position] : objects
|
99
|
+
WpApiClient::Collection.new(location)
|
100
|
+
else
|
101
|
+
unless position.nil?
|
102
|
+
location = @resource["_links"].dig(relationship, position.to_i, "href")
|
103
|
+
else
|
104
|
+
location = @resource["_links"][relationship]["href"]
|
105
|
+
end
|
106
|
+
WpApiClient.get_client.get(location) if location
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
data/wp_api_client.gemspec
CHANGED
@@ -30,6 +30,8 @@ Gem::Specification.new do |spec|
|
|
30
30
|
|
31
31
|
spec.add_dependency "faraday", "~> 0.9"
|
32
32
|
spec.add_dependency "faraday_middleware", "~> 0.10"
|
33
|
+
spec.add_dependency "faraday-http-cache", "~> 1.2"
|
34
|
+
spec.add_dependency "simple_oauth", "~> 0.3"
|
33
35
|
|
34
36
|
spec.add_development_dependency "bundler", "~> 1.11"
|
35
37
|
spec.add_development_dependency "rake", "~> 10.0"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wp-api-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Duncan Brown
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-03-
|
11
|
+
date: 2016-03-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -38,6 +38,34 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0.10'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: faraday-http-cache
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.2'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.2'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: simple_oauth
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.3'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.3'
|
41
69
|
- !ruby/object:Gem::Dependency
|
42
70
|
name: bundler
|
43
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -105,6 +133,7 @@ files:
|
|
105
133
|
- ".rspec"
|
106
134
|
- ".travis.yml"
|
107
135
|
- CODE_OF_CONDUCT.md
|
136
|
+
- Gemfile
|
108
137
|
- LICENSE.txt
|
109
138
|
- README.md
|
110
139
|
- Rakefile
|
@@ -113,11 +142,16 @@ files:
|
|
113
142
|
- lib/wp_api_client.rb
|
114
143
|
- lib/wp_api_client/client.rb
|
115
144
|
- lib/wp_api_client/collection.rb
|
145
|
+
- lib/wp_api_client/configuration.rb
|
116
146
|
- lib/wp_api_client/connection.rb
|
117
|
-
- lib/wp_api_client/entities/
|
147
|
+
- lib/wp_api_client/entities/base.rb
|
148
|
+
- lib/wp_api_client/entities/image.rb
|
149
|
+
- lib/wp_api_client/entities/meta.rb
|
118
150
|
- lib/wp_api_client/entities/post.rb
|
119
151
|
- lib/wp_api_client/entities/taxonomy.rb
|
120
152
|
- lib/wp_api_client/entities/term.rb
|
153
|
+
- lib/wp_api_client/entities/types.rb
|
154
|
+
- lib/wp_api_client/relationship.rb
|
121
155
|
- lib/wp_api_client/version.rb
|
122
156
|
- wp_api_client.gemspec
|
123
157
|
homepage: https://github.com/duncanjbrown/wp-api-client
|
@@ -1,25 +0,0 @@
|
|
1
|
-
module WpApiClient
|
2
|
-
module Entities
|
3
|
-
class BaseEntity
|
4
|
-
attr_reader :resource
|
5
|
-
|
6
|
-
def initialize(resource, api)
|
7
|
-
unless resource.is_a? Hash
|
8
|
-
raise ArgumentError.new('Tried to initialize a WP-API resource with something other than a Hash')
|
9
|
-
end
|
10
|
-
@resource = resource
|
11
|
-
@api = api
|
12
|
-
end
|
13
|
-
|
14
|
-
def links
|
15
|
-
resource["_links"]
|
16
|
-
end
|
17
|
-
|
18
|
-
def embedded
|
19
|
-
resource["_embedded"]
|
20
|
-
end
|
21
|
-
|
22
|
-
alias :embedded? :embedded
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|