Class: Ecoportal::API::V2::Pages

Inherits:
Object
  • Object
show all
Extended by:
Common::BaseClass
Includes:
Common::Content::DocHelpers
Defined in:
lib/ecoportal/api/v2/pages.rb,
lib/ecoportal/api/v2/pages/stages.rb,
lib/ecoportal/api/v2/pages/page_stage.rb,
lib/ecoportal/api/v2/pages/page_create_response.rb

Defined Under Namespace

Classes: PageCreateResponse, PageStage, Stages

Constant Summary collapse

STAGE_REX =
/stages\/(?<sid>.*)/

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Common::Content::DocHelpers

#array_id_index, #array_id_item, #array_ids, #get_body, #get_id

Constructor Details

#initialize(client) ⇒ Schemas

Returns an instance object ready to make schema api requests.

Parameters:

  • client (Common::Client)

    a Common::Client object that holds the configuration of the api connection.



19
20
21
# File 'lib/ecoportal/api/v2/pages.rb', line 19

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientCommon::Client (readonly)

a Common::Client object that holds the configuration of the api connection.

Returns:

  • (Common::Client)

    the current value of client



5
6
7
# File 'lib/ecoportal/api/v2/pages.rb', line 5

def client
  @client
end

Instance Method Details

#create(doc, from:) ⇒ Response

Requests a creation of a page via api.

Parameters:

  • doc (Hash, Page)

    data that at least contains an id (internal or external) of the target page.

  • from (String, Hash, Page)

    template or id of the template

Returns:

  • (Response)

    an object with the api response.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ecoportal/api/v2/pages.rb', line 79

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

#get(id, stage_id: nil) ⇒ Ecoportal::API::V2::Page, Ecoportal::API::V2::Pages::PageStage

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.

Gets a page via api.

Parameters:

  • id (String, Hash, Stage)

    the id of the target page.

  • stage_id (String) (defaults to: nil)

    the id of the target stage.

Returns:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ecoportal/api/v2/pages.rb', line 36

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

#get_new(from) ⇒ Ecoportal::API::V2::Page

Gets a new non-existing page via api with all the ids initialized.

Parameters:

  • from (String, Hash, Page)

    template or id of the template

Returns:



66
67
68
69
70
71
72
73
# File 'lib/ecoportal/api/v2/pages.rb', line 66

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

#stagesV2::Pages::Stages

Obtain specific object for pages api requests.

Returns:



25
26
27
# File 'lib/ecoportal/api/v2/pages.rb', line 25

def stages
  stages_class.new(client)
end

#update(doc) ⇒ Ecoportal::API::Common::Response

Note:

It won't launch the update unless there are changes

Requests to update an existing page via api.

Parameters:

  • doc (Hash, Page)

    data that at least contains an id (internal or external) of the target page.

Returns:

  • (Ecoportal::API::Common::Response)

    an object with the api response.



55
56
57
58
59
60
61
# File 'lib/ecoportal/api/v2/pages.rb', line 55

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