Class: Pagiii::ThemeSync

Inherits:
Object
  • Object
show all
Includes:
Info
Defined in:
lib/pagiii/theme_sync.rb

Constant Summary collapse

ALLOWED_DIRS =
['templates', 'sections', 'layout', 'assets', 'snippets']

Instance Method Summary collapse

Methods included from Info

#info

Constructor Details

#initialize(token:, root_api:) ⇒ ThemeSync

Returns a new instance of ThemeSync.



8
9
10
11
# File 'lib/pagiii/theme_sync.rb', line 8

def initialize(token:, root_api:)
  @token = token    
  @root_api = root_api
end

Instance Method Details

#compare_and_delete(site, theme) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/pagiii/theme_sync.rb', line 106

def compare_and_delete(site, theme)
  conn = Faraday.new(
    url: @root_api,
    params: {param: '1'},
    headers: {
      'Content-Type' => 'application/json',
      'api-token' => @token
    }
  )
  response = get_theme_by_name(site: site, name: theme)
  check_authorized(response)
  theme_id = JSON.parse(response.body)['theme']['id']
  response = conn.get("/api/v1/admin/themes/#{theme_id}/assets?site_name=#{site}")
  remote_assets = JSON.parse(response.body)['assets'].map{|x| x['asset_key']}

  local_assets = []

  Dir.entries('.').each do |dir|
    if ALLOWED_DIRS.include?(dir) && File.directory?(dir)        
      Dir.entries(dir).reject{|x| [".", ".."].include?(x)}.each do |file|              
        file_path = "#{dir}/#{file}"                    
        local_assets << file_path
      end
    end
  end

  assets_to_be_deleted = remote_assets - local_assets
  assets_to_be_deleted.each do |asset_key|
    delete_remote(site, theme, asset_key)
  end
end

#create_theme(site, name) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/pagiii/theme_sync.rb', line 258

def create_theme(site, name)
  conn = Faraday.new(
    url: "#{@root_api}/api/v1/admin/themes?site_name=#{site}",
    params: {param: '1'},
    headers: {
      'Content-Type' => 'application/json',
      'api-token' => @token
    }
  )
  response = conn.post do |req|
    req.body = {
      theme: {
        name: name
      }
    }.to_json
  end
end

#current_theme(site:) ⇒ Object



196
197
198
199
200
201
202
203
204
205
# File 'lib/pagiii/theme_sync.rb', line 196

def current_theme(site:)
  conn = Faraday.new(
    url: "#{@root_api}/api/v1/admin/themes/current?site_name=#{site}",    
    headers: {
      'Content-Type' => 'application/json',
      'api-token' => @token
    }
  )
  response = conn.get
end

#delete_remote(site, theme, asset_key) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/pagiii/theme_sync.rb', line 89

def delete_remote(site, theme, asset_key)
  puts "deleting #{asset_key}"
  response = get_theme_by_name(site: site, name: theme)
  check_authorized(response)
  theme_id = JSON.parse(response.body)['theme']['id']
  
  conn = Faraday.new(
    url: @root_api,
    params: {param: '1'},
    headers: {
      'Content-Type' => 'application/json',
      'api-token' => @token
    }
  )
  response = conn.delete("/api/v1/admin/themes/#{theme_id}/assets?key=#{asset_key}&site_name=#{site}")
end

#delete_theme(site, name) ⇒ Object



243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/pagiii/theme_sync.rb', line 243

def delete_theme(site, name)
  response = get_theme_by_name(site: site, name: name)
  check_authorized(response)
  parsed_response = JSON.parse(response.body)
  conn = Faraday.new(
    url: "#{root_api}/api/v1/admin/themes/#{parsed_response['theme']['id']}?site_name=#{site}",
    params: {param: '1'},
    headers: {
      'Content-Type' => 'application/json',
      'api-token' => @token
    }
  )
  response = conn.delete
end

#get_asset_key(file) ⇒ Object



140
141
142
143
144
# File 'lib/pagiii/theme_sync.rb', line 140

def get_asset_key(file)
  folder, file = File.split(file)
  dir = File.split(folder).last
  "#{dir}/#{file}"    
end

#get_theme_by_name(site:, name:) ⇒ Object



185
186
187
188
189
190
191
192
193
194
# File 'lib/pagiii/theme_sync.rb', line 185

def get_theme_by_name(site:, name:)
  conn = Faraday.new(
    url: "#{@root_api}/api/v1/admin/themes/by_name?name=#{name}&site_name=#{site}",    
    headers: {
      'Content-Type' => 'application/json',
      'api-token' => @token
    }
  )
  response = conn.get
end

#list_themes(site_name) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/pagiii/theme_sync.rb', line 13

