lib/ecoportal/api/v2/pages.rb in ecoportal-api-v2-1.1.8 vs lib/ecoportal/api/v2/pages.rb in ecoportal-api-v2-2.0.0
- old
+ new
@@ -1,18 +1,19 @@
module Ecoportal
module API
class V2
- # @attr_reader client [Common::Client] a `Common::Client` object that holds the configuration of the api connection.
+ # @attr_reader client [Common::Client] a `Common::Client` object that
+ # holds the configuration of the api connection.
class Pages
- STAGE_REX = /stages\/(?<sid>.*)/
+ STAGE_REX = /stages\/(?<sid>.*)/.freeze
extend Common::BaseClass
include Common::Content::DocHelpers
class_resolver :stages_class, "Ecoportal::API::V2::Pages::Stages"
- class_resolver :page_class, "Ecoportal::API::V2::Page"
- class_resolver :page_stage_class, "Ecoportal::API::V2::Pages::PageStage"
- class_resolver :create_page_response_class, "Ecoportal::API::V2::Pages::PageCreateResponse"
+ class_resolver :page_class, "Ecoportal::API::V2::Page"
+ class_resolver :page_stage_class, "Ecoportal::API::V2::Pages::PageStage"
+ class_resolver :create_page_response_class, "Ecoportal::API::V2::Pages::PageCreateResponse"
attr_reader :client
# @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.
@@ -33,20 +34,23 @@
# @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.
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
+
+ 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
# Requests to update an existing page via api.
# @note It won't launch the update unless there are changes
@@ -54,11 +58,12 @@
# @return [Ecoportal::API::Common::Response] an object with the api response.
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)
+
+ id = get_id(doc)
client.patch("/pages/#{CGI.escape(id)}", data: 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
@@ -79,13 +84,13 @@
def create(doc, from:)
id = get_id(from)
body = get_body(doc).tap do |hash|
unless hash["page"]
hash["page"] = {
- "id" => "111111111111111111111111",
+ "id" => "111111111111111111111111",
"operation" => "changed",
- "data" => {"patch_ver" => 0}
+ "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)
@@ -96,10 +101,9 @@
private
def url_to_stage_id(url)
(matches = url.match(STAGE_REX)) && matches[:sid]
end
-
end
end
end
end