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

client[R]

Public Class Methods

new(client) click to toggle source

@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

create(doc, from:) click to toggle source

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 74
def create(doc, from:)
  body   = get_body(doc)
  id     = get_id(from)
  client.post("/pages", data: body, params: {template_id: id})
end
get(id, stage_id: nil) click to toggle source

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(pid: id, sid: 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(pid: id, sid: stage_id)
    end
  end
  raise "Could not get page #{pid} - Error #{response.status}: #{response.body}"
end
get_new(from) click to toggle source

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 61
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
stages() click to toggle source

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
update(doc) click to toggle source

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.

# File lib/ecoportal/api/v2/pages.rb, line 52
def update(doc)
  body = get_body(doc) # , level: "page"
  id   = get_id(doc)
  client.patch("/pages/#{CGI.escape(id)}", data: body)
end

Private Instance Methods

url_to_stage_id(url) click to toggle source
# File lib/ecoportal/api/v2/pages.rb, line 82
def url_to_stage_id(url)
  (matches = url.match(STAGE_REX)) && matches[:sid]
end