wordpress-json-api 0.1.1 → 0.1.2
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/Gemfile.lock +1 -1
- data/lib/wordpress/json/api/client.rb +76 -25
- data/lib/wordpress/json/api/configuration.rb +3 -2
- data/lib/wordpress/json/api/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b16ed9bae362513e88acab5b6def2a44d6e24f4671762d1f3b6714ebe63bcb1e
|
4
|
+
data.tar.gz: 5b6288b9f0c8b0595429b4f395267989de933f91ca0515abcffe6f51035d8b7d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e4f0a38a8ecb5e334ccd88a03d72d02452f41c8d3031496be691b7153fc5b8df168458c443f14d2d7ad74541b6baffb2ee2e04194e625b18148c2212ae75ffe5
|
7
|
+
data.tar.gz: d09dc0344f3b1a4a7c0088d5e2f9039f961ca1405613d5988d5c44714bc41056a8289811b0f539d95021a507c6619570082d773830880cb0247a130894124831
|
data/Gemfile.lock
CHANGED
@@ -3,72 +3,123 @@
|
|
3
3
|
module Wordpress
|
4
4
|
module Json
|
5
5
|
module Api
|
6
|
-
|
7
6
|
class Client
|
8
7
|
attr_accessor :url, :configuration, :connection, :headers
|
9
8
|
|
10
9
|
def initialize(url, configuration: ::Wordpress::Json::Api.configuration, options: {})
|
11
10
|
self.configuration = configuration
|
12
11
|
set_url(url)
|
12
|
+
set_headers
|
13
13
|
set_connection
|
14
14
|
end
|
15
15
|
|
16
16
|
def set_url(url)
|
17
17
|
self.url = url.include?("/wp-json/wp/v#{self.configuration.version}/") ? url : "#{url.gsub(/\/$/i, '')}/wp-json/wp/v#{self.configuration.version}/"
|
18
18
|
end
|
19
|
+
|
20
|
+
def set_headers
|
21
|
+
self.headers ||= {}
|
22
|
+
set_user_agent
|
23
|
+
end
|
24
|
+
|
25
|
+
def set_user_agent
|
26
|
+
user_agent = self.configuration.faraday.fetch(:user_agent, nil)
|
27
|
+
|
28
|
+
if user_agent
|
29
|
+
if user_agent.is_a?(String)
|
30
|
+
self.headers["User-Agent"] = user_agent
|
31
|
+
elsif user_agent.is_a?(Array)
|
32
|
+
self.headers["User-Agent"] = user_agent.sample
|
33
|
+
elsif user_agent.is_a?(Proc)
|
34
|
+
self.headers["User-Agent"] = user_agent.call
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
19
38
|
|
20
39
|
def set_connection
|
21
40
|
self.connection = ::Faraday.new(url) do |builder|
|
22
|
-
if configuration.faraday.fetch(:timeout, nil)
|
23
|
-
|
24
|
-
end
|
25
|
-
if configuration.faraday.fetch(:open_timeout, nil)
|
26
|
-
builder.options[:open_timeout] = configuration.faraday.fetch(:open_timeout, nil)
|
27
|
-
end
|
28
|
-
|
29
|
-
builder.headers = headers if headers && !headers.empty?
|
41
|
+
builder.options[:timeout] = configuration.faraday.fetch(:timeout, nil) if configuration.faraday.fetch(:timeout, nil)
|
42
|
+
builder.options[:open_timeout] = configuration.faraday.fetch(:open_timeout, nil) if configuration.faraday.fetch(:open_timeout, nil)
|
30
43
|
|
44
|
+
builder.headers = self.headers if self.headers && !self.headers.empty?
|
31
45
|
builder.request :json
|
32
|
-
|
33
|
-
if configuration.verbose
|
34
|
-
builder.response :logger, ::Logger.new(STDOUT), bodies: true
|
35
|
-
end
|
46
|
+
builder.response :logger, ::Logger.new(STDOUT), bodies: true if configuration.verbose
|
36
47
|
builder.response :json, content_type: /\bjson$/
|
37
|
-
|
38
|
-
builder.use ::FaradayMiddleware::FollowRedirects, limit: 10
|
48
|
+
builder.use ::FaradayMiddleware::FollowRedirects, limit: 3
|
39
49
|
|
40
50
|
builder.adapter configuration.faraday.fetch(:adapter, ::Faraday.default_adapter)
|
41
51
|
end
|
42
52
|
end
|
43
53
|
|
44
|
-
def get(path, params: {})
|
45
|
-
|
46
|
-
|
47
|
-
|
54
|
+
def get(path, params: {}, headers: {})
|
55
|
+
request(path, params: params, headers: headers)&.fetch(:body, nil)
|
56
|
+
end
|
57
|
+
|
58
|
+
def request(path, params: {}, headers: {}, retries: 3)
|
59
|
+
resp = nil
|
60
|
+
|
61
|
+
begin
|
62
|
+
resp = self.connection.get(path) do |request|
|
63
|
+
request.headers = connection.headers.merge(headers) if headers && !headers.empty?
|
64
|
+
request.params = params if params && !params.empty?
|
48
65
|
end
|
49
|
-
|
66
|
+
|
67
|
+
resp = response(resp)
|
68
|
+
rescue => exception
|
69
|
+
retries -= 1
|
70
|
+
retry if retries > 0
|
50
71
|
end
|
51
72
|
|
52
|
-
|
73
|
+
return resp
|
74
|
+
end
|
75
|
+
|
76
|
+
def all(path, params: {}, headers: {})
|
77
|
+
page = 1
|
78
|
+
params.merge!(per_page: 100)
|
79
|
+
responses = []
|
80
|
+
continue = false
|
81
|
+
|
82
|
+
begin
|
83
|
+
params.merge!(page: page)
|
84
|
+
|
85
|
+
begin
|
86
|
+
resp = request(path, params: params, headers: headers)
|
87
|
+
body = resp&.fetch(:body, nil)
|
88
|
+
resp_headers = resp&.fetch(:headers, {})
|
89
|
+
total_pages = resp_headers&.fetch('x-wp-totalpages', 0)&.to_i
|
90
|
+
|
91
|
+
if body && body.is_a?(Array) && body.any?
|
92
|
+
responses = responses | body
|
93
|
+
page += 1
|
94
|
+
end
|
95
|
+
|
96
|
+
continue = (!total_pages.nil? && page <= total_pages)
|
97
|
+
rescue ::Wordpress::Json::Api::Error => exception
|
98
|
+
continue = false
|
99
|
+
end
|
100
|
+
end while continue
|
101
|
+
|
102
|
+
return responses
|
53
103
|
end
|
54
104
|
|
55
105
|
def response(resp)
|
56
106
|
if resp.success?
|
57
|
-
|
107
|
+
body = resp&.body
|
58
108
|
|
59
|
-
error_code = (
|
109
|
+
error_code = (body && body.is_a?(Hash) && !body.fetch('code', nil).to_s.empty?) ? body.fetch('code', nil).to_s : nil
|
60
110
|
unless error_code.to_s.empty?
|
61
111
|
raise ::Wordpress::Json::Api::Error, error_code
|
62
112
|
end
|
113
|
+
|
114
|
+
headers = resp&.env&.response_headers
|
63
115
|
|
64
|
-
|
116
|
+
return {body: body, headers: headers}
|
65
117
|
else
|
66
118
|
raise ::Wordpress::Json::Api::Error, "Failed to send request to #{self.url}"
|
67
119
|
end
|
68
120
|
end
|
69
121
|
|
70
122
|
end
|
71
|
-
|
72
123
|
end
|
73
124
|
end
|
74
125
|
end
|