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_stage/task.rb,
lib/ecoportal/api/v2/pages/page_stage/tasks.rb,
lib/ecoportal/api/v2/pages/page_create_response.rb

Defined Under Namespace

Classes: PageCreateResponse, PageStage, Stages

Constant Summary collapse

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

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 (Ecoportal::API::Common::Client)

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



21
22
23
# File 'lib/ecoportal/api/v2/pages.rb', line 21

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientEcoportal::API::Common::Client (readonly)

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

Returns:

  • (Ecoportal::API::Common::Client)

    the current value of client



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

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.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/ecoportal/api/v2/pages.rb', line 94

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  = Ecoportal::API::Common::Content::WrappedResponse.new(
    response,
    create_page_response_class
  )
  return wrapped.result if wrapped.success?

  msg  = "Could not create page from template #{id} "
  msg << "- Error #{response.status}: #{response.body}"
  raise msg
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:



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ecoportal/api/v2/pages.rb', line 38

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  = Ecoportal::API::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

#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:



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ecoportal/api/v2/pages.rb', line 78

def get_new(from)
  id       = get_id(from)
  response = client.get("/pages/new", params: {template_id: id})
  wrapped  = Ecoportal::API::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:



27
28
29
# File 'lib/ecoportal/api/v2/pages.rb', line 27

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.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ecoportal/api/v2/pages.rb', line 60

def update(doc)
  if doc.is_a?(Ecoportal::API::V2::Pages::PageStage)
    stage_id = doc.current_stage_id
    return stages.update(doc, stage_id: stage_id)
  end

  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