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, dont_filter_attributes = false) ⇒ Object
Get remote resource(s) from 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, attributes, locale = nil, dont_filter_attributes = false) ⇒ Object
Create a resource from the API.
-
#prepare ⇒ Object
A write may have to do some work before being launched.
-
#put(resource_name, id, attributes, locale = nil) ⇒ Object
Update a resource from 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.
18 19 20 21 |
# File 'lib/locomotive/mounter/writer/api/base.rb', line 18 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.
10 11 12 |
# File 'lib/locomotive/mounter/writer/api/base.rb', line 10 def mounting_point @mounting_point end |
#runner ⇒ Object
Returns the value of attribute runner.
10 11 12 |
# File 'lib/locomotive/mounter/writer/api/base.rb', line 10 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
131 132 133 |
# File 'lib/locomotive/mounter/writer/api/base.rb', line 131 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.
36 37 38 |
# File 'lib/locomotive/mounter/writer/api/base.rb', line 36 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.
116 117 118 119 120 121 122 |
# File 'lib/locomotive/mounter/writer/api/base.rb', line 116 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, dont_filter_attributes = false) ⇒ Object
Get remote resource(s) from the API
48 49 50 51 52 53 54 55 56 |
# File 'lib/locomotive/mounter/writer/api/base.rb', line 48 def get(resource_name, locale = nil, dont_filter_attributes = false) attribute_names = dont_filter_attributes ? nil : self.safe_attributes begin Locomotive::Mounter::EngineApi.fetch(resource_name, {}, locale, attribute_names) rescue ApiReadException => e raise WriterException.new(e.) end end |
#path_to_file(path) ⇒ Object
Take a path and convert it to a File object if possible
141 142 143 |
# File 'lib/locomotive/mounter/writer/api/base.rb', line 141 def path_to_file(path) File.new(self.absolute_path(path)) end |
#post(resource_name, attributes, locale = nil, dont_filter_attributes = false) ⇒ Object
Create a resource from the API.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/locomotive/mounter/writer/api/base.rb', line 67 def post(resource_name, attributes, locale = nil, dont_filter_attributes = false) attribute_names = dont_filter_attributes ? nil : self.safe_attributes begin Locomotive::Mounter::EngineApi.create(resource_name, attributes, locale, attribute_names) rescue ApiWriteException => e = e. = .map do |attribute, errors| " #{attribute} => #{[*errors].join(', ')}\n".colorize(color: :red) end.join("\n") if .respond_to?(:keys) raise WriterException.new() 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.
26 27 28 |
# File 'lib/locomotive/mounter/writer/api/base.rb', line 26 def prepare self.output_title end |
#put(resource_name, id, attributes, locale = nil) ⇒ Object
Update a resource from the API.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/locomotive/mounter/writer/api/base.rb', line 91 def put(resource_name, id, attributes, locale = nil) begin Locomotive::Mounter::EngineApi.update(resource_name, id, attributes, locale, self.safe_attributes) rescue ApiWriteException => e = e. = .map do |attribute, errors| " #{attribute} => #{[*errors].join(', ')}\n".colorize(color: :red) end.join("\n") if .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 # DEBUG 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.
152 153 154 155 156 157 158 159 |
# File 'lib/locomotive/mounter/writer/api/base.rb', line 152 def replace_content_assets!(source) return source if source.blank? source.to_s.gsub(/\/samples\/\S*\.[a-zA-Z0-9]+/) do |match| url = self.content_assets_writer.write(match) url || match end end |
#safe_attributes ⇒ Object
110 111 112 |
# File 'lib/locomotive/mounter/writer/api/base.rb', line 110 def safe_attributes %w(_id) end |