surely 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ YWUwOGJiZGM3YzZlMTUwZDdiMzZkOTk5ZDA5ZTQ5YjQ2YzY5MGE4NA==
5
+ data.tar.gz: !binary |-
6
+ ODY2YTJhYWYyODY3ZGM5ZmQ0YjhiZDA2ZTg5MTY1MWQ5MmU5NjY5NQ==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ NDc3OTFjNzIyZjEyOGI5MzJiZTc2NDU5NzEyMjUyZjgzNWEwZWI2OGY4ZDgz
10
+ MWMwNjg0MWUzYjhiOTY1Njg4Yjk3NzEzNTNmZWM1YzJiN2I1ZTY3NGQ4OWYx
11
+ NGUzYTNjMDllMTI4YTc2N2UxMGQyMGNhODM5NTU3NTg0ZTdmN2I=
12
+ data.tar.gz: !binary |-
13
+ YmZhM2RhODllMmY4MjQ4NmQ3NzZhZGJiNmRhZjQ4YThlNmMxOTMyNTYyOTM3
14
+ ZTk3M2Q3ZTIxNTdhYWQyMTM3MTdmYzVlYTlhNTYwZTg0YzExNDg4NGM2OTQz
15
+ NmI2ZGQ0ODBiOGMyODM5YmI1NjAwZjhkMzliY2NhZGM3MjRkZGQ=
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ /Gemfile.lock
2
+ /pkg
3
+ /tmp
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/README.md ADDED
@@ -0,0 +1,24 @@
1
+ # Surely
2
+
3
+ # Usage
4
+
5
+ Get the gem:
6
+
7
+ ```shell
8
+ $ gem install surely
9
+ $ cd surely
10
+ ```
11
+
12
+ Configure it:
13
+
14
+ ```
15
+ export DIRECTORY=/path/to/screenshots
16
+ export IMGUR_CLIENT_ID=…
17
+ export IMGUR_CLIENT_SECRET=…
18
+ ```
19
+
20
+ Start the daemon:
21
+
22
+ ```
23
+ $ surely
24
+ ```
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require "bundler"
2
+ require "rake"
3
+ require "bundler/gem_tasks"
data/bin/surely ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $:.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
4
+
5
+ require 'surely'
6
+
7
+ Surely::Daemon.new(ARGV, ENV)
@@ -0,0 +1,32 @@
1
+ module Surely
2
+ class Daemon
3
+ def initialize(argv, env)
4
+ @uploader = Uploader.new(env)
5
+ @uploader.authorize!
6
+
7
+ callback = Proc.new do |modified, added, removed|
8
+ if added.any?
9
+ puts "Uploading..."
10
+ @uploader.refresh_token!
11
+
12
+ if uploaded_file = @uploader.upload_file(File.join(env['DIRECTORY'], added.first))
13
+ puts "Done uploading #{uploaded_file['link']}"
14
+ system "say -v 'Fred' 'Uploaded'"
15
+ system "echo #{uploaded_file['link']} | pbcopy"
16
+ end
17
+ end
18
+ end
19
+
20
+ listener = Listen.to(env["DIRECTORY"])
21
+ listener.filter(/\.png$/)
22
+ listener.change(&callback)
23
+
24
+ begin
25
+ puts "Listening to changes on #{env["DIRECTORY"]}..."
26
+ listener.start!
27
+ ensure
28
+ exit
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,66 @@
1
+ module Surely
2
+ class Uploader
3
+ def initialize(env)
4
+ @access_token = File.read("#{env['HOME']}/.surely_access_token") rescue nil
5
+ @refresh_token = File.read("#{env['HOME']}/.surely_refresh_token") rescue nil
6
+
7
+ @imgur = Faraday.new(:url => 'https://api.imgur.com/3/') do |c|
8
+ c.request :multipart
9
+ c.request :url_encoded
10
+ c.adapter :net_http
11
+ end
12
+
13
+ @env = env
14
+ end
15
+
16
+ def upload_file(file)
17
+ file = Faraday::UploadIO.new(file, 'image/png')
18
+
19
+ response = @imgur.post 'image' do |r|
20
+ r.headers['Authorization'] = "Bearer #{@access_token}"
21
+ r.body = { image: file }
22
+ end
23
+
24
+ if response.success?
25
+ MultiJson.load(response.body)["data"]
26
+ else
27
+ nil
28
+ end
29
+ end
30
+
31
+ def update_tokens(token_data)
32
+ @access_token = token_data["access_token"]
33
+ @refresh_token = token_data["refresh_token"]
34
+ File.open("#{@env['HOME']}/.surely_access_token", 'w') { |f| f << @access_token }
35
+ File.open("#{@env['HOME']}/.surely_refresh_token", 'w') { |f| f << @refresh_token }
36
+ end
37
+
38
+ def authorize!
39
+ return true if @access_token && @refresh_token
40
+ system "open 'https://api.imgur.com/oauth2/authorize?client_id=#{@env['IMGUR_CLIENT_ID']}&response_type=token'"
41
+ print "Enter your access_token: "
42
+ access_token = gets.chomp
43
+ print "Enter your refresh_token: "
44
+ refresh_token = gets.chomp
45
+ update_tokens({ "access_token" => access_token, "refresh_token" => refresh_token })
46
+ end
47
+
48
+ def refresh_token!
49
+ response = @imgur.post 'oauth2/token' do |r|
50
+ r.body = {
51
+ refresh_token: @refresh_token,
52
+ client_id: @env['IMGUR_CLIENT_ID'],
53
+ client_secret: @env['IMGUR_CLIENT_SECRET'],
54
+ grant_type: 'refresh_token'
55
+ }
56
+ end
57
+
58
+ if response.success?
59
+ token_data = MultiJson.load(response.body)
60
+ update_tokens(token_data)
61
+ end
62
+
63
+ response
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,3 @@
1
+ module Surely
2
+ VERSION = '0.0.1'
3
+ end
data/lib/surely.rb ADDED
@@ -0,0 +1,8 @@
1
+ # encoding: utf-8
2
+ require 'faraday'
3
+ require 'listen'
4
+ require 'multi_json'
5
+ require 'yajl'
6
+
7
+ require 'surely/daemon'
8
+ require 'surely/uploader'
data/surely.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "surely/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "surely"
7
+ s.version = Surely::VERSION
8
+ s.authors = ["Rémi Prévost"]
9
+ s.email = ["remi@exomel.com"]
10
+ s.homepage = "http://github.com/remiprev/surely"
11
+ s.license = "MIT"
12
+ s.summary = "Surely watches your screenshots directory and upload new files to your imgur account."
13
+ s.description = "Surely watches your screenshots directory and upload new files to your imgur account."
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_runtime_dependency "faraday"
21
+ s.add_runtime_dependency "listen"
22
+ s.add_runtime_dependency "multi_json"
23
+ s.add_runtime_dependency "yajl-ruby"
24
+
25
+ s.add_development_dependency "rake"
26
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: surely
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Rémi Prévost
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: listen
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: multi_json
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: yajl-ruby
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Surely watches your screenshots directory and upload new files to your
84
+ imgur account.
85
+ email:
86
+ - remi@exomel.com
87
+ executables:
88
+ - surely
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - .gitignore
93
+ - Gemfile
94
+ - README.md
95
+ - Rakefile
96
+ - bin/surely
97
+ - lib/surely.rb
98
+ - lib/surely/daemon.rb
99
+ - lib/surely/uploader.rb
100
+ - lib/surely/version.rb
101
+ - surely.gemspec
102
+ homepage: http://github.com/remiprev/surely
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ! '>='
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ! '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.0.2
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: Surely watches your screenshots directory and upload new files to your imgur
126
+ account.
127
+ test_files: []