jist 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Rakefile CHANGED
@@ -4,3 +4,7 @@ desc 'run the tests' # that's non-DRY
4
4
  task :test do
5
5
  sh 'rspec spec'
6
6
  end
7
+
8
+ task :clipfailtest do
9
+ sh 'PATH=/ /usr/bin/ruby -Ilib -S bin/jist -ac < lib/jist.rb'
10
+ end
@@ -1,12 +1,13 @@
1
1
  require 'net/https'
2
2
  require 'cgi'
3
3
  require 'json'
4
+ require 'uri'
4
5
 
5
6
  # It just gists.
6
7
  module Jist
7
8
  extend self
8
9
 
9
- VERSION = '1.2.0'
10
+ VERSION = '1.3.0'
10
11
 
11
12
  # A list of clipboard commands with copy and paste support.
12
13
  CLIPBOARD_COMMANDS = {
@@ -16,8 +17,12 @@ module Jist
16
17
  'putclip' => 'getclip'
17
18
  }
18
19
 
20
+ GITHUB_API_URL = URI("https://api.github.com/")
21
+ GIT_IO_URL = URI("http://git.io")
22
+
19
23
  # Exception tag for errors raised while gisting.
20
24
  module Error; end
25
+ class ClipboardError < RuntimeError; include Error end
21
26
 
22
27
  # Upload a gist to https://gist.github.com
23
28
  #
@@ -91,7 +96,7 @@ module Jist
91
96
  retried = false
92
97
 
93
98
  begin
94
- response = http(request)
99
+ response = http(GITHUB_API_URL, request)
95
100
  if Net::HTTPSuccess === response
96
101
  on_success(response.body, options)
97
102
  else
@@ -107,12 +112,14 @@ module Jist
107
112
  raise e.extend Error
108
113
  end
109
114
 
110
- # Convert long github urls into shotr git.io ones
115
+ # Convert long github urls into short git.io ones
111
116
  #
112
117
  # @param [String] url
113
118
  # @return [String] shortened url, or long url if shortening fails
114
119
  def shorten(url)
115
- response = Net::HTTP.post_form(URI("http://git.io/"), :url => url)
120
+ request = Net::HTTP::Post.new("/")
121
+ request.set_form_data(:url => url)
122
+ response = http(GIT_IO_URL, request)
116
123
  case response.code
117
124
  when "201"
118
125
  response['Location']
@@ -150,7 +157,7 @@ module Jist
150
157
  request.content_type = 'application/json'
151
158
  request.basic_auth(username, password)
152
159
 
153
- response = http(request)
160
+ response = http(GITHUB_API_URL, request)
154
161
 
155
162
  if Net::HTTPCreated === response
156
163
  File.open(File.expand_path("~/.jist"), 'w') do |f|
@@ -166,29 +173,32 @@ module Jist
166
173
 
167
174
  # Return HTTP connection
168
175
  #
176
+ # @param [URI::HTTP] The URI to which to connect
169
177
  # @return [Net::HTTP]
170
- def http_connection
178
+ def http_connection(uri)
171
179
  env = ENV['http_proxy'] || ENV['HTTP_PROXY']
172
180
  connection = if env
173
- uri = URI(env)
174
- proxy_host, proxy_port = uri.host, uri.port
175
- Net::HTTP::Proxy(proxy_host, proxy_port).new("api.github.com", 443)
181
+ proxy = URI(env)
182
+ Net::HTTP::Proxy(proxy.host, proxy.port).new(uri.host, uri.port)
176
183
  else
177
- Net::HTTP.new("api.github.com", 443)
184
+ Net::HTTP.new(uri.host, uri.port)
178
185
  end
179
- connection.use_ssl = true
180
- connection.verify_mode = OpenSSL::SSL::VERIFY_NONE
186
+ if uri.scheme == "https"
187
+ connection.use_ssl = true
188
+ connection.verify_mode = OpenSSL::SSL::VERIFY_NONE
189
+ end
181
190
  connection.open_timeout = 10
182
191
  connection.read_timeout = 10
183
192
  connection
184
193
  end
185
194
 
186
- # Run an HTTP operation against api.github.com
195
+ # Run an HTTP operation
187
196
  #
188
- # @param [Net::HTTPRequest] request
197
+ # @param [URI::HTTP] The URI to which to connect
198
+ # @param [Net::HTTPRequest] The request to make
189
199
  # @return [Net::HTTPResponse]
190
- def http(request)
191
- http_connection().start do |http|
200
+ def http(url, request)
201
+ http_connection(url).start do |http|
192
202
  http.request request
193
203
  end
194
204
  rescue Timeout::Error
@@ -214,19 +224,19 @@ module Jist
214
224
  # Copy a string to the clipboard.
215
225
  #
216
226
  # @param [String] content
217
- # @raise [RuntimeError] if no clipboard integration could be found
227
+ # @raise [Jist::Error] if no clipboard integration could be found
218
228
  #
219
- # This method was heavily inspired by defunkt's Gist#copy,
220
- # @see https://github.com/defunkt/gist/blob/bca9b29/lib/gist.rb#L178
221
229
  def copy(content)
222
230
  IO.popen(clipboard_command(:copy), 'r+') { |clip| clip.print content }
223
- raise "Copying to clipboard failed" unless paste == content
231
+ raise Error, 'Copying to clipboard failed.' unless paste == content
232
+ rescue Error => e
233
+ raise ClipboardError, e.message + "\nAttempted to copy: #{content}"
224
234
  end
