Skip to content

Commit

Permalink
Support API token in URL as well as existing HTTP basic auth
Browse files Browse the repository at this point in the history
  • Loading branch information
Troy Davis committed Sep 1, 2011
1 parent b3c2b38 commit 278ee5a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ may change.
## Quick Start

$ [sudo] gem install papertrail-cli
$ echo "username: your@account.com\npassword: yourpass" > ~/.papertrail.yml
$ echo "token: 123456789012345678901234567890ab" > ~/.papertrail.yml
$ papertrail

Retrieve token from Papertrail [Account Settings].


## Installation

Expand All @@ -33,12 +35,14 @@ Install the gem (details on [RubyGems]), which includes a binary called

## Configuration

Create ~/.papertrail.yml containing your credentials, or specify the
Create ~/.papertrail.yml containing your API token, or specify the
path to that file with -c. Example (from
examples/papertrail.yml.example):

username: your@account.com
password: yourpassword
token: 123456789012345678901234567890ab

Retrieve token from Papertrail [Account Settings]. For compatibility with
older config files, `username` and `password` keys are also supported.

You may want to alias "trail" to "papertrail", like:

Expand Down Expand Up @@ -119,6 +123,7 @@ to your enhancement.
[binary]: https://github.com/papertrail/papertrail-cli/blob/master/bin/papertrail
[Papertrail]: http://papertrailapp.com/
[SearchClient]: https://github.com/papertrail/papertrail-cli/blob/master/lib/papertrail/search_client.rb
[Account Settings]: https://papertrailapp.com/account
[RubyGems]: https://rubygems.org/gems/papertrail-cli
[colortail]: http://rubydoc.info/gems/colortail
[colortailrc]: https://github.com/papertrail/papertrail-cli/wiki/colortailrc
Expand Down
6 changes: 5 additions & 1 deletion bin/papertrail
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ class PapertrailSearch
YAML.load(f)
end

client = Papertrail::SearchClient.new(credentials['username'], credentials['password'])
if credentials['token']
client = Papertrail::SearchClient.new(:token => credentials['token'])
else
client = Papertrail::SearchClient.new(credentials['username'], credentials['password'])
end

search_and_print(client)

Expand Down
3 changes: 1 addition & 2 deletions examples/papertrail.yml.example
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
username: your@account.com
password: yourpassword
token: 123456789012345678901234567890ab
12 changes: 9 additions & 3 deletions lib/papertrail/search_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ class SearchClient
attr_accessor :username, :password, :conn

def initialize(username, password)
@username = username
@password = password
if username.is_a?(Hash)
@options = username
@token = options[:token]
else
@username = username
@password = password
end

ssl_options = { :verify => OpenSSL::SSL::VERIFY_PEER }

Expand All @@ -21,7 +26,7 @@ def initialize(username, password)
end

@conn = Faraday::Connection.new(:url => 'https://papertrailapp.com', :ssl => ssl_options) do |builder|
builder.basic_auth(@username, @password)
builder.basic_auth(@username, @password) if @username && @password

builder.adapter Faraday.default_adapter
builder.response :yajl
Expand All @@ -48,6 +53,7 @@ def params_for_query(q = nil, since = nil)
params[:q] = q if q
params[:min_id] = @max_id_seen[q] if @max_id_seen[q]
params[:min_id] = since if since
params[:token] = @token if @token
params
end

Expand Down

0 comments on commit 278ee5a

Please sign in to comment.