class Ecoportal::API::V2::Pages
@attr_reader client [Common::Client] a `Common::Client` object that holds the configuration of the api connection.
Constants
- STAGE_REX
Attributes
Public Class Methods
@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.
# File lib/ecoportal/api/v2/pages.rb, line 17 def initialize(client) @client = client end
Public Instance Methods
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.
# File lib/ecoportal/api/v2/pages.rb, line 77 def create(doc, from:) body = get_body(doc) id = get_id(from) client.post("/pages", data: body, params: {template_id: id}) 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.
# File lib/ecoportal/api/v2/pages.rb, line 34 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? if (response.status == 302) && (url = response.body["data"]) if stage_id = url_to_stage_id(url) return stages.get(id: id, stage_id: stage_id) 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.
# File lib/ecoportal/api/v2/pages.rb, line 64 def get_new(from) id = get_id(from) response = client.get("/pages/new", params: {template_id: id}) wrapped = Common::Content::WrappedResponse.new(response, page_class) return wrapped.result if wrapped.success? raise "Could not get new page from template #{id} - Error #{response.status}: #{response.body}" end
Obtain specific object for pages api requests. @return [V2::Pages::Stages] an instance object ready to make pages api requests.
# File lib/ecoportal/api/v2/pages.rb, line 23 def stages stages_class.new(client) 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 [Response] an object with the api response.
# File lib/ecoportal/api/v2/pages.rb, line 53 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
Private Instance Methods
# File lib/ecoportal/api/v2/pages.rb, line 85 def url_to_stage_id(url) (matches = url.match(STAGE_REX)) && matches[:sid] end