Skip to content

Instantly share code, notes, and snippets.

@telegraham
Created July 21, 2017 22:12
Show Gist options
  • Save telegraham/d595ac043213204407e64e9918ab7082 to your computer and use it in GitHub Desktop.
Save telegraham/d595ac043213204407e64e9918ab7082 to your computer and use it in GitHub Desktop.
Update cachemonet.saver (run this in cachemonet.saver/Contents/Resources)
require 'net/http'
require 'net/https'
require 'json'
require "open-uri"
class CacheMonetGif
attr_reader :remote_path
def initialize(remote_path)
@remote_path = remote_path
end
def local_path
#por ejemplo: src/center/walkman.gif
#to: gif/center/walkman.gif
remote_path.sub("src", "gif")
end
end
def get_json(whichFile)
response = Net::HTTP.get_response("cachemonet.com","/json/#{whichFile}")
JSON.parse(response.body)
end
def get_image(path)
open("http://cachemonet.com/#{path}").read
end
def write_file(path, data)
File.open(path, 'wb' ) do |output|
output.write data
end
end
def do_it_all(remote_file)
gifs = get_json(remote_file).map { |path|
CacheMonetGif.new(path);
};
missing = gifs.reject { |gif|
File.file?(gif.local_path)
};
missing.each { |gif|
write_file(gif.local_path, get_image(gif.remote_path))
puts "#{gif.remote_path} downloaded to #{gif.local_path}"
}
gifs
end
def local_list(gifs)
gifs.map { |gif| gif.local_path }.to_json
end
bg_result = do_it_all("bg.json")
center_result = do_it_all("center.json")
puts "bg json:"
puts local_list(bg_result)
puts "\n"
puts "center json:"
puts local_list(center_result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment