Skip to content

Instantly share code, notes, and snippets.

@ihatov08
Last active July 11, 2019 01:18
Show Gist options
  • Select an option

  • Save ihatov08/b29d8df9becb4d36d7f62a89065f7010 to your computer and use it in GitHub Desktop.

Select an option

Save ihatov08/b29d8df9becb4d36d7f62a89065f7010 to your computer and use it in GitHub Desktop.
require 'open-uri'
# ref: http://www.singlecolorimage.com/api.html
class SingleColorImageApi
BASE_URL = "http://singlecolorimage.com/get"
def initialize(color_6_digest_hex:, width:, height:, download_path: nil)
@color_6_digest_hex = color_6_digest_hex
@width = width
@height = height
@download_path = download_path
end
def self.download(**args)
new(args).download
end
def download
open(file_path, 'w') do |f|
open(request_uri) do |data|
f.write(data.read)
end
end
end
private
attr_reader :color_6_digest_hex, :width, :height, :download_path
def image_size
"#{width}x#{height}"
end
def file_name
"#{color_6_digest_hex}-#{image_size}.png"
end
def file_path
File.join(donwload_dir, file_name)
end
def request_uri
URI.join("#{BASE_URL}/", "#{color_6_digest_hex}/", image_size).tap{|t| puts t}
end
def donwload_dir
download_path || FileUtils.mkdir_p(File.join("/tmp", "simple_images")).join
end
end
# ex
# SingleColorImageApi.download(color_6_digest_hex: "33fd8f", width: "400", height: "100")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment