Class: FrontyApi

Inherits:
Object
  • Object
show all
Defined in:
app/controllers/designers_controller.rb

Instance Method Summary collapse

Constructor Details

#initializeFrontyApi

Returns a new instance of FrontyApi.



192
193
194
195
# File 'app/controllers/designers_controller.rb', line 192

def initialize
  @url = 'https://fronty.com:8181'
  @token = nil
end

Instance Method Details

#download(uid, id) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'app/controllers/designers_controller.rb', line 218

def download(uid, id)
  content = open(@url + '/static/uploads/' + uid + '/result.zip')

  Zip::File.open_buffer(content) do |zip|
    zip.each do |f|
      p f.name
      f_path = File.join("/tmp/booo", f.name)
      if f.name == 'index.html'
        f_path = File.join("#{Rails.root}/app/views/pages/", "page-#{id.to_s}.html.erb")
        FileUtils.mkdir_p(File.dirname(f_path))
        zip.extract(f, f_path) unless File.exist?(f_path)
        text = File.read(f_path)
        new_contents = text.gsub('assets/img', 'assets/page-' + id.to_s)
                           .gsub('link href="assets/css', 'link href="assets/page-' + id.to_s)
                           .gsub('script src="assets/js', 'script src="assets/page-' + id.to_s)
        File.open(f_path, "w") {|file| file.puts new_contents}
      elsif f.name == 'assets/css/styles.min.css'
        f_path = File.join("#{Rails.root}/app/assets/stylesheets/page-#{id.to_s}", "styles.min.css")
        FileUtils.mkdir_p(File.dirname(f_path))
        zip.extract(f, f_path) unless File.exist?(f_path)
        text = File.read(f_path)
        new_contents = text.gsub(
            '../img', '/assets/page-' + id.to_s)
        File.open(f_path, "w") {|file| file.puts new_contents}
      elsif f.name == 'assets/js/app.min.js'
        f_path = File.join("#{Rails.root}/app/assets/javascripts/page-#{id.to_s}", "app.min.js")
        FileUtils.mkdir_p(File.dirname(f_path))
        zip.extract(f, f_path) unless File.exist?(f_path)
      elsif f.name.include? 'assets/img/'
        file_name = f.name.split('assets/img/')[1]
        f_path = File.join("#{Rails.root}/app/assets/images/page-#{id.to_s}", file_name)
        FileUtils.mkdir_p(File.dirname(f_path))
        zip.extract(f, f_path) unless File.exist?(f_path)
      else
        next
      end
    end
  end
end

#get_status(job_id) ⇒ Object



213
214
215
216
# File 'app/controllers/designers_controller.rb', line 213

def get_status(job_id)
  response = RestClient.get(@url + '/api/get_status/' + job_id)
  JSON.parse(response)
end

#loginObject



197
198
199
200
201
202
203
204
# File 'app/controllers/designers_controller.rb', line 197

def 
  if File.file?('.token')
    open('.token', 'rb') do |f|
      @token = f.read
      p @token
    end
  end
end

#upload(image_path) ⇒ Object



206
207
208
209
210
211
# File 'app/controllers/designers_controller.rb', line 206

def upload(image_path)
  response = RestClient.post(@url + '/api/upload/', payload = {'image': File.new(image_path)}, headers = {
      'Content-Type': 'multipart/form-data',
      'Authorization': 'Bearer ' + @token.to_s})
  JSON.parse(response)
end