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 STAGE_REX = /stages\/(?.*)/.freeze extend Common::BaseClass include Common::Content::DocHelpers class_resolver :stages_class, "Ecoportal::API::V2::Pages::Stages" class_resolver :page_class, "Ecoportal::API::V2::Page" class_resolver :page_stage_class, "Ecoportal::API::V2::Pages::PageStage" class_resolver :create_page_response_class, "Ecoportal::API::V2::Pages::PageCreateResponse" 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 # Obtain specific object for pages api requests. # @return [V2::Pages::Stages] an instance object ready to make pages api requests. def stages stages_class.new(client) end # Gets a page via api. # @note # - if the request has `success?` the returned `object.result` gives an object with that `Page`. # - if it failed to obtain the full page, it returns a `PageStage` with the active stage data. # @param id [String, Hash, Stage] the `id` of the target **page**. # @param stage_id [String] the `id` of the target **stage**. # @return [Ecoportal::API::V2::Page, Ecoportal::API::V2::Pages::PageStage] the target page. def get(id, stage_id: nil) return stages.get(id: id, stage_id: stage_id) if stage_id id = get_id(id) response = client.get("/pages/#{CGI.escape(id)}") wrapped = Common::Content::WrappedResponse.new(response, page_class) return wrapped.result if wrapped.success? url = nil url = response.body["data"] if response.status == 302 stage_id = url_to_stage_id(url) unless url.nil? return stages.get(id: id, stage_id: stage_id) if stage_id raise "Could not get page #{id} - Error #{response.status}: #{response.body}" end # Requests to update an existing page via api. # @note It won't launch the update unless there are changes # @param doc [Hash, Page] data that at least contains an `id` (internal or external) of the target page. # @return [Ecoportal::API::Common::Response] an object with the api response. def update(doc) body = get_body(doc) # , level: "page" # Launch only if there are changes raise "Missing page object" unless body && body["page"] id = get_id(doc) client.patch("/pages/#{CGI.escape(id)}", data: body) 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}) wrapped = Common::Content::WrappedResponse.new(response, page_stage_class) return wrapped.result if wrapped.success? raise "Could not get new page from template #{id} - Error #{response.status}: #{response.body}" 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:) id = get_id(from) body = get_body(doc).tap do |hash| unless hash["page"] hash["page"] = { "id" => "111111111111111111111111", "operation" => "changed", "data" => {"patch_ver" => 0} } end end response = client.post("/pages", data: body, params: {template_id: id}) wrapped = Common::Content::WrappedResponse.new(response, create_page_response_class) return wrapped.result if wrapped.success? raise "Could not create page from template #{id} - Error #{response.status}: #{response.body}" end private def url_to_stage_id(url) (matches = url.match(STAGE_REX)) && matches[:sid] end end end end end require 'ecoportal/api/v2/page' require 'ecoportal/api/v2/pages/stages' require 'ecoportal/api/v2/pages/page_create_response'