This repository has been archived by the owner on Jan 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
broke out secrets et. al. into new object
- Loading branch information
1 parent
25936c2
commit 272806a
Showing
4 changed files
with
114 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
module Gliffy | ||
# Encapsulates all the information needed to make a request of Gliffy | ||
# outside of request-specific information. | ||
class Credentials | ||
@@counter = 1 | ||
attr_reader :consumer_key | ||
attr_reader :consumer_secret | ||
attr_reader :access_token | ||
attr_reader :access_secret | ||
attr_reader :account_id | ||
attr_reader :description | ||
|
||
# Create a new Credentials object. | ||
# | ||
# [+consumer_key+] The OAuth consumer key given to you when you signed up | ||
# [+consumer_secret+] The OAuth consumer secret given to you when you signed up | ||
# [+description+] Description of the application you are writing | ||
# [+account_id+] Your account id | ||
# [+access_token+] The access token you were given, or nil if you don't have one yet | ||
# [+access_secret+] The access secret you were given, or nil if you don't have one yet | ||
def initialize(consumer_key, consumer_secret, description, account_id, access_token = nil, access_secret = nil) | ||
raise ArgumentError.new("consumer_key required") if consumer_key.nil? | ||
raise ArgumentError.new("consumer_secret required") if consumer_secret.nil? | ||
raise ArgumentError.new("description required") if description.nil? || description.strip == '' | ||
raise ArgumentError.new("account_id required") if account_id.nil? | ||
|
||
@consumer_key = consumer_key | ||
@consumer_secret = consumer_secret | ||
@access_token = access_token | ||
@access_secret = access_secret | ||
@description = description | ||
@account_id = account_id | ||
end | ||
|
||
# Update the access token | ||
def update_access_token(token,secret) | ||
@access_token = token | ||
@access_secret = secret | ||
end | ||
|
||
# Return a nonce that hasn't been used before (at least not in this space/time continuum) | ||
def nonce | ||
@@counter += 1 | ||
return Base64.encode64((@@counter + rand(100) + Time.new.to_i).to_s).chomp | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
require 'gliffy/credentials' | ||
require 'test/unit' | ||
require 'test/unit/ui/console/testrunner' | ||
|
||
include Gliffy | ||
|
||
class TC_testCredentials < Test::Unit::TestCase | ||
def test_default_access_token | ||
cred = Credentials.new('foo','bar','baz',1234) | ||
assert_equal(nil,cred.access_token) | ||
assert_equal(nil,cred.access_secret) | ||
end | ||
|
||
def test_given_access_token | ||
cred = Credentials.new('foo','bar','baz',1234,'blah','foo') | ||
assert_equal('blah',cred.access_token) | ||
assert_equal('foo',cred.access_secret) | ||
end | ||
|
||
def test_bad_args | ||
assert_raises(ArgumentError) { cred = Credentials.new(nil,nil,nil,nil) } | ||
assert_raises(ArgumentError) { cred = Credentials.new('foo',nil,nil,nil) } | ||
assert_raises(ArgumentError) { cred = Credentials.new('foo','bar',nil,nil) } | ||
assert_raises(ArgumentError) { cred = Credentials.new('foo','bar','',nil) } | ||
assert_raises(ArgumentError) { cred = Credentials.new('foo','bar','crud',nil) } | ||
end | ||
|
||
def test_update_token | ||
cred = Credentials.new('foo','bar','baz',1234) | ||
cred.update_access_token('blah','crud') | ||
assert_equal('blah',cred.access_token) | ||
assert_equal('crud',cred.access_secret) | ||
end | ||
|
||
def test_nonce | ||
cred = Credentials.new('foo','bar','baz',1234) | ||
n1 = cred.nonce | ||
n2 = cred.nonce | ||
assert(n1 != n2,"Two nonces shouldn't be the same: #{n1} =? #{n2}") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters