class WordpressClient::Connection
@private
Attributes
url[R]
username[R]
Public Class Methods
new(url:, username:, password:)
click to toggle source
# File lib/wordpress_client/connection.rb, line 9 def initialize(url:, username:, password:) @url = url @username = username @password = password end
Public Instance Methods
create(model, path, attributes, redirect_params: {})
click to toggle source
# File lib/wordpress_client/connection.rb, line 25 def create(model, path, attributes, redirect_params: {}) response = send_json(path, attributes) if response.status == 201 # Created model.parse(get_json(response.headers.fetch("location"), redirect_params)) else handle_status_code(response) model.parse(parse_json_response(response)) end end
create_without_response(path, attributes)
click to toggle source
# File lib/wordpress_client/connection.rb, line 36 def create_without_response(path, attributes) response = send_json(path, attributes) if response.status == 201 # Created true else handle_status_code(response) true end end
delete(path, attributes = {})
click to toggle source
# File lib/wordpress_client/connection.rb, line 47 def delete(path, attributes = {}) response = send_json(path, attributes, method: :delete) handle_status_code(response) true end
get(model, path, params = {})
click to toggle source
# File lib/wordpress_client/connection.rb, line 15 def get(model, path, params = {}) model.parse(get_json(path, params)) end
get_multiple(model, path, params = {})
click to toggle source
# File lib/wordpress_client/connection.rb, line 19 def get_multiple(model, path, params = {}) data, response = get_json_and_response(path, params) models = data.map { |model_data| model.parse(model_data) } wrap_paginated_collection(response, models, params) end
inspect()
click to toggle source
# File lib/wordpress_client/connection.rb, line 80 def inspect "#<#{self.class.name} #@username @ #@url>" end
put(model, path, attributes)
click to toggle source
# File lib/wordpress_client/connection.rb, line 53 def put(model, path, attributes) model.parse( parse_json_response(send_json(path, attributes, method: :put)) ) end
put_without_response(path, attributes)
click to toggle source
# File lib/wordpress_client/connection.rb, line 59 def put_without_response(path, attributes) handle_status_code(send_json(path, attributes, method: :put)) true end
upload(model, path, io, mime_type:, filename:)
click to toggle source
# File lib/wordpress_client/connection.rb, line 64 def upload(model, path, io, mime_type:, filename:) body = io.read response = post_data(path, body, { "Content-Length" => body.size.to_s, "Content-Type" => mime_type, "Content-Disposition" => 'attachment; filename="' + (filename || "unnamed") + '"', }) if response.status == 201 # Created model.parse(get_json(response.headers.fetch("location"))) else handle_status_code(response) model.parse(parse_json_response(response)) end end
Private Instance Methods
bad_request_details(response)
click to toggle source
# File lib/wordpress_client/connection.rb, line 179 def bad_request_details(response) details = JSON.parse(response.body) [details["code"], details["message"]] rescue [nil, "Bad Request"] end
get_json(path, params = {})
click to toggle source
# File lib/wordpress_client/connection.rb, line 106 def get_json(path, params = {}) get_json_and_response(path, params).first end
get_json_and_response(path, params = {})
click to toggle source
# File lib/wordpress_client/connection.rb, line 110 def get_json_and_response(path, params = {}) response = net.get(path, params) [parse_json_response(response), response] rescue Faraday::ConnectionFailed => error raise TimeoutError if error.cause.class == Net::OpenTimeout raise end
handle_bad_request(response)
click to toggle source
# File lib/wordpress_client/connection.rb, line 170 def handle_bad_request(response) code, message = bad_request_details(response) if code == "rest_post_invalid_id" raise NotFoundError, "Post ID is not found" else raise ValidationError, message end end
handle_status_code(response)
click to toggle source
# File lib/wordpress_client/connection.rb, line 157 def handle_status_code(response) case response.status when 200 return when 404 raise NotFoundError, "Could not find resource" when 400 handle_bad_request(response) else raise ServerError, "Server returned status code #{response.status}: #{response.body}" end end
net()
click to toggle source
# File lib/wordpress_client/connection.rb, line 85 def net @net ||= setup_network_connection end
parse_json_response(response)
click to toggle source
# File lib/wordpress_client/connection.rb, line 143 def parse_json_response(response) handle_status_code(response) content_type = response.headers["content-type"].split(";").first unless content_type == "application/json" raise ServerError, "Got content type #{content_type}" end JSON.parse(response.body) rescue JSON::ParserError => error raise ServerError, "Could not parse JSON response: #{error}" end
post_data(path, data, headers)
click to toggle source
# File lib/wordpress_client/connection.rb, line 133 def post_data(path, data, headers) net.post do |request| request.url path request.headers = headers request.body = data end rescue Faraday::TimeoutError raise TimeoutError end
send_json(path, data, method: :post)
click to toggle source
# File lib/wordpress_client/connection.rb, line 118 def send_json(path, data, method: :post) unless %i[get post put patch delete].include? method raise ArgumentError, "Invalid method: #{method.inspect}" end net.public_send(method) do |request| json = data.to_json request.url path request.headers["Content-Type"] = "application/json; charset=#{json.encoding}" request.body = json end rescue Faraday::TimeoutError raise TimeoutError end
setup_network_connection()
click to toggle source
# File lib/wordpress_client/connection.rb, line 89 def setup_network_connection Faraday.new(url: File.join(url, "wp/v2")) do |conn| conn.request :basic_auth, username, @password conn.adapter :net_http end end
wrap_paginated_collection(response, entries, params)
click to toggle source
# File lib/wordpress_client/connection.rb, line 96 def wrap_paginated_collection(response, entries, params) total = response.headers.fetch("x-wp-total").to_i current_page = params.fetch(:page).to_i per_page = params.fetch(:per_page).to_i PaginatedCollection.new( entries, total: total, current_page: current_page, per_page: per_page ) end