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
- #get(path, params: {}) ⇒ Object
-
#initialize(url, configuration: ::Wordpress::Json::Api.configuration, options: {}) ⇒ Client
constructor
A new instance of Client.
- #response(resp) ⇒ Object
- #set_connection ⇒ Object
- #set_url(url) ⇒ Object
Constructor Details
#initialize(url, configuration: ::Wordpress::Json::Api.configuration, options: {}) ⇒ Client
Returns a new instance of Client.
10 11 12 13 14 |
# File 'lib/wordpress/json/api/client.rb', line 10 def initialize(url, configuration: ::Wordpress::Json::Api.configuration, options: {}) self.configuration = configuration set_url(url) set_connection end |
Instance Attribute Details
#configuration ⇒ Object
Returns the value of attribute configuration.
8 9 10 |
# File 'lib/wordpress/json/api/client.rb', line 8 def configuration @configuration end |
#connection ⇒ Object
Returns the value of attribute connection.
8 9 10 |
# File 'lib/wordpress/json/api/client.rb', line 8 def connection @connection end |
#headers ⇒ Object
Returns the value of attribute headers.
8 9 10 |
# File 'lib/wordpress/json/api/client.rb', line 8 def headers @headers end |
#url ⇒ Object
Returns the value of attribute url.
8 9 10 |
# File 'lib/wordpress/json/api/client.rb', line 8 def url @url end |
Instance Method Details
#get(path, params: {}) ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/wordpress/json/api/client.rb', line 44 def get(path, params: {}) resp = connection.get(path) do |request| if headers && !headers.empty? request.headers = connection.headers.merge(headers) end request.params = params if params && !params.empty? end response(resp) end |
#response(resp) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/wordpress/json/api/client.rb', line 55 def response(resp) if resp.success? resp = resp&.body error_code = (resp && resp.is_a?(Hash) && !resp.fetch('code', nil).to_s.empty?) ? resp.fetch('code', nil).to_s : nil unless error_code.to_s.empty? raise ::Wordpress::Json::Api::Error, error_code end resp else raise ::Wordpress::Json::Api::Error, "Failed to send request to #{self.url}" end end |
#set_connection ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/wordpress/json/api/client.rb', line 20 def set_connection self.connection = ::Faraday.new(url) do |builder| if configuration.faraday.fetch(:timeout, nil) builder.[:timeout] = configuration.faraday.fetch(:timeout, nil) end if configuration.faraday.fetch(:open_timeout, nil) builder.[:open_timeout] = configuration.faraday.fetch(:open_timeout, nil) end builder.headers = headers if headers && !headers.empty? builder.request :json if configuration.verbose builder.response :logger, ::Logger.new(STDOUT), bodies: true end builder.response :json, content_type: /\bjson$/ builder.use ::FaradayMiddleware::FollowRedirects, limit: 10 builder.adapter configuration.faraday.fetch(:adapter, ::Faraday.default_adapter) end 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 |