Class: Ecoportal::API::V2::Pages
- Inherits:
-
Object
- Object
- Ecoportal::API::V2::Pages
- 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
-
#client ⇒ Common::Client
readonly
a
Common::Clientobject that holds the configuration of the api connection.
Instance Method Summary collapse
-
#create(doc, from:) ⇒ Response
Requests a creation of a page via api.
-
#get(id, stage_id: nil) ⇒ Ecoportal::API::V2::Page, Ecoportal::API::V2::Pages::PageStage
Gets a page via api.
-
#get_new(from) ⇒ Ecoportal::API::V2::Page
Gets a
newnon-existing page via api with all the ids initialized. -
#initialize(client) ⇒ Schemas
constructor
An instance object ready to make schema api requests.
-
#stages ⇒ V2::Pages::Stages
Obtain specific object for pages api requests.
-
#update(doc) ⇒ Ecoportal::API::Common::Response
Requests to update an existing page via api.
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.
18 19 20 |
# File 'lib/ecoportal/api/v2/pages.rb', line 18 def initialize(client) @client = client end |
Instance Attribute Details
#client ⇒ Common::Client (readonly)
a Common::Client object that holds the configuration of the api connection.
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.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/ecoportal/api/v2/pages.rb', line 78 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
- if the request has
success?the returnedobject.resultgives an object with thatPage. - if it failed to obtain the full page, it returns a
PageStagewith the active stage data.
Gets a page via api.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/ecoportal/api/v2/pages.rb', line 35 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.
65 66 67 68 69 70 71 72 |
# File 'lib/ecoportal/api/v2/pages.rb', line 65 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 ⇒ V2::Pages::Stages
Obtain specific object for pages api requests.
24 25 26 |
# File 'lib/ecoportal/api/v2/pages.rb', line 24 def stages stages_class.new(client) end |
#update(doc) ⇒ Ecoportal::API::Common::Response
It won't launch the update unless there are changes
Requests to update an existing page via api.
54 55 56 57 58 59 60 |
# File 'lib/ecoportal/api/v2/pages.rb', line 54 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 |