Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Make code more testable

  • Loading branch information...
commit 5127b8f5f2b1df22aa05a751c69b32f71a677ff8 1 parent 528e897
@kui kui authored
Showing with 22 additions and 10 deletions.
  1. +22 −10 lib/jist.rb
View
32 lib/jist.rb
@@ -17,11 +17,13 @@ module Jist
'putclip' => 'getclip'
}
- GITHUB_API_URL = URI("https://api.github.com/")
- GIT_IO_URL = URI("http://git.io")
- GHE_URL = ENV.key?("GHE_URL") ? URI(ENV["GHE_URL"]) : nil
- API_URL = GHE_URL || GITHUB_API_URL
- API_BASE_PATH = GHE_URL ? "/api/v3" : ""
+ GITHUB_API_URL = URI("https://api.github.com/")
+ GIT_IO_URL = URI("http://git.io")
+
+ GITHUB_BASE_PATH = ""
+ GHE_BASE_PATH = "/api/v3"
+
+ GHE_ENV_NAME = "GHE_URL"
# Exception tag for errors raised while gisting.
module Error;
@@ -84,7 +86,7 @@ def multi_gist(files, options={})
access_token = (options[:access_token] || File.read(File.expand_path("~/.jist")) rescue nil)
end
- url = "#{API_BASE_PATH}/gists"
+ url = "#{base_path}/gists"
url << "/" << CGI.escape(existing_gist) if existing_gist.to_s != ''
url << "?access_token=" << CGI.escape(access_token) if access_token.to_s != ''
@@ -95,7 +97,7 @@ def multi_gist(files, options={})
retried = false
begin
- response = http(API_URL, request)
+ response = http(api_url, request)
if Net::HTTPSuccess === response
on_success(response.body, options)
else
@@ -147,7 +149,7 @@ def login!
end
puts ""
- request = Net::HTTP::Post.new("#{API_BASE_PATH}/authorizations")
+ request = Net::HTTP::Post.new("#{base_path}/authorizations")
request.body = JSON.dump({
:scopes => [:gist],
:note => "The jist gem",
@@ -156,13 +158,13 @@ def login!
request.content_type = 'application/json'
request.basic_auth(username, password)
- response = http(API_URL, request)
+ response = http(api_url, request)
if Net::HTTPCreated === response
File.open(File.expand_path("~/.jist"), 'w') do |f|
f.write JSON.parse(response.body)['token']
end
- puts "Success! https://github.com/settings/applications"
+ puts "Success! #{ENV[GHE_ENV_NAME] || "https://github.com/"}settings/applications"
else
raise "Got #{response.class} from gist: #{response.body}"
end
@@ -310,4 +312,14 @@ def open(url)
`#{command} #{url}`
end
+
+ # Get the API base path
+ def base_path
+ ENV.key?(GHE_ENV_NAME) ? GHE_BASE_PATH : GITHUB_BASE_PATH
+ end
+
+ # Get the API URL
+ def api_url
+ ENV.key?(GHE_ENV_NAME) ? URI(ENV[GHE_ENV_NAME]) : GITHUB_API_URL
+ end
end

0 comments on commit 5127b8f

Please sign in to comment.
Something went wrong with that request. Please try again.