wp-api-client 0.2.3 → 0.3.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/lib/wp_api_client.rb +14 -0
- data/lib/wp_api_client/collection.rb +5 -1
- data/lib/wp_api_client/configuration.rb +1 -0
- data/lib/wp_api_client/connection.rb +4 -0
- data/lib/wp_api_client/entities/base.rb +7 -2
- data/lib/wp_api_client/entities/error.rb +14 -0
- data/lib/wp_api_client/entities/meta.rb +0 -7
- data/lib/wp_api_client/entities/post.rb +0 -8
- data/lib/wp_api_client/entities/taxonomy.rb +0 -4
- data/lib/wp_api_client/entities/term.rb +0 -12
- data/lib/wp_api_client/entities/types.rb +2 -1
- data/lib/wp_api_client/relationship.rb +39 -13
- data/lib/wp_api_client/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: e6daac342406211d577fc07b8948ab96358589a8
|
4
|
+
data.tar.gz: 3df23ccd037feb4eef033e416a50942babf49672
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6f02a6f121e4cb8be779c49e4457fc9029f483955017a2e077e0a448bf1754646bb28fb31261a9b52f7ec0deed31d1eb001f7938b28da274937780cea001dca6
|
7
|
+
data.tar.gz: 70289be078d5a64ac1db18376cc79c22fea58f24d4a8acf400a7adf61da962c3ca535859e1a76d34c434705ac6527c6cbb5cd532b52db36dbdc2f924703d2d5b
|
data/lib/wp_api_client.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require "ostruct"
|
2
|
+
|
1
3
|
require "wp_api_client/version"
|
2
4
|
require "wp_api_client/configuration"
|
3
5
|
|
@@ -8,6 +10,7 @@ require "wp_api_client/entities/meta"
|
|
8
10
|
require "wp_api_client/entities/taxonomy"
|
9
11
|
require "wp_api_client/entities/term"
|
10
12
|
require "wp_api_client/entities/image"
|
13
|
+
require "wp_api_client/entities/error"
|
11
14
|
require "wp_api_client/entities/types"
|
12
15
|
|
13
16
|
require "wp_api_client/client"
|
@@ -17,6 +20,7 @@ require "wp_api_client/collection"
|
|
17
20
|
require "wp_api_client/relationship"
|
18
21
|
|
19
22
|
module WpApiClient
|
23
|
+
|
20
24
|
def self.get_client
|
21
25
|
@client ||= Client.new(Connection.new(configuration))
|
22
26
|
end
|
@@ -27,4 +31,14 @@ module WpApiClient
|
|
27
31
|
end
|
28
32
|
|
29
33
|
class RelationNotDefined < StandardError; end
|
34
|
+
class ErrorResponse < StandardError
|
35
|
+
|
36
|
+
attr_reader :error
|
37
|
+
attr_reader :status
|
38
|
+
|
39
|
+
def initialize(json)
|
40
|
+
@error = OpenStruct.new(json)
|
41
|
+
@status = @error.data["status"]
|
42
|
+
end
|
43
|
+
end
|
30
44
|
end
|
@@ -24,7 +24,11 @@ module WpApiClient
|
|
24
24
|
def previous_page
|
25
25
|
@links[:prev] && @links[:prev]
|
26
26
|
end
|
27
|
-
|
27
|
+
|
28
|
+
def method_missing(method, *args)
|
29
|
+
@resources.send(method, *args)
|
30
|
+
end
|
31
|
+
|
28
32
|
private
|
29
33
|
|
30
34
|
# https://www.snip2code.com/Snippet/71914/Parse-link-headers-from-Github-API-in-Ru
|
@@ -32,6 +32,10 @@ module WpApiClient
|
|
32
32
|
faraday.use :http_cache, store: configuration.cache, shared_cache: false
|
33
33
|
end
|
34
34
|
|
35
|
+
if configuration.proxy
|
36
|
+
faraday.proxy configuration.proxy
|
37
|
+
end
|
38
|
+
|
35
39
|
faraday.use Faraday::Response::RaiseError
|
36
40
|
faraday.response :json, :content_type => /\bjson$/
|
37
41
|
faraday.adapter :typhoeus
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'open-uri'
|
2
|
+
require 'ostruct'
|
2
3
|
|
3
4
|
module WpApiClient
|
4
5
|
module Entities
|
@@ -12,10 +13,10 @@ module WpApiClient
|
|
12
13
|
end
|
13
14
|
|
14
15
|
def initialize(resource)
|
15
|
-
unless resource.is_a? Hash
|
16
|
+
unless resource.is_a? Hash or resource.is_a? OpenStruct
|
16
17
|
raise ArgumentError.new('Tried to initialize a WP-API resource with something other than a Hash')
|
17
18
|
end
|
18
|
-
@resource = resource
|
19
|
+
@resource = OpenStruct.new(resource)
|
19
20
|
end
|
20
21
|
|
21
22
|
def links
|
@@ -31,6 +32,10 @@ module WpApiClient
|
|
31
32
|
relations
|
32
33
|
end
|
33
34
|
end
|
35
|
+
|
36
|
+
def method_missing(method, *args)
|
37
|
+
@resource.send(method, *args)
|
38
|
+
end
|
34
39
|
end
|
35
40
|
end
|
36
41
|
end
|
@@ -11,10 +11,6 @@ module WpApiClient
|
|
11
11
|
post["title"]["rendered"]
|
12
12
|
end
|
13
13
|
|
14
|
-
def slug
|
15
|
-
post["slug"]
|
16
|
-
end
|
17
|
-
|
18
14
|
def date
|
19
15
|
Time.parse(post["date_gmt"]) if post["date_gmt"]
|
20
16
|
end
|
@@ -27,10 +23,6 @@ module WpApiClient
|
|
27
23
|
post["excerpt"]["rendered"]
|
28
24
|
end
|
29
25
|
|
30
|
-
def id
|
31
|
-
post["id"]
|
32
|
-
end
|
33
|
-
|
34
26
|
def terms(taxonomy = nil)
|
35
27
|
relations("https://api.w.org/term", taxonomy)
|
36
28
|
end
|
@@ -20,7 +20,12 @@ module WpApiClient
|
|
20
20
|
"https://api.w.org/items" => :terms,
|
21
21
|
"http://api.w.org/v2/post_type" => :post_type,
|
22
22
|
"https://api.w.org/meta" => :meta,
|
23
|
-
"https://api.w.org/featuredmedia" => :post
|
23
|
+
"https://api.w.org/featuredmedia" => :post,
|
24
|
+
"wp:term" => :term,
|
25
|
+
"wp:items" => :terms,
|
26
|
+
"wp:post_type" => :post_type,
|
27
|
+
"wp:meta" => :meta,
|
28
|
+
"wp:featuredmedia" => :post
|
24
29
|
}
|
25
30
|
end
|
26
31
|
|
@@ -64,6 +69,9 @@ module WpApiClient
|
|
64
69
|
attr_reader :relation
|
65
70
|
|
66
71
|
def initialize(resource, relation)
|
72
|
+
if resource["_links"].keys.include? 'curies'
|
73
|
+
relation = convert_uri_to_curie(relation)
|
74
|
+
end
|
67
75
|
@resource = resource
|
68
76
|
@relation = relation
|
69
77
|
end
|
@@ -96,22 +104,40 @@ Available mappings are :post, :term, and :meta.}
|
|
96
104
|
def load_relation(relationship, position = nil)
|
97
105
|
if objects = @resource.dig("_embedded", relationship)
|
98
106
|
location = position ? objects[position] : objects
|
99
|
-
|
107
|
+
begin
|
108
|
+
WpApiClient::Collection.new(location)
|
109
|
+
rescue WpApiClient::ErrorResponse
|
110
|
+
load_from_links(relationship, position)
|
111
|
+
end
|
112
|
+
else
|
113
|
+
load_from_links(relationship, position)
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def load_from_links(relationship, position = nil)
|
118
|
+
unless position.nil?
|
119
|
+
location = @resource["_links"].dig(relationship, position.to_i, "href")
|
100
120
|
else
|
101
|
-
|
102
|
-
|
121
|
+
if @resource["_links"][relationship].is_a? Array
|
122
|
+
# If the resources are linked severally, crank through and
|
123
|
+
# retrieve them one by one as an array
|
124
|
+
return @resource["_links"][relationship].map { |link| WpApiClient.get_client.get(link["href"]) }
|
103
125
|
else
|
104
|
-
|
105
|
-
|
106
|
-
# retrieve them one by one as an array
|
107
|
-
return @resource["_links"][relationship].map { |link| WpApiClient.get_client.get(link["href"]) }
|
108
|
-
else
|
109
|
-
# Otherwise, get the single link to the lot
|
110
|
-
location = @resource["_links"][relationship]["href"]
|
111
|
-
end
|
126
|
+
# Otherwise, get the single link to the lot
|
127
|
+
location = @resource["_links"][relationship]["href"]
|
112
128
|
end
|
113
|
-
WpApiClient.get_client.get(location) if location
|
114
129
|
end
|
130
|
+
WpApiClient.get_client.get(location) if location
|
131
|
+
end
|
132
|
+
|
133
|
+
def convert_uri_to_curie(uri)
|
134
|
+
uri_curie_mappings = {
|
135
|
+
"https://api.w.org/term" => "wp:term",
|
136
|
+
"https://api.w.org/items" => "wp:items",
|
137
|
+
"https://api.w.org/meta" => "wp:meta",
|
138
|
+
"https://api.w.org/featuredmedia" => "wp:featuredmedia"
|
139
|
+
}
|
140
|
+
uri_curie_mappings.dig(uri) || uri
|
115
141
|
end
|
116
142
|
end
|
117
143
|
end
|
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.3.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-
|
11
|
+
date: 2016-04-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -160,6 +160,7 @@ files:
|
|
160
160
|
- lib/wp_api_client/configuration.rb
|
161
161
|
- lib/wp_api_client/connection.rb
|
162
162
|
- lib/wp_api_client/entities/base.rb
|
163
|
+
- lib/wp_api_client/entities/error.rb
|
163
164
|
- lib/wp_api_client/entities/image.rb
|
164
165
|
- lib/wp_api_client/entities/meta.rb
|
165
166
|
- lib/wp_api_client/entities/post.rb
|