github-downloads 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- github-downloads (0.1.1)
4
+ github-downloads (0.1.3)
5
5
  highline
6
6
  hirb
7
7
  json
@@ -14,10 +14,10 @@ GEM
14
14
  specs:
15
15
  awesome_print (0.3.1)
16
16
  diff-lcs (1.1.2)
17
- highline (1.6.2)
18
- hirb (0.4.5)
17
+ highline (1.6.11)
18
+ hirb (0.6.2)
19
19
  json (1.5.3)
20
- mime-types (1.16)
20
+ mime-types (1.18)
21
21
  mimic (0.4.1)
22
22
  json
23
23
  plist
@@ -28,7 +28,7 @@ GEM
28
28
  plist (3.1.0)
29
29
  rack (1.2.1)
30
30
  rake (0.9.2)
31
- rest-client (1.6.3)
31
+ rest-client (1.6.7)
32
32
  mime-types (>= 1.16)
33
33
  rspec (2.5.0)
34
34
  rspec-core (~> 2.5.0)
data/Rakefile CHANGED
@@ -11,7 +11,7 @@ task :default => :specs
11
11
  require "rubygems"
12
12
  require "rubygems/package_task"
13
13
 
14
- GEM_VERSION = "0.1.2"
14
+ GEM_VERSION = "0.1.3"
15
15
 
16
16
  # This builds the actual gem. For details of what all these options
17
17
  # mean, and other ones you can add, check the documentation here:
@@ -1,65 +1,96 @@
1
1
  require 'ostruct'
2
2
  require 'mime/types'
3
3
  require 'github/client'
4
+ require 'github/s3_uploader'
4
5
 
5
6
  module Github
6
7
  class Downloads
7
8
  attr_accessor :uploader
8
-
9
+ attr_reader :last_response
10
+
9
11
  GITHUB_BASE_URL = "https://api.github.com"
10
-
12
+
11
13
  def initialize(client, user, repos)
12
14
  @client = client
13
15
  @user = user
14
16
  @repos = repos
17
+ @last_response = nil
15
18
  end
16
-
19
+
17
20
  def self.connect(user, password, repos)
18
21
  client = Client.connect(GITHUB_BASE_URL, user, password)
19
- new(client, user, repos)
22
+ new(client, user, repos).tap do |downloader|
23
+ downloader.uploader = Github::S3Uploader.new # default uploader
24
+ end
20
25
  end
21
-
26
+
22
27
  class UnexpectedResponse < StandardError
23
28
  attr_reader :response
24
-
29
+
25
30
  def initialize(response)
26
31
  @response = response
27
32
  end
28
33
  end
29
-
34
+
30
35
  def list
31
- response = @client.get(downloads_resource_path)
32
-
33
- if response.success?
34
- from_response_data(response.data)
36
+ @last_response = @client.get(downloads_resource_path)
37
+
38
+ if @last_response.success?
39
+ from_response_data(@last_response.data)
35
40
  else
36
- raise UnexpectedResponse, response
41
+ raise UnexpectedResponse, @last_response
37
42
  end
38
43
  end
