lib/ecoportal/api/v2/pages.rb in ecoportal-api-oozes-0.5.5 vs lib/ecoportal/api/v2/pages.rb in ecoportal-api-oozes-0.5.6

- old
+ new

@@ -1,41 +1,62 @@ 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\/(?<sid>.*)/ extend Common::BaseClass include Common::Content::DocHelpers + class_resolver :stages_class, "Ecoportal::API::V2::Pages::Stages" 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 + # 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`. + # @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 doc [String, Hash, Page] data containing an `id` of the target page. - # @return [Ecoportal::API::V2::Page] the target page. + # @return [Ecoportal::API::V2::Page, Ecoportal::API::V2::Pages::PageStage] 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 + pid = get_id(doc) + response = client.get("/pages/#{CGI.escape(pid)}") + wrapped = Common::Content::WrappedResponse.new(response, page_class) + + return wrapped.result if wrapped.success? + if (response.status == 302) && (url = response.body["data"]) + if sid = url_to_stage_id(url) + return stages.get(pid: pid, sid: sid) + end + end + raise "Could not get page #{id} - Error #{response.status}: #{response.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}) - # 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 + wrapped = Common::Content::WrappedResponse.new(response, page_class) + + return wrapped.result if wrapped.success? + #return page_class.create(response.body["data"]) if response.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 @@ -53,11 +74,18 @@ body = get_body(doc) # , level: "page" id = get_id(doc) client.patch("/pages/#{CGI.escape(id)}", data: 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'