Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/gist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,17 @@ def self.write(token)
f.write token
end
end

def self.read_token_or_fetch_from_env
ENV['GITHUB_AUTH_TOKEN'] || self.read rescue nil

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable name should be namespaced somehow to prevent conflicts. For instance, GIST_GITHUB_API_TOKEN or something.

end
end

# auth token for authentication
#
# @return [String] string value of access token or `nil`, if not found
def auth_token
@token ||= AuthTokenFile.read rescue nil
@token ||= AuthTokenFile.read_token_or_fetch_from_env
end

# Upload a gist to https://gist.github.com
Expand Down
8 changes: 8 additions & 0 deletions spec/auth_token_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

describe "::read" do
let(:token) { "auth_token" }
let(:env_variable_name) { "GITHUB_AUTH_TOKEN" }

it "reads file contents" do
File.should_receive(:read).and_return(token)
Expand All @@ -41,6 +42,13 @@
File.should_receive(:read).and_return(token + "\n")
subject.read.should eq token
end

it 'should pickup auth_token if set in environment variable' do
cached_env_variable = ENV[env_variable_name]
ENV[env_variable_name] = "auth_token"
subject.read_token_or_fetch_from_env.should eq token
ENV[env_variable_name] = cached_env_variable
end
end

describe "::write" do
Expand Down