surely 0.2.1 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -8,17 +8,12 @@ Surely is a daemon that watches your screenshots directory and upload new files
8
8
  # Install the gem
9
9
  $ gem install surely
10
10
 
11
- # Configure it (environment variables sucks but that’ll do for now)
12
- # Create an imgur application here: https://api.imgur.com/oauth2/addclient
13
- export IMGUR_CLIENT_ID=…
14
- export IMGUR_CLIENT_SECRET=…
15
-
16
11
  # Start it
17
12
  # The first time you run it, it will open a browser window and ask you to
18
13
  # enter the OAuth credentials that you will obtain after allowing your imgur app
19
14
  $ surely start
20
15
 
21
- # After that, you can kill it (^C) and start it as a real daemon
16
+ # After that, you can kill it (^C) (or `ps -ef | grep surely` and `kill` its PID) and start it as a real daemon
22
17
  $ surely start -d
23
18
  ```
24
19
 
data/lib/surely/daemon.rb CHANGED
@@ -2,14 +2,17 @@ module Surely
2
2
  class Daemon
3
3
  def initialize(env)
4
4
  @env = env
5
+ settings_file = File.join(@env['HOME'], '.surely.yml')
6
+ @settings = File.exists?(settings_file) ? YAML.load_file(settings_file) : {}
5
7
  end
6
8
 
7
9
  def start
8
- directory = @env['SURELY_DIRECTORY']
10
+ directory = @settings['directory']
9
11
  directory ||= `defaults read com.apple.screencapture location 2> /dev/null`.chomp
10
12
  directory = "#{@env['HOME']}/Desktop" if directory.empty?
11
13
 
12
- @uploader = Uploader.new(@env, directory)
14
+ @uploader = Uploader.new(@env, @settings, directory)
15
+ @uploader.add_client!
13
16
  @uploader.authorize!
14
17
 
15
18
  Raad::Logger.info "Listening to changes on #{directory}..."
@@ -1,8 +1,10 @@
1
1
  module Surely
2
2
  class Uploader
3
- def initialize(env, directory)
4
- @access_token = File.read("#{env['HOME']}/.surely_access_token").chomp rescue nil
5
- @refresh_token = File.read("#{env['HOME']}/.surely_refresh_token").chomp rescue nil
3
+ def initialize(env, settings, directory)
4
+ @access_token = settings['access_token']
5
+ @refresh_token = settings['refresh_token']
6
+ @imgur_client_id = settings['imgur_client_id']
7
+ @imgur_client_secret = settings['imgur_client_secret']
6
8
 
7
9
  @imgur = Faraday.new(url: 'https://api.imgur.com') do |c|
8
10
  c.request :multipart
@@ -11,6 +13,7 @@ module Surely
11
13
  end
12
14
 
13
15
  @env = env
16
+ @settings = settings
14
17
  @directory = directory
15
18
  end
16
19
 
@@ -32,17 +35,40 @@ module Surely
32
35
  def update_tokens(token_data)
33
36
  @access_token = token_data['access_token']
34
37
  @refresh_token = token_data['refresh_token']
35
- File.open("#{@env['HOME']}/.surely_access_token", 'w') { |f| f << @access_token }
36
- File.open("#{@env['HOME']}/.surely_refresh_token", 'w') { |f| f << @refresh_token }
38
+ save_settings
39
+ end
40
+
41
+ def save_settings
42
+ @settings = @settings.merge({
43
+ 'imgur_client_id' => @imgur_client_id,
44
+ 'imgur_client_secret' => @imgur_client_secret,
45
+ 'access_token' => @access_token,
46
+ 'refresh_token' => @refresh_token,
47
+ })
48
+
49
+ File.open("#{@env['HOME']}/.surely.yml", 'w') { |f| f << @settings.to_yaml }
50
+ end
51
+
52
+ def add_client!
53
+ return true if @imgur_client_id && @imgur_client_secret
54
+
55
+ puts 'Create an imgur application here: https://api.imgur.com/oauth2/addclient'
56
+ print 'Enter your application client_id: '
57
+ @imgur_client_id = STDIN.gets.chomp
58
+ print 'Enter your application client_secret: '
59
+ @imgur_client_secret = STDIN.gets.chomp
60
+
61
+ save_settings
37
62
  end
38
63
 
39
64
  def authorize!
40
65
  return true if @access_token && @refresh_token
41
- system "open 'https://api.imgur.com/oauth2/authorize?client_id=#{@env['IMGUR_CLIENT_ID']}&response_type=token'"
66
+
67
+ system "open 'https://api.imgur.com/oauth2/authorize?client_id=#{@imgur_client_id}&response_type=token'"
42
68
  print 'Enter your access_token: '
43
- access_token = gets.chomp
69
+ access_token = STDIN.gets.chomp
44
70
  print 'Enter your refresh_token: '
45
- refresh_token = gets.chomp
71
+ refresh_token = STDIN.gets.chomp
46
72
  update_tokens('access_token' => access_token, 'refresh_token' => refresh_token)
47
73
  end
48
74
 
@@ -50,8 +76,8 @@ module Surely
50
76
  response = @imgur.post '/oauth2/token' do |r|
51
77
  r.body = {
52
78
  refresh_token: @refresh_token,
53
- client_id: @env['IMGUR_CLIENT_ID'],
54
- client_secret: @env['IMGUR_CLIENT_SECRET'],
79
+ client_id: @imgur_client_id,
80
+ client_secret: @imgur_client_secret,
55
81
  grant_type: 'refresh_token'
56
82
  }
57
83
  end
@@ -1,3 +1,3 @@
1
1
  module Surely
2
- VERSION = '0.2.1'
2
+ VERSION = '0.3'
3
3
  end
data/lib/surely.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'yaml'
1
2
  require 'raad'
2
3
  require 'faraday'
3
4
  require 'listen'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: surely
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: '0.3'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -142,7 +142,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
142
142
  version: '0'
143
143
  segments:
144
144
  - 0
145
- hash: -13490925257366599
145
+ hash: -478749530698552417
146
146
  required_rubygems_version: !ruby/object:Gem::Requirement
147
147
  none: false
148
148
  requirements:
@@ -151,7 +151,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
151
151
  version: '0'
152
152
  segments:
153
153
  - 0
154
- hash: -13490925257366599
154
+ hash: -478749530698552417
155
155
  requirements: []
156
156
  rubyforge_project:
157
157
  rubygems_version: 1.8.23