Skip to content

Commit

Permalink
Make code more testable
Browse files Browse the repository at this point in the history
  • Loading branch information
kui committed Apr 8, 2013
1 parent 528e897 commit 5127b8f
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions lib/jist.rb
Expand Up @@ -17,11 +17,13 @@ module Jist
'putclip' => 'getclip' 'putclip' => 'getclip'
} }


GITHUB_API_URL = URI("https://api.github.com/") GITHUB_API_URL = URI("https://api.github.com/")
GIT_IO_URL = URI("http://git.io") 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 GITHUB_BASE_PATH = ""
API_BASE_PATH = GHE_URL ? "/api/v3" : "" GHE_BASE_PATH = "/api/v3"

GHE_ENV_NAME = "GHE_URL"


# Exception tag for errors raised while gisting. # Exception tag for errors raised while gisting.
module Error; module Error;
Expand Down Expand Up @@ -84,7 +86,7 @@ def multi_gist(files, options={})
access_token = (options[:access_token] || File.read(File.expand_path("~/.jist")) rescue nil) access_token = (options[:access_token] || File.read(File.expand_path("~/.jist")) rescue nil)
end end


url = "#{API_BASE_PATH}/gists" url = "#{base_path}/gists"
url << "/" << CGI.escape(existing_gist) if existing_gist.to_s != '' url << "/" << CGI.escape(existing_gist) if existing_gist.to_s != ''
url << "?access_token=" << CGI.escape(access_token) if access_token.to_s != '' url << "?access_token=" << CGI.escape(access_token) if access_token.to_s != ''


Expand All @@ -95,7 +97,7 @@ def multi_gist(files, options={})
retried = false retried = false


begin begin
response = http(API_URL, request) response = http(api_url, request)
if Net::HTTPSuccess === response if Net::HTTPSuccess === response
on_success(response.body, options) on_success(response.body, options)
else else
Expand Down Expand Up @@ -147,7 +149,7 @@ def login!
end end
puts "" puts ""


request = Net::HTTP::Post.new("#{API_BASE_PATH}/authorizations") request = Net::HTTP::Post.new("#{base_path}/authorizations")
request.body = JSON.dump({ request.body = JSON.dump({
:scopes => [:gist], :scopes => [:gist],
:note => "The jist gem", :note => "The jist gem",
Expand All @@ -156,13 +158,13 @@ def login!
request.content_type = 'application/json' request.content_type = 'application/json'
request.basic_auth(username, password) request.basic_auth(username, password)


response = http(API_URL, request) response = http(api_url, request)


if Net::HTTPCreated === response if Net::HTTPCreated === response
File.open(File.expand_path("~/.jist"), 'w') do |f| File.open(File.expand_path("~/.jist"), 'w') do |f|
f.write JSON.parse(response.body)['token'] f.write JSON.parse(response.body)['token']
end end
puts "Success! https://github.com/settings/applications" puts "Success! #{ENV[GHE_ENV_NAME] || "https://github.com/"}settings/applications"
else else
raise "Got #{response.class} from gist: #{response.body}" raise "Got #{response.class} from gist: #{response.body}"
end end
Expand Down Expand Up @@ -310,4 +312,14 @@ def open(url)


`#{command} #{url}` `#{command} #{url}`
end 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 end

0 comments on commit 5127b8f

Please sign in to comment.