Skip to content
This repository has been archived by the owner on Jan 6, 2019. It is now read-only.

Commit

Permalink
broke out secrets et. al. into new object
Browse files Browse the repository at this point in the history
  • Loading branch information
davetron5000 committed Apr 19, 2009
1 parent 25936c2 commit 272806a
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 20 deletions.
47 changes: 47 additions & 0 deletions lib/gliffy/credentials.rb
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
31 changes: 17 additions & 14 deletions lib/gliffy/url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,25 @@ def self.encode(string)
# [:method] The HTTP Request method that will be made
# [:url] The URL (without parameters) to request
#
def initialize(options)
raise ArgumentError.new("consumer_key is required") if !options[:consumer_key]
raise ArgumentError.new("consumer_secret is required") if !options[:consumer_secret]
raise ArgumentError.new("url is required") if !options[:url]
raise ArgumentError.new("method is required") if !options[:method]
def initialize(credentials,url,method)
raise ArgumentError.new("credentials is required") if credentials.nil?
raise ArgumentError.new("url is required") if url.nil?
raise ArgumentError.new("method is required") if method.nil?

# TODO externalize this somehow
@logger = Logger.new(Config.config.log_device)
@logger.level = Config.config.log_level
@params = Hash.new
@params['oauth_consumer_key'] = options[:consumer_key]
@params['oauth_token'] = options[:access_token] if options[:access_token]
@params['oauth_signature_method'] = 'HMAC-SHA1'
@params['oauth_version'] = '1.0'
@consumer_secret = options[:consumer_secret]
@access_secret = options[:access_secret]
@method = options[:method].upcase
@url = options[:url]

@params = {
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_version' => '1.0',
}
@params['oauth_consumer_key'] = credentials.consumer_key
@params['oauth_token'] = credentials.access_token if credentials.access_token
@consumer_secret = credentials.consumer_secret
@access_secret = credentials.access_secret
@method = method.upcase
@url = url
end

# Sets a request parameter
Expand Down
41 changes: 41 additions & 0 deletions test/tc_credentials.rb
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
15 changes: 9 additions & 6 deletions test/tc_url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
class TC_testURL < Test::Unit::TestCase

def setup
@signed_url = SignedURL.new(:consumer_key => 'dpf43f3p2l4k3l03',
:consumer_secret => 'kd94hf93k423kf44',
:url => 'http://photos.example.net/photos',
:access_token => 'nnch734d00sl2jdk',
:access_secret => 'pfkkdhi9sl3r4s00',
:method => 'GET')
@cred = Credentials.new('dpf43f3p2l4k3l03',
'kd94hf93k423kf44',
'Test Cases',
666,
'nnch734d00sl2jdk',
'pfkkdhi9sl3r4s00')
@signed_url = SignedURL.new(@cred,
'http://photos.example.net/photos',
'GET')
end

def test_bad_param_override
Expand Down

0 comments on commit 272806a

Please sign in to comment.