jist 1.4.0 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.md +13 -0
  2. data/lib/jist.rb +44 -12
  3. data/spec/ghe_spec.rb +80 -0
  4. metadata +26 -25
data/README.md CHANGED
@@ -145,6 +145,19 @@ Jist.login!
145
145
  This will take them through the process of obtaining an OAuth2 token, and storing it
146
146
  in `~/.jist`, where it can later be read by `Jist.gist`
147
147
 
148
+ GitHub enterprise
149
+ ==================
150
+
151
+ If you'd like `jist` to use your locally installed [Github Enterprise](https://enterprise.github.com/),
152
+ you need to export the `GITHUB_URL` environment variable in your `~/.bashrc`.
153
+
154
+ ```bash
155
+ export GITHUB_URL=http://github.internal.example.com/
156
+ ```
157
+
158
+ Once you've done this and restarted your terminal (or run `source ~/.bashrc`), jist will
159
+ automatically use github enterprise instead of the public github.com
160
+
148
161
  Configuration
149
162
  =============
150
163
 
@@ -7,7 +7,7 @@ require 'uri'
7
7
  module Jist
8
8
  extend self
9
9
 
10
- VERSION = '1.4.0'
10
+ VERSION = '1.5.0'
11
11
 
12
12
  # A list of clipboard commands with copy and paste support.
13
13
  CLIPBOARD_COMMANDS = {
@@ -17,8 +17,13 @@ module Jist
17
17
  'putclip' => 'getclip'
18
18
  }
19
19
 
20
- GITHUB_API_URL = URI("https://api.github.com/")
21
- GIT_IO_URL = URI("http://git.io")
20
+ GITHUB_API_URL = URI("https://api.github.com/")
21
+ GIT_IO_URL = URI("http://git.io")
22
+
23
+ GITHUB_BASE_PATH = ""
24
+ GHE_BASE_PATH = "/api/v3"
25
+
26
+ URL_ENV_NAME = "GITHUB_URL"
22
27
 
23
28
  # Exception tag for errors raised while gisting.
24
29
  module Error;
@@ -78,10 +83,10 @@ module Jist
78
83
  if options[:anonymous]
79
84
  access_token = nil
80
85
  else
81
- access_token = (options[:access_token] || File.read(File.expand_path("~/.jist")) rescue nil)
86
+ access_token = (options[:access_token] || File.read(auth_token_file) rescue nil)
82
87
  end
83
88
 
84
- url = "/gists"
89
+ url = "#{base_path}/gists"
85
90
  url << "/" << CGI.escape(existing_gist) if existing_gist.to_s != ''
86
91
  url << "?access_token=" << CGI.escape(access_token) if access_token.to_s != ''
87
92
 
@@ -92,7 +97,7 @@ module Jist
92
97
  retried = false
93
98
 
94
99
  begin
95
- response = http(GITHUB_API_URL, request)
100
+ response = http(api_url, request)
96
101
  if Net::HTTPSuccess === response
97
102
  on_success(response.body, options)
98
103
  else
@@ -144,7 +149,7 @@ module Jist
144
149
  end
145
150
  puts ""
146
151
 
