Class: Flickr::Auth

Inherits:
APIBase show all
Defined in:
lib/flickr/auth.rb

Instance Attribute Summary collapse

Attributes inherited from APIBase

#flickr

Instance Method Summary collapse

Constructor Details

#initialize(flickr, cache_file = nil) ⇒ Auth

Returns a new instance of Auth.



13
14
15
16
17
18
19
20
21
# File 'lib/flickr/auth.rb', line 13

def initialize(flickr,cache_file=nil)
	super(flickr)
	@frob = nil
	@token = nil
	@cache_file = cache_file
	if @cache_file && File.exists?(@cache_file)
		load_token
	end
end

Instance Attribute Details

#cache_fileObject

Returns the value of attribute cache_file.



6
7
8
# File 'lib/flickr/auth.rb', line 6

def cache_file
  @cache_file
end

#tokenObject

Returns the value of attribute token.



6
7
8
# File 'lib/flickr/auth.rb', line 6

def token
  @token
end

Instance Method Details

#cache_tokenObject



34
35
36
# File 'lib/flickr/auth.rb', line 34

def cache_token
	File.open(@cache_file,'w'){ |f| f.write @token.to_xml } if token
end

#checkToken(token = nil) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/flickr/auth.rb', line 69

def checkToken(token=nil)
	token ||= @token
	token = token.token if token.class == Flickr::Token
	res = @flickr.call_unauth_method('flickr.auth.checkToken',
		'auth_token' => token)
	@token = Flickr::Token.from_xml(res)
end

#clear_cacheObject



8
9
10
11
# File 'lib/flickr/auth.rb', line 8

def clear_cache
	@token = nil
	@frob = nil
end

#frobObject



32
# File 'lib/flickr/auth.rb', line 32

def frob() return @frob || getFrob end

#frob=(frob) ⇒ Object



31
# File 'lib/flickr/auth.rb', line 31

def frob=(frob) @frob = frob end

#getFrobObject



63
64
65
66
67
# File 'lib/flickr/auth.rb', line 63

def getFrob
	doc = @flickr.call_unauth_method('flickr.auth.getFrob')
	@frob = doc.elements['/frob'].text
	return @frob
end

#getFullToken(mini_token) ⇒ Object



57
58
59
60
61
# File 'lib/flickr/auth.rb', line 57

def getFullToken(mini_token)
	res = flickr.call_unauth_method('flickr.auth.getFullToken',
			'mini_token' => mini_token)
	@token = Flickr::Token.from_xml(res)
end

#getToken(frob = nil) ⇒ Object



50
51
52
53
54
55
# File 'lib/flickr/auth.rb', line 50

def getToken(frob=nil)
	frob ||= @frob
	res=@flickr.call_unauth_method('flickr.auth.getToken',
			'frob'=>frob)
	@token = Flickr::Token.from_xml(res)
end

#load_tokenObject



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/flickr/auth.rb', line 38

def load_token
	token = nil
	File.open(@cache_file,'r'){ |f| token = f.read }
	# Dirt stupid check to see if it's probably XML or
	# not.  If it is, then we don't call checkToken.
	#
	# Backwwards compatible with old token storage.
	@token = token.include?('<') ?
		Flickr::Token.from_xml(REXML::Document.new(token)) :
		@token = checkToken(token)
end


23
24
25
26
27
28
29
# File 'lib/flickr/auth.rb', line 23

def (perms='delete')
	args={ 'api_key' => @flickr.api_key, 'perms' => perms}
	args['frob'] = self.frob
	args['api_sig'] = @flickr.sign(args)
	return "http://flickr.com/services/auth/?"+
		args.to_a.map{|arr| arr.join('=')}.join('&')
end