Class: Wordpress::Json::Api::Client
- Inherits:
-
Object
- Object
- Wordpress::Json::Api::Client
- Defined in:
- lib/wordpress/json/api/client.rb
Instance Attribute Summary collapse
-
#configuration ⇒ Object
Returns the value of attribute configuration.
-
#connection ⇒ Object
Returns the value of attribute connection.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
- #all(path, params: {}, headers: {}) ⇒ Object
- #get(path, params: {}, headers: {}) ⇒ Object
-
#initialize(url, configuration: ::Wordpress::Json::Api.configuration, options: {}) ⇒ Client
constructor
A new instance of Client.
- #process_response(resp) ⇒ Object
- #request(path, params: {}, headers: {}, retries: 3) ⇒ Object
- #set_connection ⇒ Object
- #set_headers ⇒ Object
- #set_url(url) ⇒ Object
- #set_user_agent ⇒ Object
Constructor Details
#initialize(url, configuration: ::Wordpress::Json::Api.configuration, options: {}) ⇒ Client
Returns a new instance of Client.
9 10 11 12 13 14 |
# File 'lib/wordpress/json/api/client.rb', line 9 def initialize(url, configuration: ::Wordpress::Json::Api.configuration, options: {}) self.configuration = configuration set_url(url) set_headers set_connection end |
Instance Attribute Details
#configuration ⇒ Object
Returns the value of attribute configuration.
7 8 9 |
# File 'lib/wordpress/json/api/client.rb', line 7 def configuration @configuration end |
#connection ⇒ Object
Returns the value of attribute connection.
7 8 9 |
# File 'lib/wordpress/json/api/client.rb', line 7 def connection @connection end |
#headers ⇒ Object
Returns the value of attribute headers.
7 8 9 |
# File 'lib/wordpress/json/api/client.rb', line 7 def headers @headers end |
#url ⇒ Object
Returns the value of attribute url.
7 8 9 |
# File 'lib/wordpress/json/api/client.rb', line 7 def url @url end |
Instance Method Details
#all(path, params: {}, headers: {}) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/wordpress/json/api/client.rb', line 76 def all(path, params: {}, headers: {}) page = 1 params.merge!(per_page: 100) responses = [] continue = false begin params.merge!(page: page) begin resp = request(path, params: params, headers: headers) body = resp&.fetch(:body, nil) resp_headers = resp&.fetch(:headers, {}) total_pages = resp_headers&.fetch('x-wp-totalpages', 0)&.to_i if body && body.is_a?(Array) && body.any? responses = responses | body page += 1 end continue = (!total_pages.nil? && page <= total_pages) rescue ::Wordpress::Json::Api::Error => exception continue = false end end while continue return responses end |
#get(path, params: {}, headers: {}) ⇒ Object
54 55 56 |
# File 'lib/wordpress/json/api/client.rb', line 54 def get(path, params: {}, headers: {}) request(path, params: params, headers: headers)&.fetch(:body, nil) end |
#process_response(resp) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/wordpress/json/api/client.rb', line 105 def process_response(resp) if resp.success? body = resp&.body error_code = (body && body.is_a?(Hash) && !body.fetch('code', nil).to_s.empty?) ? body.fetch('code', nil).to_s : nil unless error_code.to_s.empty? raise ::Wordpress::Json::Api::Error, error_code end headers = resp&.env&.response_headers return {body: body, headers: headers} else raise ::Wordpress::Json::Api::Error, "Failed to send request to #{self.url}" end end |
#request(path, params: {}, headers: {}, retries: 3) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/wordpress/json/api/client.rb', line 58 def request(path, params: {}, headers: {}, retries: 3) response = nil begin resp = self.connection.get(path) do |request| request.headers = connection.headers.merge(headers) if headers && !headers.empty? request.params = params if params && !params.empty? end response = process_response(resp) rescue => exception retries -= 1 retry if retries > 0 end return response end |
#set_connection ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/wordpress/json/api/client.rb', line 39 def set_connection self.connection = ::Faraday.new(url) do |builder| builder.[:timeout] = configuration.faraday.fetch(:timeout, nil) if configuration.faraday.fetch(:timeout, nil) builder.[:open_timeout] = configuration.faraday.fetch(:open_timeout, nil) if configuration.faraday.fetch(:open_timeout, nil) builder.headers = self.headers if self.headers && !self.headers.empty? builder.request :json builder.response :logger, ::Logger.new(STDOUT), bodies: true if configuration.verbose builder.response :json, content_type: /\bjson$/ builder.use ::FaradayMiddleware::FollowRedirects, limit: 3 builder.adapter configuration.faraday.fetch(:adapter, ::Faraday.default_adapter) end end |
#set_headers ⇒ Object
20 21 22 23 |
# File 'lib/wordpress/json/api/client.rb', line 20 def set_headers self.headers ||= {} set_user_agent end |
#set_url(url) ⇒ Object
16 17 18 |
# File 'lib/wordpress/json/api/client.rb', line 16 def set_url(url) self.url = url.include?("/wp-json/wp/v#{self.configuration.version}/") ? url : "#{url.gsub(/\/$/i, '')}/wp-json/wp/v#{self.configuration.version}/" end |
#set_user_agent ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/wordpress/json/api/client.rb', line 25 def set_user_agent user_agent = self.configuration.faraday.fetch(:user_agent, nil) if user_agent if user_agent.is_a?(String) self.headers["User-Agent"] = user_agent elsif user_agent.is_a?(Array) self.headers["User-Agent"] = user_agent.sample elsif user_agent.is_a?(Proc) self.headers["User-Agent"] = user_agent.call end end end |