147
- request = Net::HTTP::Post.new("/authorizations")
152
+ request = Net::HTTP::Post.new("#{base_path}/authorizations")
148
153
  request.body = JSON.dump({
149
154
  :scopes => [:gist],
150
155
  :note => "The jist gem",
@@ -153,13 +158,13 @@ module Jist
153
158
  request.content_type = 'application/json'
154
159
  request.basic_auth(username, password)
155
160
 
156
- response = http(GITHUB_API_URL, request)
161
+ response = http(api_url, request)
157
162
 
158
163
  if Net::HTTPCreated === response
159
- File.open(File.expand_path("~/.jist"), 'w') do |f|
164
+ File.open(auth_token_file, 'w') do |f|
160
165
  f.write JSON.parse(response.body)['token']
161
166
  end
162
- puts "Success! https://github.com/settings/applications"
167
+ puts "Success! #{ENV[URL_ENV_NAME] || "https://github.com/"}settings/applications"
163
168
  else
164
169
  raise "Got #{response.class} from gist: #{response.body}"
165
170
  end
@@ -198,7 +203,7 @@ module Jist
198
203
  http.request request
199
204
  end
200
205
  rescue Timeout::Error
201
- raise "Could not connect to https://api.github.com/"
206
+ raise "Could not connect to #{api_url}"
202
207
  end
203
208
 
204
209
  # Called after an HTTP response to gist to perform post-processing.
@@ -233,7 +238,16 @@ module Jist
233
238
  #
234
239
  def copy(content)
235
240
  IO.popen(clipboard_command(:copy), 'r+') { |clip| clip.print content }
236
- raise Error, 'Copying to clipboard failed.' unless paste == content
241
+
242
+ unless paste == content
243
+ message = 'Copying to clipboard failed.'
244
+
245
+ if ENV["TMUX"] && clipboard_command(:copy) == 'pbcopy'
246
+ message << "\nIf you're running tmux on a mac, try http://robots.thoughtbot.com/post/19398560514/how-to-copy-and-paste-with-tmux-on-mac-os-x"
247
+ end
248
+
249
+ raise Error, message
250
+ end
237
251
  rescue Error => e
238
252
  raise ClipboardError, e.message + "\nAttempted to copy: #{content}"
239
253
  end
@@ -307,4 +321,22 @@ Could not find copy command, tried:
307
321
 
308
322
  `#{command} #{url}`
309
323
  end
324
+
325
+ # Get the API base path
326
+ def base_path
327
+ ENV.key?(URL_ENV_NAME) ? GHE_BASE_PATH : GITHUB_BASE_PATH
328
+ end
329
+
330
+ # Get the API URL
331
+ def api_url
332
+ ENV.key?(URL_ENV_NAME) ? URI(ENV[URL_ENV_NAME]) : GITHUB_API_URL
333
+ end
334
+
335
+ def auth_token_file
336
+ if ENV.key?(URL_ENV_NAME)
337
+ File.expand_path "~/.jist.#{ENV[URL_ENV_NAME].gsub(/[^a-z.]/, '')}"
338
+ else
339
+ File.expand_path "~/.jist"
340
+ end
341
+ end
310
342
  end
@@ -0,0 +1,80 @@
1
+ describe '...' do
2
+
3
+ MOCK_GHE_HOST = 'ghe.example.com'
4
+ MOCK_GHE_PROTOCOL = 'http'
5
+ MOCK_USER = 'foo'
6
+ MOCK_PASSWORD = 'bar'
7
+
8
+ MOCK_AUTHZ_GHE_URL = "#{MOCK_GHE_PROTOCOL}://#{MOCK_USER}:#{MOCK_PASSWORD}@#{MOCK_GHE_HOST}/api/v3/"
9
+ MOCK_GHE_URL = "#{MOCK_GHE_PROTOCOL}://#{MOCK_GHE_HOST}/api/v3/"
10
+ MOCK_AUTHZ_GITHUB_URL = "https://#{MOCK_USER}:#{MOCK_PASSWORD}@api.github.com/"
11
+ MOCK_GITHUB_URL = "https://api.github.com/"
12
+
13
+ before do
14
+ @saved_env = ENV[Jist::URL_ENV_NAME]
15
+
16
+ # stub requests for /gists
17
+ stub_request(:post, /#{MOCK_GHE_URL}gists/).to_return(:body => %[{"html_url": "http://#{MOCK_GHE_HOST}"}])
18
+ stub_request(:post, /#{MOCK_GITHUB_URL}gists/).to_return(:body => '{"html_url": "http://github.com/"}')
19
+
20
+ # stub requests for /authorizations
21
+ stub_request(:post, /#{MOCK_AUTHZ_GHE_URL}authorizations/).
22
+ to_return(:status => 201, :body => '{"token": "asdf"}')
23
+ stub_request(:post, /#{MOCK_AUTHZ_GITHUB_URL}authorizations/).
24
+ to_return(:status => 201, :body => '{"token": "asdf"}')
25
+ end
26
+
27
+ after do
28
+ ENV[Jist::URL_ENV_NAME] = @saved_env
29
+ end
30
+
31
+ describe :login! do
32
+ before do
33
+ @saved_stdin = $stdin
34
+
35
+ # stdin emulation
36
+ $stdin = StringIO.new "#{MOCK_USER}\n#{MOCK_PASSWORD}\n"
37
+
38
+ # intercept for updating ~/.jist
39
+ File.stub(:open)
40
+ end
41
+
42
+ after do
43
+ $stdin = @saved_stdin
44
+ end
45
+
46
+ it "should access to api.github.com when $#{Jist::URL_ENV_NAME} wasn't set" do
47
+ ENV.delete Jist::URL_ENV_NAME
48
+
49
+ Jist.login!
50
+
51
+ assert_requested(:post, /#{MOCK_AUTHZ_GITHUB_URL}authorizations/)
52
+ end
53
+
54
+ it "should access to #{MOCK_GHE_HOST} when $#{Jist::URL_ENV_NAME} was set" do
55
+ ENV[Jist::URL_ENV_NAME] = MOCK_GHE_URL
56
+
57
+ Jist.login!
58
+
59
+ assert_requested(:post, /#{MOCK_AUTHZ_GHE_URL}authorizations/)
60
+ end
61
+ end
62
+
63
+ describe :gist do
64
+ it "should access to api.github.com when $#{Jist::URL_ENV_NAME} wasn't set" do
65
+ ENV.delete Jist::URL_ENV_NAME
66
+
67
+ Jist.gist "test gist"
68
+
69
+ assert_requested(:post, /#{MOCK_GITHUB_URL}gists/)
70
+ end
71
+
72
+ it "should access to #{MOCK_GHE_HOST} when $#{Jist::URL_ENV_NAME} was set" do
73
+ ENV[Jist::URL_ENV_NAME] = MOCK_GHE_URL
74
+
75
+ Jist.gist "test gist"
76
+
77
+ assert_requested(:post, /#{MOCK_GHE_URL}gists/)
78
+ end
79
+ end
80
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jist
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,72 +9,72 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-22 00:00:00.000000000 Z
12
+ date: 2013-04-12 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
- none: false
18
17
  requirements:
19
18
  - - ! '>='
20
19
  - !ruby/object:Gem::Version
21
20
  version: '0'
22
- type: :runtime
23
- prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
21
  none: false
22
+ version_requirements: !ruby/object:Gem::Requirement
26
23
  requirements:
27
24
  - - ! '>='
28
25
  - !ruby/object:Gem::Version
29
26
  version: '0'
27
+ none: false
28
+ prerelease: false
29
+ name: json
30
30
  - !ruby/object:Gem::Dependency
31
- name: rake
31
+ type: :development
32
32
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
33
  requirements:
35
34
  - - ! '>='
36
35
  - !ruby/object:Gem::Version
37
36
  version: '0'
38
- type: :development
39
- prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
41
37
  none: false
38
+ version_requirements: !ruby/object:Gem::Requirement
42
39
  requirements:
43
40
  - - ! '>='
44
41
  - !ruby/object:Gem::Version
45
42
  version: '0'
43
+ none: false
44
+ prerelease: false
45
+ name: rake
46
46
  - !ruby/object:Gem::Dependency
47
- name: rspec
47
+ type: :development
48
48
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
49
  requirements:
51
50
  - - ! '>='
52
51
  - !ruby/object:Gem::Version
53
52
  version: '0'
54
- type: :development
55
- prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
57
53
  none: false
54
+ version_requirements: !ruby/object:Gem::Requirement
58
55
  requirements:
59
56
  - - ! '>='
60
57
  - !ruby/object:Gem::Version
61
58
  version: '0'
59
+ none: false
60
+ prerelease: false
61
+ name: rspec
62
62
  - !ruby/object:Gem::Dependency
63
- name: webmock
63
+ type: :development
64
64
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
65
  requirements:
67
66
  - - ! '>='
68
67
  - !ruby/object:Gem::Version
69
68
  version: '0'
70
- type: :development
71
- prerelease: false
72
- version_requirements: !ruby/object:Gem::Requirement
73
69
  none: false
70
+ version_requirements: !ruby/object:Gem::Requirement
74
71
  requirements:
75
72
  - - ! '>='
76
73
  - !ruby/object:Gem::Version
77
74
  version: '0'
75
+ none: false
76
+ prerelease: false
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:
@@ -92,6 +92,7 @@ files:
92
92
  - jist.gemspec
93
93
  - lib/jist.rb
94
94
  - spec/clipboard_spec.rb
95
+ - spec/ghe_spec.rb
95
96
  - spec/proxy_spec.rb
96
97
  - spec/shorten_spec.rb
97
98
  - spec/spec_helper.rb
@@ -103,20 +104,20 @@ rdoc_options: []
103
104
  require_paths:
104
105
  - lib
105
106
  required_ruby_version: !ruby/object:Gem::Requirement
106
- none: false
107
107
  requirements:
108
108
  - - ! '>='
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
- required_rubygems_version: !ruby/object:Gem::Requirement
112
111
  none: false
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
113
  requirements:
114
114
  - - ! '>='
115
115
  - !ruby/object:Gem::Version
116
116
  version: '0'
117
+ none: false
117
118
  requirements: []
118
119
  rubyforge_project:
119
- rubygems_version: 1.8.23
120
+ rubygems_version: 1.8.25
120
121
  signing_key:
121
122
  specification_version: 3
122
123
  summary: Just allows you to upload gists