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.



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_pointObject

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

#runnerObject

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

Parameters:

  • path (String)

    The path to the file within the public folder

Returns:

  • (String)

    The absolute path



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.

Returns:

  • (Boolean)

    True if the data option has been set to true



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

Parameters:

  • resource_name (String)

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

  • locale (String) (defaults to: nil)

    The locale for the request

  • raw (Boolean) (defaults to: false)

    True if the result has to be converted into object.

Returns:

  • (Object)

    The object or a collection of objects.



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

Parameters:

  • path (String)

    The path to the file within the public folder

Returns:

  • (Object)

    The file



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.

Parameters:

  • resource_name (String)

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

  • params (Hash)

    The attributes of the resource

  • locale (String) (defaults to: nil)

    The locale for the request

  • raw (Boolean) (defaults to: false)

    True if the result has to be converted into object.

Returns:

  • (Object)

    The response of the API or nil if an error occurs



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
    message = data

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



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.

Parameters:

  • resource_name (String)

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

  • id (String)

    The unique identifier 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 or nil if an error occurs



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
    message = data

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

    raise WriterException.new(message)

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

Parameters:

  • source (String)

    The source text

Returns:

  • (String)

    The source with remote urls



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_attributesObject



141
142
143
# File 'lib/locomotive/mounter/writer/api/base.rb', line 141

def safe_attributes
  %w(_id)
end