net-github-upload 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +82 -0
- data/Rakefile +86 -0
- data/lib/net/github-upload.rb +113 -0
- data/test/github_test.rb +40 -0
- data/test/test +1 -0
- metadata +95 -0
data/README.rdoc
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
= Ruby Net::GitHub::Upload
|
2
|
+
|
3
|
+
* http://github.com/Constellation/ruby-net-github-upload
|
4
|
+
* http://gemcutter.org/gems/net-github-upload
|
5
|
+
|
6
|
+
== DESCRIPTION
|
7
|
+
|
8
|
+
Ruby Net::GitHub::Upload is upload user agent for GitHub Downloads
|
9
|
+
respect to http://github.com/typester/net-github-upload-perl
|
10
|
+
|
11
|
+
== SYNOPSIS
|
12
|
+
|
13
|
+
# require
|
14
|
+
require 'rubygems'
|
15
|
+
require 'net/github-upload'
|
16
|
+
|
17
|
+
# setup
|
18
|
+
login = `git config github.user`.chomp # your login for github
|
19
|
+
token = `git config github.token`.chomp # your token for github
|
20
|
+
repos = 'repository name' # your repos name (like 'taberareloo')
|
21
|
+
gh = Net::GitHub::Upload.new(
|
22
|
+
:login => login,
|
23
|
+
:token => token
|
24
|
+
)
|
25
|
+
|
26
|
+
# file upload
|
27
|
+
direct_link = gh.upload(
|
28
|
+
:repos => repos,
|
29
|
+
:file => 'test/test',
|
30
|
+
:description => "test file"
|
31
|
+
)
|
32
|
+
# direct link is link to Amazon S3.
|
33
|
+
# Because GitHub refrection for file changing is async,
|
34
|
+
# if you get changed file synchronously, you use this "direct_link"
|
35
|
+
|
36
|
+
# data upload
|
37
|
+
# you can define content_type => Amazon S3 Content-Type
|
38
|
+
direct_link = gh.upload(
|
39
|
+
:repos => repos,
|
40
|
+
:data => 'test',
|
41
|
+
:name => "test_#{Time.now.to_i}.txt",
|
42
|
+
:content_type => 'text/plain',
|
43
|
+
:description => "test file2"
|
44
|
+
)
|
45
|
+
|
46
|
+
== DEPENDENCY
|
47
|
+
|
48
|
+
* nokogiri
|
49
|
+
* faster_xml_simple
|
50
|
+
* httpclient
|
51
|
+
|
52
|
+
== INSTALL
|
53
|
+
|
54
|
+
gem source -a http://gemcutter.org
|
55
|
+
sudo gem install net-github-upload
|
56
|
+
|
57
|
+
== LICENSE
|
58
|
+
|
59
|
+
Ruby Net::Github::Upload
|
60
|
+
(The MIT License)
|
61
|
+
|
62
|
+
Copyright (c) 2009 Constellation
|
63
|
+
|
64
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
65
|
+
a copy of this software and associated documentation files (the
|
66
|
+
'Software'), to deal in the Software without restriction, including
|
67
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
68
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
69
|
+
permit persons to whom the Software is furnished to do so, subject to
|
70
|
+
the following conditions:
|
71
|
+
|
72
|
+
The above copyright notice and this permission notice shall be
|
73
|
+
included in all copies or substantial portions of the Software.
|
74
|
+
|
75
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
76
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
77
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
78
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
79
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
80
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
81
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
82
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
# vim: fileencoding=utf-8
|
2
|
+
require 'rubygems'
|
3
|
+
require 'rake'
|
4
|
+
require 'rake/clean'
|
5
|
+
require 'rake/testtask'
|
6
|
+
require 'rake/packagetask'
|
7
|
+
require 'rake/gempackagetask'
|
8
|
+
require 'rake/rdoctask'
|
9
|
+
require 'rake/contrib/sshpublisher'
|
10
|
+
require 'lib/net/github-upload'
|
11
|
+
|
12
|
+
$version = Net::GitHub::Upload::VERSION
|
13
|
+
$readme = 'README.rdoc'
|
14
|
+
$rdoc_opts = %W(--main #{$readme} --charset utf-8 --line-numbers --inline-source)
|
15
|
+
$name = 'net-github-upload'
|
16
|
+
$github_name = 'ruby-net-github-upload'
|
17
|
+
$summary = 'ruby porting of Net::GitHub::Upload'
|
18
|
+
$description = <<-EOS
|
19
|
+
Ruby Net::GitHub::Upload is upload user agent for GitHub Downloads
|
20
|
+
EOS
|
21
|
+
$author = 'Constellation'
|
22
|
+
$email = 'utatane.tea@gmail.com'
|
23
|
+
$page = 'http://github.com/Constellation/ruby-net-github-upload'
|
24
|
+
$rubyforge_project = 'ruby-net-github-upload'
|
25
|
+
|
26
|
+
|
27
|
+
task :default => [:test]
|
28
|
+
task :package => [:clean]
|
29
|
+
|
30
|
+
Rake::TestTask.new("test") do |t|
|
31
|
+
t.libs << "test"
|
32
|
+
t.pattern = "test/**/*_test.rb"
|
33
|
+
t.verbose = true
|
34
|
+
end
|
35
|
+
|
36
|
+
spec = Gem::Specification.new do |s|
|
37
|
+
s.name = $name
|
38
|
+
s.version = $version
|
39
|
+
s.platform = Gem::Platform::RUBY
|
40
|
+
s.has_rdoc = true
|
41
|
+
s.extra_rdoc_files = [$readme]
|
42
|
+
s.rdoc_options += $rdoc_opts
|
43
|
+
s.summary = $summary
|
44
|
+
s.description = $description
|
45
|
+
s.author = $author
|
46
|
+
s.email = $email
|
47
|
+
s.homepage = $page
|
48
|
+
s.executables = $exec
|
49
|
+
s.rubyforge_project = $rubyforge_project
|
50
|
+
s.require_path = 'lib'
|
51
|
+
s.test_files = Dir["test/*_test.rb"]
|
52
|
+
s.add_dependency('nokogiri')
|
53
|
+
s.add_dependency('faster_xml_simple')
|
54
|
+
s.add_dependency('httpclient')
|
55
|
+
s.files = %w(README.rdoc Rakefile) + Dir["{bin,test,lib}/**/*"]
|
56
|
+
end
|
57
|
+
|
58
|
+
Rake::GemPackageTask.new(spec) do |p|
|
59
|
+
p.need_tar = true
|
60
|
+
p.gem_spec = spec
|
61
|
+
end
|
62
|
+
|
63
|
+
Rake::RDocTask.new do |rdoc|
|
64
|
+
rdoc.rdoc_dir = 'doc'
|
65
|
+
rdoc.options += $rdoc_opts
|
66
|
+
rdoc.rdoc_files.include("README.rdoc", "lib/**/*.rb", "ext/**/*.c")
|
67
|
+
end
|
68
|
+
|
69
|
+
desc "gem spec"
|
70
|
+
task :gemspec do
|
71
|
+
File.open("#{$github_name}.gemspec", "wb") do |f|
|
72
|
+
f << spec.to_ruby
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
desc "gem install"
|
77
|
+
task :install => [:package] do
|
78
|
+
sh "sudo gem install pkg/#{$name}-#{$version}.gem --local"
|
79
|
+
end
|
80
|
+
|
81
|
+
desc "gem uninstall"
|
82
|
+
task :uninstall do
|
83
|
+
sh "sudo gem uninstall #{$name}"
|
84
|
+
end
|
85
|
+
|
86
|
+
# vim: syntax=ruby
|
@@ -0,0 +1,113 @@
|
|
1
|
+
require 'nokogiri'
|
2
|
+
require 'httpclient'
|
3
|
+
require 'stringio'
|
4
|
+
require 'faster_xml_simple'
|
5
|
+
|
6
|
+
module Net
|
7
|
+
module GitHub
|
8
|
+
class Upload
|
9
|
+
VERSION = '0.0.1'
|
10
|
+
def initialize params=nil
|
11
|
+
@login = params[:login]
|
12
|
+
@token = params[:token]
|
13
|
+
end
|
14
|
+
|
15
|
+
def upload info
|
16
|
+
unless info[:repos]
|
17
|
+
raise "required repository name"
|
18
|
+
end
|
19
|
+
info[:repos] = @login + '/' + info[:repos] unless info[:repos].include? '/'
|
20
|
+
|
21
|
+
if info[:file]
|
22
|
+
file = info[:file]
|
23
|
+
unless File.exist?(file) && File.readable?(file)
|
24
|
+
raise "file does not exsits or readable"
|
25
|
+
end
|
26
|
+
info[:name] ||= File.basename(file)
|
27
|
+
end
|
28
|
+
unless info[:file] || info[:data]
|
29
|
+
raise "required file or data parameter to upload"
|
30
|
+
end
|
31
|
+
|
32
|
+
unless info[:name]
|
33
|
+
raise "required name parameter for filename with data parameter"
|
34
|
+
end
|
35
|
+
|
36
|
+
if list_files(info[:repos]).any?{|obj| obj[:name] == info[:name]}
|
37
|
+
raise "file '#{info[:name]}' is already uploaded. please try different name"
|
38
|
+
end
|
39
|
+
|
40
|
+
stat = HTTPClient.post("http://github.com/#{info[:repos]}/downloads", {
|
41
|
+
"file_size" => info[:file] ? File.stat(info[:file]).size : info[:data].size,
|
42
|
+
"content_type" => info[:content_type] || 'application/octet-stream',
|
43
|
+
"file_name" => info[:name],
|
44
|
+
"description" => info[:description] || '',
|
45
|
+
"login" => @login,
|
46
|
+
"token" => @token
|
47
|
+
})
|
48
|
+
|
49
|
+
unless stat.code == 200
|
50
|
+
raise "Failed to post file info"
|
51
|
+
end
|
52
|
+
|
53
|
+
upload_info = FasterXmlSimple.xml_in(stat.content)['hash']
|
54
|
+
if info[:file]
|
55
|
+
f = File.open(info[:file], 'rb')
|
56
|
+
stat = HTTPClient.post("http://github.s3.amazonaws.com/", [
|
57
|
+
['Filename', info[:name]],
|
58
|
+
['policy', upload_info['policy']],
|
59
|
+
['success_action_status', 201],
|
60
|
+
['key', upload_info['prefix'] + info[:name]],
|
61
|
+
['AWSAccessKeyId', upload_info['accesskeyid']],
|
62
|
+
['Content-Type', info[:content_type] || 'application/octet-stream'],
|
63
|
+
['signature', upload_info['signature']],
|
64
|
+
['acl', upload_info['acl']],
|
65
|
+
['file', f]
|
66
|
+
])
|
67
|
+
f.close
|
68
|
+
else
|
69
|
+
stat = HTTPClient.post("http://github.s3.amazonaws.com/", [
|
70
|
+
['Filename', info[:name]],
|
71
|
+
['policy', upload_info['policy']],
|
72
|
+
['success_action_status', 201],
|
73
|
+
['key', upload_info['prefix'] + info[:name]],
|
74
|
+
['AWSAccessKeyId', upload_info['accesskeyid']],
|
75
|
+
['Content-Type', info[:content_type] || 'application/octet-stream'],
|
76
|
+
['signature', upload_info['signature']],
|
77
|
+
['acl', upload_info['acl']],
|
78
|
+
['file', StringIO.new(info[:data])]
|
79
|
+
])
|
80
|
+
end
|
81
|
+
|
82
|
+
if stat.code == 201
|
83
|
+
return FasterXmlSimple.xml_in(stat.content)['PostResponse']['Location']
|
84
|
+
else
|
85
|
+
pp stat.content
|
86
|
+
raise 'Failed to upload'
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
private
|
91
|
+
def list_files repos
|
92
|
+
raise "required repository name" unless repos
|
93
|
+
res = HTTPClient.get_content("http://github.com/#{repos}/downloads", {
|
94
|
+
"login" => @login,
|
95
|
+
"token" => @token
|
96
|
+
})
|
97
|
+
Nokogiri::HTML(res).xpath('id("browser")/descendant::tr[contains(@id, "download")]').map do |fileinfo|
|
98
|
+
obj = {
|
99
|
+
:id => fileinfo.attribute('id').text,
|
100
|
+
:description => fileinfo.at_xpath('descendant::td[3]').text,
|
101
|
+
:date => fileinfo.at_xpath('descendant::td[4]').text,
|
102
|
+
:size => fileinfo.at_xpath('descendant::td[5]').text
|
103
|
+
}
|
104
|
+
anchor = fileinfo.at_xpath('descendant::td[2]/a')
|
105
|
+
obj[:link] = anchor.attribute('href').text
|
106
|
+
obj[:name] = anchor.text
|
107
|
+
obj
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
data/test/github_test.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require File.dirname(__FILE__) + '/../lib/net/github-upload'
|
3
|
+
|
4
|
+
class GitHubUploadTest < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
login = `git config github.user`.chomp
|
7
|
+
token = `git config github.token`.chomp
|
8
|
+
@gh = Net::GitHub::Upload.new(
|
9
|
+
:login => login,
|
10
|
+
:token => token
|
11
|
+
)
|
12
|
+
@repos = 'taberareloo'
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_file_upload
|
16
|
+
direct_link = nil
|
17
|
+
assert_nothing_raised {
|
18
|
+
direct_link = @gh.upload(
|
19
|
+
:repos => @repos,
|
20
|
+
:file => 'test/test',
|
21
|
+
:description => "test file"
|
22
|
+
)
|
23
|
+
}
|
24
|
+
assert_instance_of String, direct_link
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_content_upload
|
28
|
+
direct_link = nil
|
29
|
+
assert_nothing_raised {
|
30
|
+
direct_link = @gh.upload(
|
31
|
+
:repos => @repos,
|
32
|
+
:data => 'test',
|
33
|
+
:name => "test_#{Time.now.to_i}.txt",
|
34
|
+
:content_type => 'text/plain',
|
35
|
+
:description => "test file2"
|
36
|
+
)
|
37
|
+
}
|
38
|
+
assert_instance_of String, direct_link
|
39
|
+
end
|
40
|
+
end
|
data/test/test
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
This is TEST FILE
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: net-github-upload
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Constellation
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-12-01 00:00:00 +09:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: nokogiri
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: faster_xml_simple
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: httpclient
|
37
|
+
type: :runtime
|
38
|
+
version_requirement:
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: "0"
|
44
|
+
version:
|
45
|
+
description: |
|
46
|
+
Ruby Net::GitHub::Upload is upload user agent for GitHub Downloads
|
47
|
+
|
48
|
+
email: utatane.tea@gmail.com
|
49
|
+
executables: []
|
50
|
+
|
51
|
+
extensions: []
|
52
|
+
|
53
|
+
extra_rdoc_files:
|
54
|
+
- README.rdoc
|
55
|
+
files:
|
56
|
+
- README.rdoc
|
57
|
+
- Rakefile
|
58
|
+
- test/github_test.rb
|
59
|
+
- test/test
|
60
|
+
- lib/net/github-upload.rb
|
61
|
+
has_rdoc: true
|
62
|
+
homepage: http://github.com/Constellation/ruby-net-github-upload
|
63
|
+
licenses: []
|
64
|
+
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options:
|
67
|
+
- --main
|
68
|
+
- README.rdoc
|
69
|
+
- --charset
|
70
|
+
- utf-8
|
71
|
+
- --line-numbers
|
72
|
+
- --inline-source
|
73
|
+
require_paths:
|
74
|
+
- lib
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: "0"
|
80
|
+
version:
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: "0"
|
86
|
+
version:
|
87
|
+
requirements: []
|
88
|
+
|
89
|
+
rubyforge_project: ruby-net-github-upload
|
90
|
+
rubygems_version: 1.3.5
|
91
|
+
signing_key:
|
92
|
+
specification_version: 3
|
93
|
+
summary: ruby porting of Net::GitHub::Upload
|
94
|
+
test_files:
|
95
|
+
- test/github_test.rb
|