class Artiq::Sync::Runner
Constants
- DIRECTORY
- MAX_SIZE
- WHITELIST
Public Class Methods
execute(args)
click to toggle source
# File lib/artiq/sync/runner.rb, line 15 def execute(args) puts args config_filename = 'config.yml' if File.exist?(config_filename) config = YAML::load_file(config_filename) else config = {} print "Please enter journal's domain, theme name and your API token\n" config['domain'] = [(print 'Domain (with http://): '), gets.rstrip][1] config['theme'] = [(print 'Theme: '), gets.rstrip][1] config['access_token'] = [(print 'Access Token: '), gets.rstrip][1] File.open(config_filename, 'a') { |f| f.write config.to_yaml } end print "Domain: #{config['domain']}\n" print "Theme: #{config['theme']}\n" print "\n" if !File.exist?('assets') || !File.exist?('templates') || !File.exist?('layouts') print "Starting pull files\n" pull(config['domain'], config['theme'], config['access_token']) print "Pulling is done\n" end begin watch(config['domain'], config['theme'], config['access_token']) rescue Exception => e puts "\nExiting..." end end
pull(api_base, theme, access_token)
click to toggle source
Pulling file from remote
# File lib/artiq/sync/runner.rb, line 94 def pull(api_base, theme, access_token) # url = "#{api_base}/api/v1/themes/#{theme}/tree" url = [api_base, 'api', 'v1', 'themes', theme, 'tree'].join('/') puts url response = RestClient.get url, :access_token => access_token response_json = JSON.parse(response) response_json['files'].each do |filename| puts filename dist = filename[url.length+1, filename.length] FileUtils.mkdir_p(File.dirname(dist)) unless File.exist?(File.dirname(dist)) response = RestClient.get(filename, :access_token => access_token) { |response, request, result| response } if response.code == 200 open(dist, 'wb') do |file| file.write(response.body) end else puts response.code end end end
watch(api_base, theme, access_token)
click to toggle source
# File lib/artiq/sync/runner.rb, line 48 def watch(api_base, theme, access_token) puts "Watching: #{Dir.pwd}" listener = Listen.to(Dir.pwd, latency: 1) do |modified, added, removed| modified.each do |file| next unless allow?(file) Thread.new do puts "POST #{api_base}/api/v1/themes/#{theme}/blob#{cut(file)}\n" response = RestClient.post("#{api_base}/api/v1/themes/#{theme}/blob#{cut(file)}", {file: File.new(file, 'rb')}, :access_token => access_token) { |response, request, result| response } if response.code != 200 puts response.body end end.join end # New files added.each do |file| next unless allow?(file) Thread.new do puts "POST #{api_base}/api/v1/themes/#{theme}/blob#{cut(file)}\n" response = RestClient.post("#{api_base}/api/v1/themes/#{theme}/blob#{cut(file)}", {file: File.new(file, 'rb')}, :access_token => access_token) { |response, request, result| response } if response.code != 200 puts response.body end end.join end # Deleted files removed.each do |file| # next unless allow?(file) Thread.new do puts "DELETE #{api_base}/api/v1/themes/#{theme}/blob#{cut(file)}\n" response = RestClient.delete("#{api_base}/api/v1/themes/#{theme}/blob#{cut(file)}", :access_token => access_token) { |response, request, result| response } if response.code != 200 puts response.body end end.join end end listener.start sleep end
Private Class Methods
allow?(file)
click to toggle source
Allow this file?
# File lib/artiq/sync/runner.rb, line 125 def allow?(file) if File.size(file) > MAX_SIZE puts "Too large, skip: #{file}" return false end if !DIRECTORY.include?(file[Dir.pwd.length+1, file.length].split('/').first) puts "Directory not allowed: #{file}" return false end if !WHITELIST.include?(File.extname(file)) puts "Extension not allowed: #{file}" return false end true end
cut(file, pwd = Dir.pwd)
click to toggle source
Cutting this filename
# File lib/artiq/sync/runner.rb, line 120 def cut(file, pwd = Dir.pwd) file[pwd.length, file.length] end