Created
October 4, 2013 02:30
-
-
Save ltk/6820112 to your computer and use it in GitHub Desktop.
A simple screen shot uploader. New screen shots are uploaded to your own S3 bucket, their URLs are copied to your clipboard, and you receive a OS X notification that links directly to the file on S3 in Chrome.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'aws-sdk' | |
require 'listen' | |
require 'pathname' | |
require 'terminal-notifier' | |
module Kloud | |
class Application | |
attr_reader :directory, :screen_shots | |
def initialize(directory) | |
@directory = directory | |
@screen_shots = [] | |
end | |
def run | |
Thread.new { listener.start } | |
sleep | |
end | |
private | |
def listener | |
@listener ||= Listen.to(directory) do |modified, added, removed| | |
added.each do |file| | |
screen_shots << ScreenShot.new(file).tap do |ss| | |
perform actions_for(ss) | |
end | |
end | |
end | |
end | |
def perform(actions) | |
actions.each(&:perform) | |
end | |
def actions_for(screen_shot) | |
[ | |
Action::Upload.new(screen_shot), | |
Action::CopyUrlToClipBoard.new(screen_shot), | |
Action::SendNotification.new(screen_shot), | |
Action::TerminalOutput.new(screen_shot) | |
] | |
end | |
end | |
class ScreenShot | |
attr_reader :file_path | |
attr_accessor :url | |
def initialize(file_path) | |
@file_path = file_path | |
end | |
def filename | |
@filename ||= Pathname.new(@file_path).basename | |
end | |
end | |
module Action | |
class CopyUrlToClipBoard | |
attr_reader :screen_shot | |
def initialize(screen_shot) | |
@screen_shot = screen_shot | |
end | |
def perform | |
IO.popen('pbcopy', 'w') { |f| f << screen_shot.url } | |
end | |
end | |
class Upload | |
AWS_ACCESS_KEY_ID = 'your access key' | |
AWS_SECRET_ACCESS_KEY = 'your secret access key' | |
AWS.config :access_key_id => AWS_ACCESS_KEY_ID, :secret_access_key => AWS_SECRET_ACCESS_KEY | |
attr_reader :screen_shot | |
def initialize(screen_shot) | |
@screen_shot = screen_shot | |
end | |
def perform | |
s3_object.write :file => screen_shot.file_path | |
screen_shot.url = s3_object.url_for(:read) | |
end | |
private | |
def s3 | |
@s3 ||= AWS::S3.new | |
end | |
def s3_object | |
@s3_object ||= s3.buckets['kurtzkloud.com'].objects["#{Time.now.to_i}-#{screen_shot.filename}"] | |
end | |
end | |
class SendNotification | |
attr_reader :screen_shot | |
def initialize(screen_shot) | |
@screen_shot = screen_shot | |
end | |
def perform | |
TerminalNotifier.notify("In the Kloud at #{screen_shot.url}", | |
:title => 'Kloud', | |
:subtitle => screen_shot.filename.to_s, | |
:sound => 'Submarine', | |
:active => 'com.google.Chrome', | |
:sender => 'com.google.Chrome', | |
:open => URI.unescape(screen_shot.url.to_s)) | |
end | |
end | |
class TerminalOutput | |
attr_reader :screen_shot | |
def initialize(screen_shot) | |
@screen_shot = screen_shot | |
end | |
def perform | |
puts "#{screen_shot.filename} is in the Kloud." | |
end | |
end | |
end | |
end | |
Kloud::Application.new('/Users/Me/Desktop').run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment