Class: Locomotive::Mounter::Writer::Api::Base

Inherits:
Object
  • Object
show all
Includes:
Utils::Output
Defined in:
lib/locomotive/mounter/writer/api/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_pointObject

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

#runnerObject

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

Parameters:

  • path (String)

    The path to the file within the public folder

Returns:

  • (String)

    The absolute path



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.

Returns:

  • (Boolean)

    True if the data option has been set to true



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

Parameters:

  • resource_name (String)

    The path to the resource (usually, the resource name)

  • locale (String) (defaults to: nil)

    The locale for the request

  • dont_filter_attributes (Boolean) (defaults to: false)

    False if the we want to only keep the attributes defined by the safe_attributes method

Returns:

  • (Object)

    The object or a collection of objects.



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.message)
  end
end

#path_to_file(path) ⇒ Object

Take a path and convert it to a File object if possible

Parameters:

  • path (String)

    The path to the file within the public folder

Returns:

  • (Object)

    The file



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.

Parameters:

  • resource_name (String)

    The path to the resource (usually, the resource name)

  • attributes (Hash)

    The attributes of the resource

  • locale (String) (defaults to: nil)

    The locale for the request

  • raw (Boolean)

    True if the result has to be filtered in order to keep only the attributes defined by the safe_attributes method

Returns:

  • (Object)

    The response of the API or nil if an error occurs



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
    message = e.message
    message = message.map do |attribute, errors|
      "      #{attribute} => #{[*errors].join(', ')}\n".colorize(color: :red)
    end.join("\n") if message.respond_to?(:keys)

    raise WriterException.new(message)
  end
end

#prepareObject

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.

Parameters:

  • resource_name (String)

    The path to the resource (usually, the resource name)

  • attributes (Hash)

    The attributes of the resource

  • params (Hash)

    The attributes of the resource

  • locale (String) (defaults to: nil)

    The locale for the request

Returns:

  • (Object)

    The response of 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
    message = e.message
    message = message.map do |attribute, errors|
      "      #{attribute} => #{[*errors].join(', ')}\n".colorize(color: :red)
    end.join("\n") if message.respond_to?(:keys)

    raise WriterException.new(message)

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

Parameters:

  • source (String)

    The source text

Returns:

  • (String)

    The source with remote urls



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_attributesObject



110
111
112
# File 'lib/locomotive/mounter/writer/api/base.rb', line 110

def safe_attributes
  %w(_id)
end