fronty-gem 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (136) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +1 -0
  3. data/Gemfile +6 -0
  4. data/LICENSE.txt +21 -0
  5. data/README.md +39 -0
  6. data/Rakefile +2 -0
  7. data/app/controllers/concerns/dynamic_content.rb +160 -0
  8. data/app/controllers/designers_controller.rb +257 -0
  9. data/app/controllers/pages_controller.rb +23 -0
  10. data/app/views/designers/editor.html.erb +245 -0
  11. data/app/views/pages/404.html.erb +2 -0
  12. data/app/views/pages/welcome.html.erb +2 -0
  13. data/bin/console +14 -0
  14. data/bin/setup +8 -0
  15. data/config/initializers/assets.rb +2 -0
  16. data/config/routes.rb +20 -0
  17. data/fronty-gem.gemspec +37 -0
  18. data/lib/config/routes.rb +3 -0
  19. data/lib/fronty-gem.rb +5 -0
  20. data/lib/fronty-gem/version.rb +3 -0
  21. data/lib/tasks/fronty.rake +14 -0
  22. data/vendor/assets/demo/album/album.css +39 -0
  23. data/vendor/assets/demo/album/index.html +231 -0
  24. data/vendor/assets/demo/blog/blog.css +130 -0
  25. data/vendor/assets/demo/blog/index.html +230 -0
  26. data/vendor/assets/demo/carousel/carousel.css +91 -0
  27. data/vendor/assets/demo/carousel/index.html +186 -0
  28. data/vendor/assets/demo/narrow-jumbotron/index.html +82 -0
  29. data/vendor/assets/demo/narrow-jumbotron/narrow-jumbotron.css +80 -0
  30. data/vendor/assets/demo/offcanvas/index.html +158 -0
  31. data/vendor/assets/demo/offcanvas/offcanvas.css +79 -0
  32. data/vendor/assets/demo/offcanvas/offcanvas.js +7 -0
  33. data/vendor/assets/demo/pricing/index.html +141 -0
  34. data/vendor/assets/demo/pricing/pricing.css +25 -0
  35. data/vendor/assets/demo/product/index.html +177 -0
  36. data/vendor/assets/demo/product/product.css +79 -0
  37. data/vendor/assets/fonts/line-awesome.eot +0 -0
  38. data/vendor/assets/fonts/line-awesome.svg +2628 -0
  39. data/vendor/assets/fonts/line-awesome.ttf +0 -0
  40. data/vendor/assets/fonts/line-awesome.woff +0 -0
  41. data/vendor/assets/fonts/line-awesome.woff2 +0 -0
  42. data/vendor/assets/images/icons/alert.svg +66 -0
  43. data/vendor/assets/images/icons/arrow-down.svg +60 -0
  44. data/vendor/assets/images/icons/arrow-right.svg +60 -0
  45. data/vendor/assets/images/icons/badge.svg +81 -0
  46. data/vendor/assets/images/icons/breadcrumbs.svg +77 -0
  47. data/vendor/assets/images/icons/button.svg +78 -0
  48. data/vendor/assets/images/icons/button_group.svg +81 -0
  49. data/vendor/assets/images/icons/button_toolbar.svg +87 -0
  50. data/vendor/assets/images/icons/cart.svg +62 -0
  51. data/vendor/assets/images/icons/categories.svg +62 -0
  52. data/vendor/assets/images/icons/chart.svg +1 -0
  53. data/vendor/assets/images/icons/checkbox.svg +66 -0
  54. data/vendor/assets/images/icons/checkout.svg +62 -0
  55. data/vendor/assets/images/icons/chevron-down.svg +67 -0
  56. data/vendor/assets/images/icons/chevron-right.svg +67 -0
  57. data/vendor/assets/images/icons/components/cart.svg +16 -0
  58. data/vendor/assets/images/icons/components/checkbox.svg +1 -0
  59. data/vendor/assets/images/icons/components/contact-form.svg +13 -0
  60. data/vendor/assets/images/icons/components/map.svg +12 -0
  61. data/vendor/assets/images/icons/container.svg +66 -0
  62. data/vendor/assets/images/icons/facebook.svg +1 -0
  63. data/vendor/assets/images/icons/file.svg +1 -0
  64. data/vendor/assets/images/icons/filters.svg +62 -0
  65. data/vendor/assets/images/icons/folder.svg +1 -0
  66. data/vendor/assets/images/icons/form.svg +66 -0
  67. data/vendor/assets/images/icons/grid_row.svg +66 -0
  68. data/vendor/assets/images/icons/heading.svg +66 -0
  69. data/vendor/assets/images/icons/hr.svg +66 -0
  70. data/vendor/assets/images/icons/image.svg +62 -0
  71. data/vendor/assets/images/icons/instagram.svg +62 -0
  72. data/vendor/assets/images/icons/jumbotron.svg +66 -0
  73. data/vendor/assets/images/icons/label.svg +73 -0
  74. data/vendor/assets/images/icons/link.svg +1 -0
  75. data/vendor/assets/images/icons/list_group.svg +66 -0
  76. data/vendor/assets/images/icons/map.svg +62 -0
  77. data/vendor/assets/images/icons/maps.png +0 -0
  78. data/vendor/assets/images/icons/minus_round.svg +1 -0
  79. data/vendor/assets/images/icons/navbar.svg +66 -0
  80. data/vendor/assets/images/icons/pagination.svg +68 -0
  81. data/vendor/assets/images/icons/panel.svg +66 -0
  82. data/vendor/assets/images/icons/paragraph.svg +66 -0
  83. data/vendor/assets/images/icons/paypal.svg +1 -0
  84. data/vendor/assets/images/icons/play-button.svg +42 -0
  85. data/vendor/assets/images/icons/plus_round.svg +1 -0
  86. data/vendor/assets/images/icons/product.png +0 -0
  87. data/vendor/assets/images/icons/product.svg +62 -0
  88. data/vendor/assets/images/icons/product_gallery.svg +68 -0
  89. data/vendor/assets/images/icons/products.svg +62 -0
  90. data/vendor/assets/images/icons/progressbar.svg +67 -0
  91. data/vendor/assets/images/icons/radio.svg +66 -0
  92. data/vendor/assets/images/icons/search.svg +69 -0
  93. data/vendor/assets/images/icons/select_input.svg +67 -0
  94. data/vendor/assets/images/icons/slider.svg +62 -0
  95. data/vendor/assets/images/icons/table.svg +66 -0
  96. data/vendor/assets/images/icons/text_area.svg +66 -0
  97. data/vendor/assets/images/icons/text_input.svg +66 -0
  98. data/vendor/assets/images/icons/twitter.svg +55 -0
  99. data/vendor/assets/images/icons/user.svg +62 -0
  100. data/vendor/assets/images/icons/video.svg +45 -0
  101. data/vendor/assets/images/icons/well.svg +82 -0
  102. data/vendor/assets/images/svg/icon-a.svg +235 -0
  103. data/vendor/assets/images/svg/icon-b.svg +224 -0
  104. data/vendor/assets/images/svg/icon-c.svg +224 -0
  105. data/vendor/assets/images/svg/icon-d.svg +224 -0
  106. data/vendor/assets/images/svg/logo.svg +41 -0
  107. data/vendor/assets/images/thumbnails/XX__.png +0 -0
  108. data/vendor/assets/images/thumbnails/comparison.png +0 -0
  109. data/vendor/assets/images/thumbnails/details-with-title-complate.png +0 -0
  110. data/vendor/assets/images/thumbnails/details.png +0 -0
  111. data/vendor/assets/images/thumbnails/footer-with-links.png +0 -0
  112. data/vendor/assets/images/thumbnails/footer.png +0 -0
  113. data/vendor/assets/images/thumbnails/header.png +0 -0
  114. data/vendor/assets/images/thumbnails/jumbotron-with-title.png +0 -0
  115. data/vendor/assets/images/thumbnails/jumbotron.png +0 -0
  116. data/vendor/assets/images/thumbnails/linkbar.png +0 -0
  117. data/vendor/assets/images/thumbnails/liticle.png +0 -0
  118. data/vendor/assets/images/thumbnails/logobar.png +0 -0
  119. data/vendor/assets/images/thumbnails/page-title.png +0 -0
  120. data/vendor/assets/images/thumbnails/quiz-complate.png +0 -0
  121. data/vendor/assets/images/thumbnails/quiz-with-hero-complate.png +0 -0
  122. data/vendor/assets/images/thumbnails/result-complate.png +0 -0
  123. data/vendor/assets/images/thumbnails/review-section.png +0 -0
  124. data/vendor/assets/images/thumbnails/review-v2-complate.png +0 -0
  125. data/vendor/assets/images/thumbnails/review-v3-complate.png +0 -0
  126. data/vendor/assets/images/thumbnails/review_section_v4_complate.png +0 -0
  127. data/vendor/assets/images/thumbnails/split-hero-complate.png +0 -0
  128. data/vendor/assets/images/thumbnails/subtitle-with-button-complate.png +0 -0
  129. data/vendor/assets/images/thumbnails/subtitle-with-link.png +0 -0
  130. data/vendor/assets/images/thumbnails/unknown-complate.png +0 -0
  131. data/vendor/assets/images/thumbnails/unknown1-complate.png +0 -0
  132. data/vendor/assets/images/thumbnails/video-section-complate.png +0 -0
  133. data/vendor/assets/javascript/fronty-designer-init.js +491 -0
  134. data/vendor/assets/javascript/fronty-designer.js +16518 -0
  135. data/vendor/assets/stylesheets/fronty-designer.css +11255 -0
  136. metadata +235 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 18ee76cdb44c364010ab600157ea646e719ce086416f00d6a8a52b15f8cc883f
