module Ecoportal module API class V2 # @attr_reader client [Common::Client] a `Common::Client` object that holds the configuration of the api connection. class Pages extend Common::BaseClass include Common::Content::DocHelpers class_resolver :page_class, "Ecoportal::API::V2::Page" attr_reader :client # @param client [Common::Client] a `Common::Client` object that holds the configuration of the api connection. # @return [Schemas] an instance object ready to make schema api requests. def initialize(client) @client = client end # Gets a page via api. # @note if the request has `success?` the returned `object.result` gives an object with that `Page`. # @param doc [String, Hash, Page] data containing an `id` of the target page. # @return [Ecoportal::API::V2::Page] the target page. def get(doc) id = get_id(doc) response = client.get("/pages/#{CGI.escape(id)}") Common::Content::WrappedResponse.new(response, page_class).result end # Gets a `new` non-existing page via api with all the ids initialized. # @param from [String, Hash, Page] template or `id` of the template # @return [Ecoportal::API::V2::Page] the new page object. def get_new(from) id = get_id(from) response = client.get("/pages/new", params: {template_id: id}) # TODO: make it so the obtained `doc` can be used as `doc`, yet `source_doc` to be empty {} #Common::Content::WrappedResponse.new(response, page_class).result end # Requests a creation of a page via api. # @param doc [Hash, Page] data that at least contains an `id` (internal or external) of the target page. # @param from [String, Hash, Page] template or `id` of the template # @return [Response] an object with the api response. def create(doc, from:) body = get_body(doc) id = get_id(from) client.post("/pages", data: body, params: {template_id: id}) end # Requests to update an existing page via api. # @param doc [Hash, Page] data that at least contains an `id` (internal or external) of the target page. # @return [Response] an object with the api response. def update(doc) body = get_body(doc) # , level: "page" id = get_id(doc) client.patch("/pages/#{CGI.escape(id)}", data: body) end end end end end require 'ecoportal/api/v2/page'