def list_themes(site_name)
  conn = Faraday.new(
    url: "#{root_api}/api/v1/admin/themes?site_name=#{site_name}",
    headers: {
      'Content-Type' => 'application/json',
      'api-token' => @token
    }
  )    
  response = conn.get
  check_authorized(response)
  if response.status == 200        
    hash = JSON.parse(response.body)
    say "List themes at #{hash['site']['name']}"
    say "--------------------------------------"      
    hash['themes'].each do |theme|
      say " - #{theme['name']}  #{(theme['id'] == hash['current']['id'])? "(active)" : '' }"
    end
  elsif response.status == 401
    say "Unauthorized for listing themes"
  else
    say "error #{response.status}"
  end
end

#pull_theme(site, name) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/pagiii/theme_sync.rb', line 209

def pull_theme(site, name)
  response = get_theme_by_name(site: site, name: name)
  check_authorized(response)
  parsed_response = JSON.parse(response.body)

  conn = Faraday.new(
    url: "#{root_api}/api/v1/admin/themes/#{parsed_response['theme']['id']}/pull?site_name=#{site}",
    params: {param: '1'},
    headers: {
      'Content-Type' => 'application/json',
      'api-token' => @token
    }
  )
  response = conn.get

  assets = JSON.parse(response.body)['assets']

  assets.each do |asset|
    say "downloading #{asset['asset_key']}"
    response = download_asset(site: site, theme_id: parsed_response['theme']['id'], key: asset['asset_key'])
    path = File.split(asset['asset_key'])
    FileUtils.mkdir(path.first) unless Dir.exist?(path.first)
    File.open(asset['asset_key'], 'wb') do |f|
      f.write(response.body)
    end        
  end
  
  if response.status == 200
    say "theme created"
  else
    say response.body
  end
end

#set_current_theme(site, name) ⇒ Object



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/pagiii/theme_sync.rb', line 276

def set_current_theme(site, name)
  response = get_theme_by_name(site: site, name: name)
  check_authorized(response)
  parsed_response = JSON.parse(response.body)
  conn = Faraday.new(
    url: "#{root_api}/api/v1/admin/themes/#{parsed_response['theme']['id']}/set_current?site_name=#{site}",
    params: {param: '1'},
    headers: {
      'Content-Type' => 'application/json',
      'api-token' => @token
    }
  )
  response = conn.patch do |req|
    req.body = {}.to_json
  end
end

#sync_all(site) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/pagiii/theme_sync.rb', line 146

def sync_all(site)
  response = current_theme(site: site)
  check_authorized(response)
  theme = JSON.parse(response.body)['theme']['name']
  compare_and_delete(site, theme)    
  Dir.entries('.').each do |dir|
    if ALLOWED_DIRS.include?(dir) && File.directory?(dir)
      puts "--#{dir}"
      # skip files starts_with dot
      Dir.entries(dir).reject{|x| x =~ /(^\.)/}.each do |file|
        print "----#{file}"          
        file_path = "./#{dir}/#{file}"                    
        resp = sync_file(site, theme, file_path)
        if resp.status == 200
          puts " [OK]"
        else
          json = JSON.parse(resp.body)
          puts " [FAILED] #{json['message']}"
        end
      end
    end
  end
end

#sync_delete(site, theme_name, file) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/pagiii/theme_sync.rb', line 72

def sync_delete(site, theme_name, file)
  puts "deleteing #{file}"
  response = get_theme_by_name(site: site, name: theme_name)
  check_authorized(response)
  theme_id = JSON.parse(response.body)['theme']['id']
  
  conn = Faraday.new(
    url: @root_api,
    params: {param: '1'},
    headers: {
      'Content-Type' => 'application/json',
      'api-token' => @token
    }
  )
  response = conn.delete("/api/v1/admin/themes/#{theme_id}/assets?key=#{get_asset_key(file)}&site_name=#{site}")
end

#sync_file(site, theme_name, file) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/pagiii/theme_sync.rb', line 38

def sync_file(site, theme_name, file)    
  conn = Faraday.new(
    url: @root_api,
    params: {param: '1'},
    headers: {
      'Content-Type' => 'application/json',
      'api-token' => @token
    }
  )

  data = ""
  
  ext = File.extname(file)
  if [".png", ".jpg", ".jpeg", ".gif"].include?(ext)
    data = Base64.encode64(IO.read(file))      
  else
    data = IO.read(file)
  end    
  response = get_theme_by_name(site: site, name: theme_name)

  check_authorized(response)
  theme_id = JSON.parse(response.body)['theme']['id']

  response = conn.post("/api/v1/admin/themes/#{theme_id}/assets?site_name=#{site}") do |req|
    req.params['limit'] = 100
    req.body = {
      key: get_asset_key(file),        
      data: data
    }.to_json
  end
end

#watch(site) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/pagiii/theme_sync.rb', line 170

def watch(site)
  response = current_theme(site: site)
  theme_name = JSON.parse(response.body)['theme']['name']
  Filewatcher.new(ALLOWED_DIRS).watch do |changes|
    changes.each do |filename, event|
      puts "#{filename} #{event}"        
      if event == :updated || event == :created
        sync_file(site, theme_name, filename)
      elsif event == :deleted
        sync_delete(site, theme_name, filename)
      end
    end
  end
end