225
235
 
226
236
  # Get a string from the clipboard.
227
237
  #
228
238
  # @param [String] content
229
- # @raise [RuntimeError] if no clipboard integration could be found
239
+ # @raise [Jist::Error] if no clipboard integration could be found
230
240
  def paste
231
241
  `#{clipboard_command(:paste)}`
232
242
  end
@@ -252,12 +262,15 @@ module Jist
252
262
  #
253
263
  # @param [Symbol] action either :copy or :paste
254
264
  # @return [String] the command to run
255
- # @raise [RuntimeError] if no clipboard integration could be found
265
+ # @raise [Jist::ClipboardError] if no clipboard integration could be found
256
266
  def clipboard_command(action)
257
267
  command = CLIPBOARD_COMMANDS.keys.detect do |cmd|
258
268
  which cmd
259
269
  end
260
- raise "Could not find copy command, tried: #{CLIPBOARD_COMMANDS}" unless command
270
+ raise ClipboardError, <<-EOT unless command
271
+ Could not find copy command, tried:
272
+ #{CLIPBOARD_COMMANDS.values.join(' || ')}
273
+ EOT
261
274
  action == :copy ? command : CLIPBOARD_COMMANDS[command]
262
275
  end
263
276
 
@@ -29,6 +29,6 @@ describe '...' do
29
29
  ENV['PATH'] = ''
30
30
  lambda{
31
31
  ask_for_copy
32
- }.should raise_error(/Could not find copy command/)
32
+ }.should raise_error(/Could not find copy command.*http/m)
33
33
  end
34
34
  end
@@ -7,13 +7,15 @@ describe '...' do
7
7
  ENV['HTTP_PROXY'] = @saved_env
8
8
  end
9
9
 
10
+ FOO_URL = URI('http://ddg.gg/')
11
+
10
12
  it "should be Net::HTTP when $HTTP_PROXY wasn't set" do
11
13
  ENV['HTTP_PROXY'] = ''
12
- Jist.http_connection().should be_an_instance_of(Net::HTTP)
14
+ Jist.http_connection(FOO_URL).should be_an_instance_of(Net::HTTP)
13
15
  end
14
16
 
15
17
  it "should be Net::HTTP::Proxy when $HTTP_PROXY was set" do
16
18
  ENV['HTTP_PROXY'] = 'http://proxy.example.com:8080'
17
- Jist.http_connection().should_not be_an_instance_of(Net::HTTP)
19
+ Jist.http_connection(FOO_URL).should_not be_an_instance_of(Net::HTTP)
18
20
  end
19
21
  end
metadata CHANGED
@@ -1,25 +1,24 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jist
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
5
4
  prerelease:
5
+ version: 1.3.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Conrad Irwin
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-11 00:00:00.000000000 Z
12
+ date: 2012-12-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: json
15
+ type: :runtime
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
21
  version: '0'
22
- type: :runtime
23
22
  prerelease: false
24
23
  version_requirements: !ruby/object:Gem::Requirement
25
24
  none: false
@@ -27,15 +26,15 @@ dependencies:
27
26
  - - ! '>='
28
27
  - !ruby/object:Gem::Version
29
28
  version: '0'
29
+ name: json
30
30
  - !ruby/object:Gem::Dependency
31
- name: rake
31
+ type: :development
32
32
  requirement: !ruby/object:Gem::Requirement
33
33
  none: false
34
34
  requirements:
35
35
  - - ! '>='
36
36
  - !ruby/object:Gem::Version
37
37
  version: '0'
38
- type: :development
39
38
  prerelease: false
40
39
  version_requirements: !ruby/object:Gem::Requirement
41
40
  none: false
@@ -43,15 +42,15 @@ dependencies:
43
42
  - - ! '>='
44
43
  - !ruby/object:Gem::Version
45
44
  version: '0'
45
+ name: rake
46
46
  - !ruby/object:Gem::Dependency
47
- name: rspec
47
+ type: :development
48
48
  requirement: !ruby/object:Gem::Requirement
49
49
  none: false
50
50
  requirements:
51
51
  - - ! '>='
52
52
  - !ruby/object:Gem::Version
53
53
  version: '0'
54
- type: :development
55
54
  prerelease: false
56
55
  version_requirements: !ruby/object:Gem::Requirement
57
56
  none: false
@@ -59,15 +58,15 @@ dependencies:
59
58
  - - ! '>='
60
59
  - !ruby/object:Gem::Version
61
60
  version: '0'
61
+ name: rspec
62
62
  - !ruby/object:Gem::Dependency
63
- name: webmock
63
+ type: :development
64
64
  requirement: !ruby/object:Gem::Requirement
65
65
  none: false
66
66
  requirements:
67
67
  - - ! '>='
68
68
  - !ruby/object:Gem::Version
69
69
  version: '0'
70
- type: :development
71
70
  prerelease: false
72
71
  version_requirements: !ruby/object:Gem::Requirement
73
72
  none: false
@@ -75,6 +74,7 @@ dependencies:
75
74
  - - ! '>='
76
75
  - !ruby/object:Gem::Version
77
76
  version: '0'
77
+ name: webmock
78
78
  description: Provides a single function (Jist.gist) that uploads a gist.
79
79
  email: conrad.irwin@gmail.com
80
80
  executables:
@@ -121,4 +121,3 @@ signing_key:
121
121
  specification_version: 3
122
122
  summary: Just allows you to upload gists
123
123
  test_files: []
124
- has_rdoc: