gyaazle 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,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c0d9ae977f0adc26ee26ed3824260bcab996e09e
4
+ data.tar.gz: cd246022a2a2ad71637ca3db31e4a7007d4ad4bc
5
+ SHA512:
6
+ metadata.gz: fa2f348c040367ccf160e2bbe435ddb9442c18f13064af2f5f167b92e659a4e8eb2685356ca978635c410f4f7aaeb5cd3062175cfdc2927ddfd5889f088a14ca
7
+ data.tar.gz: 3f92778199e8963581e35437af28a71f00f6aea0dd9a597909527c77dd8b8707170f622705875707f5fb30bc7a6363605881f27fb49786d76b270068e1095ca8
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in gyaazle.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 uu59
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Gyaazle
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'gyaazle'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install gyaazle
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/gyaazle ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.expand_path("../../lib/gyaazle.rb", __FILE__)
4
+ require "gyaazle"
5
+
6
+ Gyaazle::CLI.new(ARGV).run!
data/gyaazle.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'gyaazle/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "gyaazle"
8
+ spec.version = Gyaazle::VERSION
9
+ spec.authors = ["uu59"]
10
+ spec.email = ["k@uu59.org"]
11
+ spec.description = %q{Gyazo like image uploader to Google Drive}
12
+ spec.summary = %q{Gyazo like image uploader to Google Drive}
13
+ spec.homepage = "https://github.com/uu59/gyaazle"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "httpclient"
22
+ spec.add_dependency "multi_json"
23
+ spec.add_dependency "nokogiri"
24
+ spec.add_dependency "trollop"
25
+ spec.add_dependency "launchy"
26
+ spec.add_development_dependency "pry"
27
+ spec.add_development_dependency "bundler", "~> 1.3"
28
+ spec.add_development_dependency "rake"
29
+ end
@@ -0,0 +1,84 @@
1
+ module Gyaazle
2
+ class CLI
3
+ def initialize(argv)
4
+ @argv = argv
5
+ @opts = Trollop.options(@argv) do
6
+ version "Gyaazle #{Gyaazle::VERSION}"
7
+ banner <<TEXT
8
+ Gyaazle #{Gyaazle::VERSION}
9
+
10
+ Upload file(s) to Google Drive.
11
+ See https://github.com/uu59/gyaazle for more information.
12
+
13
+ Usage: #{$0} [options] <file1> <file2> ..
14
+
15
+ [options] are:
16
+ TEXT
17
+ opt :config, "Use config file", :default => File.join(ENV["HOME"], ".gyaazle", "config.json"), :short => :none
18
+ opt :edit, "Edit config file by $EDITOR", :type => :boolean, :default => false
19
+ opt :capture, "Capture screenshot to go upload", :type => :boolean, :default => false, :short => "-c"
20
+ opt :open, "Open uploaded file by browser", :type => :boolean, :default => false
21
+ end
22
+ @config = Config.new(@opts[:config])
23
+ @client = Client.new(@config)
24
+ end
25
+
26
+ def run!
27
+ if @opts[:edit]
28
+ edit_config
29
+ else
30
+ if @opts[:capture]
31
+ @argv = [capture]
32
+ end
33
+ upload
34
+ end
35
+ end
36
+
37
+ def upload
38
+ if @config.load.nil? || @config.load[:refresh_token].nil?
39
+ credentials = initialize_tokens
40
+ else
41
+ @client.refresh_token!
42
+ credentials = @client.credentials
43
+ end
44
+ @argv.each do |file|
45
+ fileobj = @client.upload(file)
46
+ puts "#{file}: #{fileobj[:alternateLink]}"
47
+ if @opts[:open]
48
+ Launchy.open fileobj[:alternateLink]
49
+ end
50
+ @client.set_permissions(fileobj[:id])
51
+ end
52
+ end
53
+
54
+ def capture
55
+ tmpfile = "/tmp/gyaazle_capture_#{Time.now.strftime("%F %T")}.png"
56
+ capture_cmd = case RUBY_PLATFORM
57
+ when /darwin/
58
+ "screencapture -i '#{tmpfile}'"
59
+ else
60
+ "import '#{tmpfile}'"
61
+ end
62
+ system capture_cmd
63
+ tmpfile
64
+ end
65
+
66
+ def edit_config
67
+ system(ENV["EDITOR"], @config.file)
68
+ puts "Updated!"
69
+ end
70
+
71
+ def initialize_tokens
72
+ tokens = @client.get_tokens(authorize)
73
+ @config.save(tokens)
74
+ tokens
75
+ end
76
+
77
+ def authorize
78
+ puts "Open this link by your browser, and authorize"
79
+ puts @client.authorize_url
80
+ print "Paste code here: "
81
+ STDIN.gets.strip
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,137 @@
1
+ module Gyaazle
2
+ class Client
3
+ attr_reader :client, :config
4
+
5
+ def initialize(config)
6
+ @config = config
7
+ @client = HTTPClient.new
8
+ end
9
+
10
+ def authorize(code)
11
+ json = client.post_content("https://accounts.google.com/o/oauth2/token", {
12
+ :code => code,
13
+ :client_id => config.id,
14
+ :client_secret => config.secret,
15
+ :redirect_uri => "urn:ietf:wg:oauth:2.0:oob",
16
+ :grant_type => "authorization_code",
17
+ })
18
+ MultiJson.load(json, :symbolize_keys => true)
19
+ end
20
+
21
+ def authorize_url
22
+ url = "https://accounts.google.com/o/oauth2/auth?client_id=#{config.id}&response_type=code&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=https://www.googleapis.com/auth/drive"
23
+ Nokogiri::HTML.parse(client.get(url).body).at('a').attributes["href"].to_s
24
+ end
25
+
26
+ def get_tokens(verifier)
27
+ client.post_content("https://accounts.google.com/o/oauth2/token",{
28
+ :code => verifier,
29
+ :client_id => config.id,
30
+ :client_secret => config.secret,
31
+ :redirect_uri => "urn:ietf:wg:oauth:2.0:oob",
32
+ :grant_type => "authorization_code",
33
+ })
34
+ end
35
+
36
+ def refresh_token!
37
+ json = client.post("https://accounts.google.com/o/oauth2/token", {
38
+ :refresh_token => credentials[:refresh_token],
39
+ :client_id => config.id,
40
+ :client_secret => config.secret,
41
+ :grant_type => "refresh_token",
42
+ }).body
43
+ config.update(:access_token => MultiJson.load(json)["access_token"])
44
+ config.load
45
+ end
46
+
47
+ def upload(file, metadata = nil)
48
+ body = [
49
+ {
50
+ 'Content-Type' => 'application/json;charset=utf-8',
51
+ :content => MultiJson.dump(metadata || {
52
+ :title => File.basename(file),
53
+ :shared => "true",
54
+ :parents => [:id => folder_id]
55
+ })
56
+ },
57
+ {
58
+ :content => File.read(file),
59
+ },
60
+ ]
61
+
62
+ response = client.post(
63
+ 'https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart',
64
+ body,
65
+ {
66
+ "Authorization" => "#{credentials[:token_type]} #{credentials[:access_token]}",
67
+ "Content-Type" => "multipart/related; boundary=___#{Time.now.to_f}___",
68
+ }
69
+ )
70
+ MultiJson.load(response.body, :symbolize_keys => true)
71
+ end
72
+
73
+ def set_permissions(file_id, permissions = nil)
74
+ json = MultiJson.dump(permissions || {
75
+ :role => "reader",
76
+ :type => "#{"anyone"}",
77
+ :value => "#{"me"}",
78
+ :withLink => "true",
79
+ :additionalRoles => ["commenter"],
80
+ })
81
+ client.post_content(
82
+ "https://www.googleapis.com/drive/v2/files/#{file_id}/permissions",
83
+ json,
84
+ {
85
+ "Authorization" => "#{credentials[:token_type]} #{credentials[:access_token]}",
86
+ 'Content-Type' => 'application/json;charset=utf-8',
87
+ }
88
+ )
89
+ end
90
+
91
+ def get(file_id)
92
+ json = client.get(
93
+ "https://www.googleapis.com/drive/v2/files/#{file_id}",
94
+ {},
95
+ {
96
+ "Authorization" => "#{credentials[:token_type]} #{credentials[:access_token]}",
97
+ }
98
+ ).body
99
+
100
+ MultiJson.load(json, :symbolize_keys => true)
101
+ end
102
+
103
+ def folder_id
104
+ id = credentials[:folder_id]
105
+ return create_folder("Gyaazle") unless id
106
+
107
+ folder = get(id)
108
+ if !folder[:id] || folder[:labels][:trashed]
109
+ create_folder("Gyaazle")
110
+ else
111
+ id
112
+ end
113
+ end
114
+
115
+ def create_folder(name)
116
+ json = client.post_content(
117
+ "https://www.googleapis.com/drive/v2/files",
118
+ MultiJson.dump({
119
+ :title => name,
120
+ :mimeType => "application/vnd.google-apps.folder",
121
+ :parents => [{:id => "root"}],
122
+ }),
123
+ {
124
+ "Authorization" => "#{credentials[:token_type]} #{credentials[:access_token]}",
125
+ 'Content-Type' => 'application/json;charset=utf-8',
126
+ }
127
+ )
128
+ folder = MultiJson.load(json, :symbolize_keys => true)
129
+ config.update(:folder_id => folder[:id])
130
+ folder[:id]
131
+ end
132
+
133
+ def credentials
134
+ config.load
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,31 @@
1
+ module Gyaazle
2
+ class Config
3
+ CLIENT_ID = "810975313441.apps.googleusercontent.com"
4
+ CLIENT_SECRET = "0CJJ4jT2jcUsCWDrsHvXmARs"
5
+
6
+ attr_reader :id, :secret, :file
7
+
8
+ def initialize(file = nil)
9
+ @id = CLIENT_ID
10
+ @secret = CLIENT_SECRET
11
+ @file = file
12
+ dir = File.dirname(@file)
13
+ FileUtils.mkdir_p(dir) unless File.directory?(dir)
14
+ FileUtils.touch(file) unless File.exists?(file)
15
+ end
16
+
17
+ def save(json)
18
+ File.open(file, "w") do |f|
19
+ f.write json.is_a?(String) ? json : MultiJson.dump(json, :pretty => true)
20
+ end
21
+ end
22
+
23
+ def update(values)
24
+ save self.load.merge(values)
25
+ end
26
+
27
+ def load
28
+ MultiJson.load(File.read(file), :symbolize_keys => true) rescue nil
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,3 @@
1
+ module Gyaazle
2
+ VERSION = "0.0.1"
3
+ end
data/lib/gyaazle.rb ADDED
@@ -0,0 +1,16 @@
1
+ require "httpclient"
2
+ require "multi_json"
3
+ require "nokogiri"
4
+ require "trollop"
5
+ require "launchy"
6
+
7
+ dir = File.dirname(__FILE__)
8
+ $LOAD_PATH.unshift dir unless $LOAD_PATH.include?(dir)
9
+ require "gyaazle/version"
10
+ require "gyaazle/cli"
11
+ require "gyaazle/client"
12
+ require "gyaazle/config"
13
+
14
+ module Gyaazle
15
+ # Your code goes here...
16
+ end
metadata ADDED
@@ -0,0 +1,170 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gyaazle
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - uu59
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httpclient
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: multi_json
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: nokogiri
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: trollop
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: launchy
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: bundler
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ~>
102
+ - !ruby/object:Gem::Version
103
+ version: '1.3'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ~>
109
+ - !ruby/object:Gem::Version
110
+ version: '1.3'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rake
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: Gyazo like image uploader to Google Drive
126
+ email:
127
+ - k@uu59.org
128
+ executables:
129
+ - gyaazle
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - .gitignore
134
+ - Gemfile
135
+ - LICENSE.txt
136
+ - README.md
137
+ - Rakefile
138
+ - bin/gyaazle
139
+ - gyaazle.gemspec
140
+ - lib/gyaazle.rb
141
+ - lib/gyaazle/cli.rb
142
+ - lib/gyaazle/client.rb
143
+ - lib/gyaazle/config.rb
144
+ - lib/gyaazle/version.rb
145
+ homepage: https://github.com/uu59/gyaazle
146
+ licenses:
147
+ - MIT
148
+ metadata: {}
149
+ post_install_message:
150
+ rdoc_options: []
151
+ require_paths:
152
+ - lib
153
+ required_ruby_version: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ required_rubygems_version: !ruby/object:Gem::Requirement
159
+ requirements:
160
+ - - '>='
161
+ - !ruby/object:Gem::Version
162
+ version: '0'
163
+ requirements: []
164
+ rubyforge_project:
165
+ rubygems_version: 2.0.2
166
+ signing_key:
167
+ specification_version: 4
168
+ summary: Gyazo like image uploader to Google Drive
169
+ test_files: []
170
+ has_rdoc: