Class: Locomotive::Mounter::Writer::Api::Base
- Inherits:
-
Object
- Object
- Locomotive::Mounter::Writer::Api::Base
- Includes:
- Utils::Output
- Defined in:
- lib/locomotive/mounter/writer/api/base.rb
Direct Known Subclasses
ContentAssetsWriter, ContentEntriesWriter, ContentTypesWriter, PagesWriter, SiteWriter, SnippetsWriter, ThemeAssetsWriter, TranslationsWriter
Instance Attribute Summary collapse
-
#mounting_point ⇒ Object
Returns the value of attribute mounting_point.
-
#runner ⇒ Object
Returns the value of attribute runner.
Instance Method Summary collapse
-
#absolute_path(path) ⇒ String
Return the absolute path from a relative path pointing to an asset within the public folder.
-
#data? ⇒ Boolean
By setting the data option to true, user content (content entries and editable elements from page) can be pushed too.
-
#each_locale(&block) ⇒ Object
Loop on each locale of the mounting point and change the current locale at the same time.
-
#get(resource_name, locale = nil, raw = false) ⇒ Object
Get remote resource(s) by the API.
-
#initialize(mounting_point, runner) ⇒ Base
constructor
A new instance of Base.
-
#path_to_file(path) ⇒ Object
Take a path and convert it to a File object if possible.
-
#post(resource_name, params, locale = nil, raw = false) ⇒ Object
Create a resource by the API.
-
#prepare ⇒ Object
A write may have to do some work before being launched.
-
#put(resource_name, id, params, locale = nil) ⇒ Object
Update a resource by the API.
-
#replace_content_assets!(source) ⇒ String
Take in the source the assets whose url begins by “/samples”, upload them to the engine and replace them by their remote url.
- #safe_attributes ⇒ Object
Constructor Details
#initialize(mounting_point, runner) ⇒ Base
Returns a new instance of Base.
19 20 21 22 |
# File 'lib/locomotive/mounter/writer/api/base.rb', line 19 def initialize(mounting_point, runner) self.mounting_point = mounting_point self.runner = runner end |
Instance Attribute Details
#mounting_point ⇒ Object
Returns the value of attribute mounting_point.
13 14 15 |
# File 'lib/locomotive/mounter/writer/api/base.rb', line 13 def mounting_point @mounting_point end |
#runner ⇒ Object
Returns the value of attribute runner.
13 14 15 |
# File 'lib/locomotive/mounter/writer/api/base.rb', line 13 def runner @runner end |
Instance Method Details
#absolute_path(path) ⇒ String
Return the absolute path from a relative path pointing to an asset within the public folder
162 163 164 |
# File 'lib/locomotive/mounter/writer/api/base.rb', line 162 def absolute_path(path) File.join(self.mounting_point.path, 'public', path) end |
#data? ⇒ Boolean
By setting the data option to true, user content (content entries and editable elements from page) can be pushed too. By default, its value is false.
37 38 39 |
# File 'lib/locomotive/mounter/writer/api/base.rb', line 37 def data? self.runner.parameters[:data] || false end |
#each_locale(&block) ⇒ Object
Loop on each locale of the mounting point and change the current locale at the same time.
147 148 149 150 151 152 153 |
# File 'lib/locomotive/mounter/writer/api/base.rb', line 147 def each_locale(&block) self.mounting_point.locales.each do |locale| Locomotive::Mounter.with_locale(locale) do block.call(locale) end end end |
#get(resource_name, locale = nil, raw = false) ⇒ Object
Get remote resource(s) by the API
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/locomotive/mounter/writer/api/base.rb', line 49 def get(resource_name, locale = nil, raw = false) params = { query: {} } params[:query][:locale] = locale if locale response = Locomotive::Mounter::EngineApi.get("/#{resource_name}.json", params) data = response.parsed_response if response.success? return data if raw self.raw_data_to_object(data) else raise WriterException.new(data['error']) end end |
#path_to_file(path) ⇒ Object
Take a path and convert it to a File object if possible
172 173 174 |
# File 'lib/locomotive/mounter/writer/api/base.rb', line 172 def path_to_file(path) File.new(self.absolute_path(path)) end |
#post(resource_name, params, locale = nil, raw = false) ⇒ Object
Create a resource by the API.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/locomotive/mounter/writer/api/base.rb', line 74 def post(resource_name, params, locale = nil, raw = false) params_name = resource_name.to_s.split('/').last.singularize query = { query: { params_name => params } } query[:query][:locale] = locale if locale response = Locomotive::Mounter::EngineApi.post("/#{resource_name}.json", query) data = response.parsed_response if response.success? return data if raw self.raw_data_to_object(data) else = data = data.map do |attribute, errors| " #{attribute} => #{[*errors].join(', ')}\n".colorize(color: :red) end.join("\n") if data.respond_to?(:keys) raise WriterException.new() # self.log "\n" # data.each do |attribute, errors| # self.log " #{attribute} => #{[*errors].join(', ')}\n".colorize(color: :red) # end if data.respond_to?(:keys) # nil end end |
#prepare ⇒ Object
A write may have to do some work before being launched. By default, it displays to the output the resource being pushed.
27 28 29 |
# File 'lib/locomotive/mounter/writer/api/base.rb', line 27 def prepare self.output_title end |
#put(resource_name, id, params, locale = nil) ⇒ Object
Update a resource by the API.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/locomotive/mounter/writer/api/base.rb', line 113 def put(resource_name, id, params, locale = nil) params_name = resource_name.to_s.split('/').last.singularize query = { query: { params_name => params } } query[:query][:locale] = locale if locale response = Locomotive::Mounter::EngineApi.put("/#{resource_name}/#{id}.json", query) data = response.parsed_response if response.success? self.raw_data_to_object(data) else = data = data.map do |attribute, errors| " #{attribute} => #{[*errors].join(', ')}" #.colorize(color: :red) end.join("\n") if data.respond_to?(:keys) raise WriterException.new() # data.each do |attribute, errors| # self.log "\t\t #{attribute} => #{[*errors].join(', ')}".colorize(color: :red) # end if data.respond_to?(:keys) # nil end end |
#replace_content_assets!(source) ⇒ String
Take in the source the assets whose url begins by “/samples”, upload them to the engine and replace them by their remote url.
183 184 185 186 187 188 189 190 |
# File 'lib/locomotive/mounter/writer/api/base.rb', line 183 def replace_content_assets!(source) return source if source.blank? source.to_s.gsub(/\/samples\/.*\.[a-zA-Z0-9]+/) do |match| url = self.runner.content_assets_writer.write(match) url || match end end |
#safe_attributes ⇒ Object
141 142 143 |
# File 'lib/locomotive/mounter/writer/api/base.rb', line 141 def safe_attributes %w(_id) end |