4
+ data.tar.gz: d325c1bce0ef12a2f48a401a02b3945f93ee4ef5e9811536c3401586ec5f0b5c
5
+ SHA512:
6
+ metadata.gz: 7d2f60557719d1ec0d8d700cc68b338cd0d957edb1f15ed041e5c38c1b5fb9a73fabf09bc33c8ac942d5d08b76571b59cb640bb43dbd6104f35f1e428f010087
7
+ data.tar.gz: 87e761bb2a268b3408efbe9b96b0b40c82f6925c62133a50d0631e2033f65c4db75646a81f74417ddce0c6e5b7b9212ecd6968bc99cbe4eb6f7e6ba9dffe8849
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ .idea/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in fronty_designer.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Miqayel Manvelyan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # FrontyGem
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/fronty_designer`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'fronty_designer'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install fronty_designer
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/fronty_designer.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,160 @@
1
+ require 'csv'
2
+
3
+ module DynamicContent
4
+ extend ActiveSupport::Concern
5
+
6
+ def set_dynamic_content(mapper, match_term, query = :exact)
7
+ dynamic_content = Rails.cache.fetch("#{mapper}-#{match_term}-dynamic", :expires_in => 1.seconds) do
8
+ match_row = find_match(parse_csv(mapper), match_term, query)
9
+ match_row.to_h
10
+ end
11
+ @dynamic_content = OpenStruct.new(dynamic_content)
12
+ end
13
+
14
+ def parse_csv(mapper)
15
+ csv_path = "#{Rails.root}/lib/mappers/#{mapper}.csv"
16
+ csv_dir = "#{Rails.root}/lib/mappers"
17
+ Dir.mkdir(csv_dir) unless File.exists?(csv_dir)
18
+ if (!File.exist?(csv_path))
19
+ File.open(csv_path, "w+") do |f|
20
+ f.write("id,route,title,redirect_to,thumbnail\n")
21
+ end
22
+ end
23
+ CSV.parse(File.read(csv_path), :headers => true, :header_converters => :symbol)
24
+ end
25
+
26
+ def update_csv_by_id(id, route, title, redirect, mapper)
27
+ table = CSV.table("#{Rails.root}/lib/mappers/#{mapper}.csv")
28
+ table.each do |row|
29
+ if row[:id] == id.to_i
30
+ if redirect and redirect.empty?
31
+ redirect = nil
32
+ end
33
+ row[:redirect_to] = redirect
34
+ row[:route] = route
35
+ row[:title] = title
36
+ end
37
+ end
38
+
39
+ File.open("#{Rails.root}/lib/mappers/#{mapper}.csv", 'w') do |f|
40
+ f.write(table.to_csv(write_headers: true))
41
+ end
42
+ end
43
+
44
+ def get_row_by_route(route, mapper)
45
+ data_list = csv_to_json(mapper)
46
+ row = nil
47
+
48
+ data_list.each do |data|
49
+ if data[:route] == route
50
+ row = data
51
+ end
52
+ end
53
+ row
54
+ end
55
+
56
+ def get_id_by_route(route, mapper)
57
+ data_list = csv_to_json(mapper)
58
+
59
+ page_id = 0
60
+ data_list.each do |data|
61
+ page_id += 1
62
+ if data[:route] == route
63
+ break
64
+ end
65
+ end
66
+ page_id
67
+ end
68
+
69
+ def get_last_id_from_csv(mapper)
70
+ data_list = csv_to_json(mapper)
71
+
72
+ if data_list.any?
73
+ last_id = data_list[-1][:id].to_i
74
+ else
75
+ last_id = 0
76
+ end
77
+ (last_id + 1).to_s
78
+ end
79
+
80
+ def remove_by_route(route, mapper)
81
+ table = CSV.table("#{Rails.root}/lib/mappers/#{mapper}.csv")
82
+
83
+ page_id = get_id_by_route(route, mapper)
84
+
85
+ html_path = "#{Rails.root}/app/views/pages/page-#{page_id}.html.erb"
86
+ assets_dirs = [
87
+ "#{Rails.root}/app/assets/images/page-#{page_id}",
88
+ "#{Rails.root}/app/assets/stylesheets/page-#{page_id}",
89
+ "#{Rails.root}/app/assets/javascripts/page-#{page_id}"
90
+ ]
91
+
92
+ File.delete(html_path) if File.exist?(html_path)
93
+
94
+ for dir in assets_dirs
95
+ FileUtils.remove_dir(dir) if File.directory?(dir)
96
+ end
97
+
98
+ table.delete_if do |row|
99
+ row[:route] == route
100
+ end
101
+
102
+ if table.size() != 0
103
+ File.open("#{Rails.root}/lib/mappers/#{mapper}.csv", 'w') do |f|
104
+ f.write(table.to_csv(write_headers: true))
105
+ end
106
+ else
107
+ File.open("#{Rails.root}/lib/mappers/#{mapper}.csv", 'w') do |f|
108
+ f.write("id,route,title,redirect_to,thumbnail\n")
109
+ end
110
+ end
111
+ end
112
+
113
+ def add_to_csv(mapper, row)
114
+ CSV.open("#{Rails.root}/lib/mappers/#{mapper}.csv", "a+") do |csv|
115
+ csv << row
116
+ end
117
+ end
118
+
119
+ def csv_is_empty(mapper)
120
+ CSV.open("#{Rails.root}/lib/mappers/#{mapper}.csv", "r") do |csv|
121
+ count = csv.readlines.size
122
+ flag = true
123
+ if count > 1
124
+ flag = false
125
+ end
126
+ return flag
127
+ end
128
+ end
129
+
130
+ def csv_to_json(mapper)
131
+ col_data = Array.new
132
+ first = true
133
+ CSV.foreach("#{Rails.root}/lib/mappers/#{mapper}.csv") do |row|
134
+ if first
135
+ first = false
136
+ next
137
+ end
138
+ col_data.append({
139
+ :title => row[2],
140
+ :route => row[1],
141
+ :id => row[0],
142
+ :redirect_to => row[3],
143
+ :thumbnail => row[4]
144
+ })
145
+ end
146
+ col_data
147
+ end
148
+
149
+ def find_match(csv, match_term, query)
150
+ csv.detect {|row| send(query, row[:route].downcase, match_term.downcase)} || nil
151
+ end
152
+
153
+ def exact(row_key, match_term)
154
+ row_key == match_term
155
+ end
156
+
157
+ def partial(row_key, match_term)
158
+ match_term.include?(row_key)
159
+ end
160
+ end
@@ -0,0 +1,257 @@
1
+ require 'rest-client'
2
+ require 'json'
3
+ require 'open-uri'
4
+ require 'zip'
5
+
6
+
7
+ class DesignersController < ApplicationController
8
+ include DynamicContent
9
+
10
+ # POST /designer/git_pull
11
+ def git_pull
12
+ system('git pull')
13
+ render json: {:status => 'success'}
14
+ end
15
+
16
+ # POST /designer/git_push
17
+ def git_push
18
+ system('git add .')
19
+ system('git commit -m "my commit message"')
20
+ system('git push origin master')
21
+ render json: {:status => 'success'}
22
+ end
23
+
24
+ def index
25
+ render 'editor', layout: false
26
+ end
27
+
28
+ def get_pages
29
+ data_list = csv_to_json('pages')
30
+ p data_list
31
+ render json: data_list
32
+ end
33
+
34
+ def get_page_by_route
35
+ route = params['route']
36
+ row = get_row_by_route(route, 'pages')
37
+ render json: row
38
+ end
39
+
40
+ def update
41
+ redirect = params['redirect'] if !params['redirect'].empty? else nil
42
+ route = params['route']
43
+ title = params['title']
44
+ id = params['id']
45
+
46
+
47
+ update_csv_by_id(id, route, title, redirect, 'pages')
48
+ render json: {'status': 'success'}
49
+ end
50
+
51
+ # POST /designer/remove
52
+ def remove
53
+ route = params['route']
54
+ remove_by_route(route, 'pages')
55
+ render json: {'status': 'success'}
56
+ end
57
+
58
+ # POST /designer/save
59
+ def save
60
+ route = params[:route]
61
+ html = params[:html]
62
+
63
+ page_id = get_id_by_route(route, 'pages')
64
+
65
+ if page_id
66
+ file_path = "#{Rails.root}/app/views/pages/page-#{page_id}.html.erb"
67
+ File.open(file_path, "w") {|file| file.puts html}
68
+ end
69
+
70
+ render json: {
71
+ :status => 'success'
72
+ }
73
+ end
74
+
75
+ # POST /designer/create
76
+ def create
77
+ url = params[:template]
78
+ title = params[:title]
79
+ route = params[:route]
80
+ image = params[:image]
81
+ template = params[:template]
82
+ redirect = params[:redirect]
83
+
84
+ if template and !template.empty?
85
+ row = get_row_by_route(template, 'pages')
86
+
87
+ old_id = row[:id]
88
+ old_route = row[:route]
89
+ old_thumbnail = row[:thumbnail]
90
+ new_id = get_last_id_from_csv('pages')
91
+ new_redirect = redirect
92
+ new_route = route
93
+ new_title = title
94
+
95
+ if new_route == old_route
96
+ new_route += '-copy'
97
+ end
98
+
99
+ if new_route[0] != '/'
100
+ new_route = '/' + new_route
101
+ end
102
+
103
+ old_css_dir = "#{Rails.root}/app/assets/stylesheets/page-#{old_id}"
104
+ new_css_dir = "#{Rails.root}/app/assets/stylesheets/page-#{new_id}"
105
+
106
+ old_js_dir = "#{Rails.root}/app/assets/javascripts/page-#{old_id}"
107
+ new_js_dir = "#{Rails.root}/app/assets/javascripts/page-#{new_id}"
108
+
109
+ old_img_dir = "#{Rails.root}/app/assets/images/page-#{old_id}"
110
+ new_img_dir = "#{Rails.root}/app/assets/images/page-#{new_id}"
111
+
112
+ old_template_path = "#{Rails.root}/app/views/pages/page-#{old_id}.html.erb"
113
+ new_template_path = "#{Rails.root}/app/views/pages/page-#{new_id}.html.erb"
114
+
115
+ if File.exists?(old_css_dir)
116
+ FileUtils.cp_r old_css_dir, new_css_dir
117
+ end
118
+
119
+ if File.exists?(old_js_dir)
120
+ FileUtils.cp_r old_js_dir, new_js_dir
121
+ end
122
+
123
+ if File.exists?(old_img_dir)
124
+ FileUtils.cp_r old_img_dir, new_img_dir
125
+ end
126
+
127
+ if File.exists?(old_template_path)
128
+ FileUtils.cp_r old_template_path, new_template_path
129
+ end
130
+
131
+ add_to_csv('pages', [new_id, new_route, new_title, new_redirect, old_thumbnail])
132
+
133
+ render json: {
134
+ :status => 'success',
135
+ }
136
+ else
137
+ image_path = image.tempfile.path
138
+
139
+ api = FrontyApi.new
140
+ api.login()
141
+ response = api.upload(image_path)
142
+
143
+ render json: {
144
+ :status => 'started',
145
+ :job_id => response['job_id'],
146
+ :thumbnail => response['thumbnail_url']
147
+ }
148
+ end
149
+ end
150
+
151
+ # GET /get_status
152
+ def get_status
153
+ job_id = params[:job_id]
154
+ api = FrontyApi.new
155
+ render json: {'data': api.get_status(job_id)}
156
+ end
157
+
158
+ # POST /create_new_page
159
+ def create_new_page
160
+ uid = params['uid']
161
+ route = params['route']
162
+ title = params['title']
163
+ template = params['template']
164
+ redirect = params['redirect']
165
+ thumbnail = params['thumbnail']
166
+
167
+ if template
168
+ p template
169
+ else
170
+ if redirect and redirect.empty?
171
+ redirect = nil
172
+ end
173
+
174
+ api = FrontyApi.new
175
+ id = get_last_id_from_csv('pages')
176
+ api.download(uid, id)
177
+ if route
178
+ if route[0] != '/'
179
+ route = '/' + route
180
+ end
181
+ end
182
+ add_to_csv('pages', [id, route, title, redirect, thumbnail])
183
+ render json: {
184
+ :status => 'success',
185
+ :id => id
186
+ }
187
+ end
188
+ end
189
+ end
190
+
191
+ class FrontyApi
192
+ def initialize
193
+ @url = 'https://fronty.com:8181'
194
+ @token = nil
195
+ end
196
+
197
+ def login
198
+ if File.file?('.token')
199
+ open('.token', 'rb') do |f|
200
+ @token = f.read
201
+ p @token
202
+ end
203
+ end
204
+ end
205
+
206
+ def upload(image_path)
207
+ response = RestClient.post(@url + '/api/upload/', payload = {'image': File.new(image_path)}, headers = {
208
+ 'Content-Type': 'multipart/form-data',
209
+ 'Authorization': 'Bearer ' + @token.to_s})
210
+ JSON.parse(response)
211
+ end
212
+
213
+ def get_status(job_id)
214
+ response = RestClient.get(@url + '/api/get_status/' + job_id)
215
+ JSON.parse(response)
216
+ end
217
+
218
+ def download(uid, id)
219
+ content = open(@url + '/static/uploads/' + uid + '/result.zip')
220
+
221
+ Zip::File.open_buffer(content) do |zip|
222
+ zip.each do |f|
223
+ p f.name
224
+ f_path = File.join("/tmp/booo", f.name)
225
+ if f.name == 'index.html'
226
+ f_path = File.join("#{Rails.root}/app/views/pages/", "page-#{id.to_s}.html.erb")
227
+ FileUtils.mkdir_p(File.dirname(f_path))
228
+ zip.extract(f, f_path) unless File.exist?(f_path)
229
+ text = File.read(f_path)
230
+ new_contents = text.gsub('assets/img', 'assets/page-' + id.to_s)
231
+ .gsub('link href="assets/css', 'link href="assets/page-' + id.to_s)
232
+ .gsub('script src="assets/js', 'script src="assets/page-' + id.to_s)
233
+ File.open(f_path, "w") {|file| file.puts new_contents}
234
+ elsif f.name == 'assets/css/styles.min.css'
235
+ f_path = File.join("#{Rails.root}/app/assets/stylesheets/page-#{id.to_s}", "styles.min.css")
236
+ FileUtils.mkdir_p(File.dirname(f_path))
237
+ zip.extract(f, f_path) unless File.exist?(f_path)
238
+ text = File.read(f_path)
239
+ new_contents = text.gsub(
240
+ '../img', '/assets/page-' + id.to_s)
241
+ File.open(f_path, "w") {|file| file.puts new_contents}
242
+ elsif f.name == 'assets/js/app.min.js'
243
+ f_path = File.join("#{Rails.root}/app/assets/javascripts/page-#{id.to_s}", "app.min.js")
244
+ FileUtils.mkdir_p(File.dirname(f_path))
245
+ zip.extract(f, f_path) unless File.exist?(f_path)
246
+ elsif f.name.include? 'assets/img/'
247
+ file_name = f.name.split('assets/img/')[1]
248
+ f_path = File.join("#{Rails.root}/app/assets/images/page-#{id.to_s}", file_name)
249
+ FileUtils.mkdir_p(File.dirname(f_path))
250
+ zip.extract(f, f_path) unless File.exist?(f_path)
251
+ else
252
+ next
253
+ end
254
+ end
255
+ end
256
+ end
257
+ end