wordpress-json-api 0.1.1 → 0.1.2

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
  SHA256:
3
- metadata.gz: 24188f2c4b40c0c7efbe73fe75a26ac862a5324e15d1dfca1e3f4c7dfcc59d3a
4
- data.tar.gz: b8a5740cb11a5954087836dd3f420131db2084af64b31c7cb458b37bc55c7b62
3
+ metadata.gz: b16ed9bae362513e88acab5b6def2a44d6e24f4671762d1f3b6714ebe63bcb1e
4
+ data.tar.gz: 5b6288b9f0c8b0595429b4f395267989de933f91ca0515abcffe6f51035d8b7d
5
5
  SHA512:
6
- metadata.gz: f0b3c0d1e9b312654fcec8664ef2a63e7881c267d059c3fcc8baa47adb8d44879b34f561fc5a62f2870a8474261956d599d194fb7e35cd08d572a09b7a06bb6c
7
- data.tar.gz: df859d2f8e9141c4924d69d5565cb8705077050d469eb2a73d182861b7fd928584a144e8463d70a41152cb5b89dce4243a64c2a172d577b1586007924b55f79b
6
+ metadata.gz: e4f0a38a8ecb5e334ccd88a03d72d02452f41c8d3031496be691b7153fc5b8df168458c443f14d2d7ad74541b6baffb2ee2e04194e625b18148c2212ae75ffe5
7
+ data.tar.gz: d09dc0344f3b1a4a7c0088d5e2f9039f961ca1405613d5988d5c44714bc41056a8289811b0f539d95021a507c6619570082d773830880cb0247a130894124831
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- wordpress-json-api (0.1.0)
4
+ wordpress-json-api (0.1.1)
5
5
  faraday (>= 1.1.0)
6
6
  faraday_middleware (>= 1.0.0)
7
7
 
@@ -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
- builder.options[:timeout] = configuration.faraday.fetch(:timeout, nil)
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
- resp = connection.get(path) do |request|
46
- if headers && !headers.empty?
47
- request.headers = connection.headers.merge(headers)
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
- request.params = params if params && !params.empty?
66
+
67
+ resp = response(resp)
68
+ rescue => exception
69
+ retries -= 1
70
+ retry if retries > 0
50
71
  end
51
72
 
52
- response(resp)
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
- resp = resp&.body
107
+ body = resp&.body
58
108
 
59
- error_code = (resp && resp.is_a?(Hash) && !resp.fetch('code', nil).to_s.empty?) ? resp.fetch('code', nil).to_s : nil
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
- resp
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
@@ -12,8 +12,9 @@ module Wordpress
12
12
 
13
13
  self.faraday = {
14
14
  adapter: :net_http,
15
- timeout: 120,
16
- open_timeout: 60
15
+ timeout: 60,
16
+ open_timeout: 30,
17
+ #user_agent: 'ENTER_A_CUSTOM_USER_AGENT_HERE'
17
18
  }
18
19
 
19
20
  self.verbose = false
@@ -1,7 +1,7 @@
1
1
  module Wordpress
2
2
  module Json
3
3
  module Api
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.2"
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wordpress-json-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sebastian Johnsson