39
-
40
- def create(file_path, description = "")
41
- response = @client.post(downloads_resource_path, {
44
+
45
+ def create(file_path, description = "", options={})
46
+ delete(:name => File.basename(file_path)) if options[:overwrite]
47
+
48
+ @last_response = @client.post(downloads_resource_path, {
42
49
  :name => File.basename(file_path),
43
50
  :description => description,
44
51
  :content_type => MIME::Types.type_for(file_path)[0] || MIME::Types["application/octet-stream"][0],
45
52
  :size => File.size?(file_path)
46
53
  })
47
-
48
- if response.success?
49
- uploader.upload(File.expand_path(file_path), response.data)
54
+
55
+ if @last_response.success?
56
+ uploader.upload(File.expand_path(file_path), @last_response.data)
50
57
  end
51
58
  end
52
-
59
+
60
+ def delete(options={})
61
+ if download_id = options[:id]
62
+ delete_download_with_id(download_id)
63
+ elsif file_name = options[:name]
64
+ download = list.find { |download| download.name == file_name }
65
+ delete_download_with_id(download.download_id) if download
66
+ else
67
+ raise "Either :id or :name should be specified"
68
+ end
69
+ end
70
+
53
71
  private
54
-
72
+
73
+ def delete_download_with_id(id)
74
+ @last_response = @client.delete("#{downloads_resource_path}/#{id}")
75
+ @last_response.success?
76
+ end
77
+
55
78
  def downloads_resource_path
56
79
  "/repos/#{@user}/#{@repos}/downloads"
57
80
  end
58
-
81
+
59
82
  def from_response_data(data)
60
- data.map { |hash| Download.new(hash) }
83
+ data.map do |hash|
84
+ # OpenStruct doesn't like :id attributes
85
+ Download.new(with_swapped_key(hash, "id", "download_id"))
86
+ end
61
87
  end
62
88
 
89
+ def with_swapped_key(hash, old_key, new_key)
90
+ hash[new_key] = hash[old_key]
91
+ hash
92
+ end
93
+
63
94
  class Download < OpenStruct
64
95
  end
65
96
  end
@@ -73,9 +73,7 @@ module Github
73
73
  password = lookup_or_prompt_for_password(params[:user])
74
74
  end
75
75
 
76
- @github ||= Github::Downloads.connect(params[:user], password, params[:repos]).tap do |gh|
77
- gh.uploader = Github::S3Uploader.new
78
- end
76
+ @github ||= Github::Downloads.connect(params[:user], password, params[:repos])
79
77
  end
80
78
 
81
79
  def lookup_or_prompt_for_password(user)
@@ -4,7 +4,7 @@ module Github
4
4
  class S3Uploader
5
5
  def upload(path, metadata)
6
6
  response = RestClient.post(metadata["s3_url"], [
7
- ["key", "#{metadata["prefix"]}#{metadata["name"]}"],
7
+ ["key", "#{metadata["path"]}"],
8
8
  ["acl", metadata["acl"]],
9
9
  ["success_action_status", 201],
10
10
  ["Filename", metadata["name"]],
@@ -19,7 +19,7 @@ describe "Github::Downloads" do
19
19
  {
20
20
  "url" => "https://api.github.com/repos/octocat/Hello-World/downloads/1",
21
21
  "html_url" => "https://github.com/repos/octocat/Hello-World/downloads/filename",
22
- "id" => 1,
22
+ "id" => 123,
23
23
  "name" => "file.zip",
24
24
  "description" => "The latest release",
25
25
  "size" => 1024,
@@ -28,12 +28,20 @@ describe "Github::Downloads" do
28
28
  ])
29
29
  @downloads.list.size.should == 1
30
30
  @downloads.list.first.description.should == "The latest release"
31
+ @downloads.list.first.download_id.should == 123
31
32
  end
32
33
 
33
34
  it "raises if the response is not successful" do
34
35
  @client.stubs(:get).with("/repos/username/somerepo/downloads").returns unsuccessful_response
35
36
  proc { @downloads.list }.should raise_error(Github::Downloads::UnexpectedResponse)
36
37
  end
38
+
39
+ it "stores the last response" do
40
+ expected_response = successful_response_with([])
41
+ @client.stubs(:get).with("/repos/username/somerepo/downloads").returns expected_response
42
+ @downloads.list
43
+ @downloads.last_response.should == expected_response
44
+ end
37
45
  end
38
46
 
39
47
  describe "#upload" do
@@ -72,6 +80,57 @@ describe "Github::Downloads" do
72
80
  @downloads.create("fixtures/textfile.txt", "an example file").should be_false
73
81
  end
74
82
 
83
+ it "stores the last response" do
84
+ expected_response = successful_response_with("upload data")
85
+ @client.stubs(:post).returns expected_response
86
+ @uploader.stubs(:upload).returns true
87
+ @downloads.create("fixtures/textfile.txt", "an example file")
88
+ @downloads.last_response.should == expected_response
89
+ end
90
+
91
+ it "attempts to delete the file first if :overwrite is specified" do
92
+ @downloads.expects(:delete).with(:name => "textfile.txt")
93
+ @client.stubs(:post).returns successful_response_with("upload data")
94
+ @uploader.stubs(:upload).returns true
95
+ @downloads.create("fixtures/textfile.txt", "an example file", :overwrite => true)
96
+ end
97
+
98
+ end
99
+
100
+ describe "#delete" do
101
+
102
+ it "sends a DELETE request to the download's resource when provided with an ID" do
103
+ @client.expects(:delete).with("/repos/username/somerepo/downloads/123").returns(successful_response_with([]))
104
+ @downloads.delete(:id => 123)
105
+ end
106
+
107
+ it "looks up the ID of the download before deletion when provided with a name" do
108
+ @client.stubs(:get).with("/repos/username/somerepo/downloads").returns successful_response_with([
109
+ {"id" => 1, "name" => "file.zip"},
110
+ {"id" => 2, "name" => "other_file.zip"},
111
+ {"id" => 3, "name" => "some_file.txt"},
112
+ {"id" => 4, "name" => "file.mp3"}
113
+ ])
114
+ @client.expects(:delete).with("/repos/username/somerepo/downloads/3").returns(successful_response_with([]))
115
+ @downloads.delete(:name => "some_file.txt")
116
+ end
117
+
118
+ it "returns true for a successful deletion" do
119
+ @client.stubs(:delete).with("/repos/username/somerepo/downloads/123").returns(successful_response_with([]))
120
+ @downloads.delete(:id => 123).should be_true
121
+ end
122
+
123
+ it "stores the last response" do
124
+ expected_response = successful_response_with([])
125
+ @client.stubs(:delete).with("/repos/username/somerepo/downloads/123").returns expected_response
126
+ @downloads.delete(:id => 123)
127
+ @downloads.last_response.should == expected_response
128
+ end
129
+
130
+ it "raises if :name or :id is not provided" do
131
+ proc{ @downloads.delete }.should raise_error
132
+ end
133
+
75
134
  end
76
135
 
77
136
  private
metadata CHANGED
@@ -1,171 +1,133 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: github-downloads
3
- version: !ruby/object:Gem::Version
4
- hash: 31
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 1
9
- - 2
10
- version: 0.1.2
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Luke Redpath
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-07-02 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2012-03-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
21
15
  name: rest-client
22
- version_requirements: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &70102415004520 !ruby/object:Gem::Requirement
23
17
  none: false
24
- requirements:
25
- - - ">="
26
- - !ruby/object:Gem::Version
27
- hash: 3
28
- segments:
29
- - 0
30
- version: "0"
31
- prerelease: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
32
22
  type: :runtime
33
- requirement: *id001
34
- - !ruby/object:Gem::Dependency
23
+ prerelease: false
24
+ version_requirements: *70102415004520
25
+ - !ruby/object:Gem::Dependency
35
26
  name: json
36
- version_requirements: &id002 !ruby/object:Gem::Requirement
27
+ requirement: &70102415003520 !ruby/object:Gem::Requirement
37
28
  none: false
38
- requirements:
39
- - - ">="
40
- - !ruby/object:Gem::Version
41
- hash: 3
42
- segments:
43
- - 0
44
- version: "0"
45
- prerelease: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
46
33
  type: :runtime
47
- requirement: *id002
48
- - !ruby/object:Gem::Dependency
34
+ prerelease: false
35
+ version_requirements: *70102415003520
36
+ - !ruby/object:Gem::Dependency
49
37
  name: simpleconsole
50
- version_requirements: &id003 !ruby/object:Gem::Requirement
38
+ requirement: &70102415000660 !ruby/object:Gem::Requirement
51
39
  none: false
52
- requirements:
53
- - - ">="
54
- - !ruby/object:Gem::Version
55
- hash: 3
56
- segments:
57
- - 0
58
- version: "0"
59
- prerelease: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
60
44
  type: :runtime
61
- requirement: *id003
62
- - !ruby/object:Gem::Dependency
45
+ prerelease: false
46
+ version_requirements: *70102415000660
47
+ - !ruby/object:Gem::Dependency
63
48
  name: hirb
64
- version_requirements: &id004 !ruby/object:Gem::Requirement
49
+ requirement: &70102414999020 !ruby/object:Gem::Requirement
65
50
  none: false
66
- requirements:
67
- - - ">="
68
- - !ruby/object:Gem::Version
69
- hash: 3
70
- segments:
71
- - 0
72
- version: "0"
73
- prerelease: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
74
55
  type: :runtime
75
- requirement: *id004
76
- - !ruby/object:Gem::Dependency
56
+ prerelease: false
57
+ version_requirements: *70102414999020
58
+ - !ruby/object:Gem::Dependency
77
59
  name: mime-types
78
- version_requirements: &id005 !ruby/object:Gem::Requirement
60
+ requirement: &70102415054660 !ruby/object:Gem::Requirement
79
61
  none: false
80
- requirements:
81
- - - ">="
82
- - !ruby/object:Gem::Version
83
- hash: 3
84
- segments:
85
- - 0
86
- version: "0"
87
- prerelease: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
88
66
  type: :runtime
89
- requirement: *id005
90
- - !ruby/object:Gem::Dependency
67
+ prerelease: false
68
+ version_requirements: *70102415054660
69
+ - !ruby/object:Gem::Dependency
91
70
  name: highline
92
- version_requirements: &id006 !ruby/object:Gem::Requirement
71
+ requirement: &70102415053040 !ruby/object:Gem::Requirement
93
72
  none: false
94
- requirements:
95
- - - ">="
96
- - !ruby/object:Gem::Version
97
- hash: 3
98
- segments:
99
- - 0
100
- version: "0"
101
- prerelease: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
102
77
  type: :runtime
103
- requirement: *id006
104
- - !ruby/object:Gem::Dependency
78
+ prerelease: false
79
+ version_requirements: *70102415053040
80
+ - !ruby/object:Gem::Dependency
105
81
  name: rspec
106
- version_requirements: &id007 !ruby/object:Gem::Requirement
82
+ requirement: &70102415052180 !ruby/object:Gem::Requirement
107
83
  none: false
108
- requirements:
109
- - - ">="
110
- - !ruby/object:Gem::Version
111
- hash: 3
112
- segments:
113
- - 0
114
- version: "0"
115
- prerelease: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
116
88
  type: :development
117
- requirement: *id007
118
- - !ruby/object:Gem::Dependency
89
+ prerelease: false
90
+ version_requirements: *70102415052180
91
+ - !ruby/object:Gem::Dependency
119
92
  name: mocha
120
- version_requirements: &id008 !ruby/object:Gem::Requirement
93
+ requirement: &70102415051500 !ruby/object:Gem::Requirement
121
94
  none: false
122
- requirements:
123
- - - ">="
124
- - !ruby/object:Gem::Version
125
- hash: 3
126
- segments:
127
- - 0
128
- version: "0"
129
- prerelease: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
130
99
  type: :development
131
- requirement: *id008
132
- - !ruby/object:Gem::Dependency
100
+ prerelease: false
101
+ version_requirements: *70102415051500
102
+ - !ruby/object:Gem::Dependency
133
103
  name: mimic
134
- version_requirements: &id009 !ruby/object:Gem::Requirement
104
+ requirement: &70102415050560 !ruby/object:Gem::Requirement
135
105
  none: false
136
- requirements:
137
- - - ">="
138
- - !ruby/object:Gem::Version
139
- hash: 3
140
- segments:
141
- - 0
142
- version: "0"
143
- prerelease: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
144
110
  type: :development
145
- requirement: *id009
146
- - !ruby/object:Gem::Dependency
111
+ prerelease: false
112
+ version_requirements: *70102415050560
113
+ - !ruby/object:Gem::Dependency
147
114
  name: awesome_print
148
- version_requirements: &id010 !ruby/object:Gem::Requirement
115
+ requirement: &70102415049580 !ruby/object:Gem::Requirement
149
116
  none: false
150
- requirements:
151
- - - ">="
152
- - !ruby/object:Gem::Version
153
- hash: 3
154
- segments:
155
- - 0
156
- version: "0"
157
- prerelease: false
117
+ requirements:
118
+ - - ! '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
158
121
  type: :development
159
- requirement: *id010
122
+ prerelease: false
123
+ version_requirements: *70102415049580
160
124
  description:
161
125
  email: luke@lukeredpath.co.uk
162
- executables:
126
+ executables:
163
127
  - github-downloads
164
128
  extensions: []
165
-
166
129
  extra_rdoc_files: []
167
-
168
- files:
130
+ files:
169
131
  - Gemfile
170
132
  - Gemfile.lock
171
133
  - Rakefile
@@ -182,36 +144,29 @@ files:
182
144
  - lib/github.rb
183
145
  homepage: http://lukeredpath.co.uk
184
146
  licenses: []
185
-
186
147
  post_install_message:
187
148
  rdoc_options: []
188
-
189
- require_paths:
149
+ require_paths:
190
150
  - lib
191
- required_ruby_version: !ruby/object:Gem::Requirement
151
+ required_ruby_version: !ruby/object:Gem::Requirement
192
152
  none: false
193
- requirements:
194
- - - ">="
195
- - !ruby/object:Gem::Version
196
- hash: 3
197
- segments:
153
+ requirements:
154
+ - - ! '>='
155
+ - !ruby/object:Gem::Version
156
+ version: '0'
157
+ segments:
198
158
  - 0
199
- version: "0"
200
- required_rubygems_version: !ruby/object:Gem::Requirement
159
+ hash: -2923225652763402578
160
+ required_rubygems_version: !ruby/object:Gem::Requirement
201
161
  none: false
202
- requirements:
203
- - - ">="
204
- - !ruby/object:Gem::Version
205
- hash: 3
206
- segments:
207
- - 0
208
- version: "0"
162
+ requirements:
163
+ - - ! '>='
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
209
166
  requirements: []
210
-
211
167
  rubyforge_project:
212
- rubygems_version: 1.8.5
168
+ rubygems_version: 1.8.11
213
169
  signing_key:
214
170
  specification_version: 3
215
171
  summary: Manages downloads for your Github projects
216
172
  test_files: []
217
-