Class: Flickr

Inherits:
Object
  • Object
show all
Defined in:
lib/flickr/base.rb

Defined Under Namespace

Classes: APIBase, Auth, Blog, Blogs, Category, Contacts, Context, Count, Exif, Favorites, FormPart, Group, GroupList, Groups, Interestingness, License, Licenses, Method, MethodArgument, MultiPartForm, Note, Notes, People, Person, Photo, PhotoList, PhotoPool, PhotoSet, PhotoSets, Photos, Pools, Reflection, Size, SubCategory, Tag, Tags, Test, Ticket, Token, Transform, Upload, Urls

Constant Summary collapse

API_KEY =
''
SHARED_SECRET =
''

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token_cache = nil, api_key = API_KEY, shared_secret = SHARED_SECRET, endpoint = 'http://www.flickr.com/services/xmlrpc/') ⇒ Flickr

Returns a new instance of Flickr.

Raises:

  • (ProtoUnknownError)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/flickr/base.rb', line 83

def initialize(token_cache=nil,api_key=API_KEY,
		shared_secret=SHARED_SECRET,
		endpoint='http://www.flickr.com/services/xmlrpc/')
	@async = false
	@caching = true
	@auth_mode = true
	@api_key=api_key
	@shared_secret=shared_secret
	@token_cache = token_cache
	@endpoint=endpoint
	proto,host,port,path,user,pass=parse_url(@endpoint)
	raise ProtoUnknownError.new("Unhandled protocol '#{proto}'") if
		proto.downcase != 'http'
	@client=XMLRPC::Client.new(host,path,port)
	clear_cache
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



16
17
18
# File 'lib/flickr/base.rb', line 16

def api_key
  @api_key
end

#asyncObject

Returns the value of attribute async.



17
18
19
# File 'lib/flickr/base.rb', line 17

def async
  @async
end

#auth_modeObject

Returns the value of attribute auth_mode.



17
18
19
# File 'lib/flickr/base.rb', line 17

def auth_mode
  @auth_mode
end

#cachingObject

Returns the value of attribute caching.



17
18
19
# File 'lib/flickr/base.rb', line 17

def caching
  @caching
end

#debug(*args) ⇒ Object

Returns the value of attribute debug.



17
18
19
# File 'lib/flickr/base.rb', line 17

def debug
  @debug
end

Class Method Details

.todoObject



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

def Flickr.todo
	[
  'Refactor, especially more Class.from_xml methods',
  'More logical OO design, wrap the API methods to make transparent',
  'Class & method documentation',
  'Unit tests',
  'Implement missing methods (see flickr.reflection.missing_methods)'
  	]
end

Instance Method Details

#authObject



124
# File 'lib/flickr/base.rb', line 124

def auth() @auth ||= Auth.new(self,@token_cache) end

#blog_cache_lookupObject



44
# File 'lib/flickr/base.rb', line 44

def blog_cache_lookup() @blog_cache if @caching end

#blog_cache_store(blogs) ⇒ Object



46
# File 'lib/flickr/base.rb', line 46

def blog_cache_store(blogs) @blog_cache = blogs if @caching end

#blogsObject



125
# File 'lib/flickr/base.rb', line 125

def blogs() @blogs ||= Blogs.new(self) end

#call_auth_method(method, args = {}) ⇒ Object



172
173
174
175
176
177
178
# File 'lib/flickr/base.rb', line 172

def call_auth_method(method,args={})
	at = args['auth_token']
	args['auth_token'] ||= auth.token.token
	res = call_unauth_method(method,args)
	args.delete('auth_token') unless at
	return res
end

#call_method(method, args = {}) ⇒ Object



138
139
140
141
# File 'lib/flickr/base.rb', line 138

def call_method(method,args={})
	@auth_mode ? call_auth_method(method,args) :
	             call_unauth_method(method,args)
end

#call_unauth_method(method, args = {}) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/flickr/base.rb', line 143

def call_unauth_method(method,args={})
	debug('%s(%s)', method, args.inspect)
	tries = 3
	args = args.dup
	args['api_key'] = @api_key
	api_sig=sign(args)
	args['api_sig']=api_sig
	begin
		tries -= 1;
		str = @async ? @client.call_async(method,args) :
			@client.call(method,args)
		debug('RETURN: %s',str)
		return REXML::Document.new(str)
	rescue Timeout::Error => te
		$stderr.puts "Timed out, will try #{tries} more times."
		if tries > 0
			retry
		else
			raise te
		end
	rescue REXML::ParseException => pe
		return REXML::Document.new('<rsp>'+str+'</rsp>').
			elements['/rsp']
	rescue XMLRPC::FaultException => fe
		$stderr.puts "ERR: #{fe.faultString} (#{fe.faultCode})"
		raise fe
	end
end

#clear_cacheObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/flickr/base.rb', line 100

def clear_cache()
	@auth = nil
	@blogs = nil
	@contacts = nil
	@favorites = nil
	@groups = nil
	@interestingness = nil
	@reflection = nil
	@people = nil
	@photos = nil
	@photosets = nil
	@test = nil
	@urls = nil

	@ticket_by_id = {}
	@person_by_nsid = {}
	@photo_by_id = {}
	@photoset_by_id = {}
	@photopool_by_id = {}
	@group_by_id = {}
	@license_cache = nil
	@blog_cache = nil
end

#contactsObject



126
# File 'lib/flickr/base.rb', line 126

def contacts() @contacts ||= Contacts.new(self) end

#favoritesObject



127
# File 'lib/flickr/base.rb', line 127

def favorites() @favorites ||= Favorites.new(self) end

#group_cache_lookup(id) ⇒ Object



60
# File 'lib/flickr/base.rb', line 60

def group_cache_lookup(id) @group_by_id[id] if @caching end

#group_cache_store(group) ⇒ Object



62
63
64
# File 'lib/flickr/base.rb', line 62

def group_cache_store(group)
	@group_by_id[group.id] = group if @caching
end

#groupsObject



128
# File 'lib/flickr/base.rb', line 128

def groups() @groups ||= Groups.new(self) end

#interestingnessObject



136
# File 'lib/flickr/base.rb', line 136

def interestingness() @interestingness ||= Interestingness.new(self) end

#license_cache_lookupObject



38
# File 'lib/flickr/base.rb', line 38

def license_cache_lookup() @license_cache if @caching end

#license_cache_store(licenses) ⇒ Object



40
41
42
# File 'lib/flickr/base.rb', line 40

def license_cache_store(licenses)
	@license_cache = licenses if @caching
end

#mysql_date(time) ⇒ Object



201
# File 'lib/flickr/base.rb', line 201

def mysql_date(time) time.strftime('%Y-%m-%d') end

#mysql_datetime(time) ⇒ Object



200
# File 'lib/flickr/base.rb', line 200

def mysql_datetime(time) time.strftime('%Y-%m-%d %H:%M:%S') end

#parse_url(url) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/flickr/base.rb', line 184

def parse_url(url)
	url =~ /([^:]+):\/\/([^\/]*)(.*)/
	proto = $1.to_s
	hostplus = $2.to_s
	path = $3.to_s

	hostplus =~ /(?:(.*)@)?(.*)/
	userpass = $1
	hostport = $2
	user,pass = userpass.to_s.split(':',2)
	host,port = hostport.to_s.split(':',2)
	port = port ? port.to_i : 80

	return proto,host,port,path,user,pass
end

#peopleObject



129
# File 'lib/flickr/base.rb', line 129

def people() @people ||= People.new(self) end

#person_cache_lookup(nsid) ⇒ Object



26
# File 'lib/flickr/base.rb', line 26

def person_cache_lookup(nsid) @person_by_nsid[nsid] if @caching end

#person_cache_store(person) ⇒ Object



28
29
30
# File 'lib/flickr/base.rb', line 28

def person_cache_store(person)
	@person_by_nsid[person.nsid] = person if @caching
end

#photo_cache_lookup(id) ⇒ Object



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

def photo_cache_lookup(id) @photo_by_id[id] if @caching end

#photo_cache_store(photo) ⇒ Object



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

def photo_cache_store(photo)
	@photo_by_id[photo.id] = photo if @caching
end

#photopool_cache_lookup(id) ⇒ Object



54
# File 'lib/flickr/base.rb', line 54

def photopool_cache_lookup(id) @photopool_by_id[id] if @caching end

#photopool_cache_store(pool) ⇒ Object



56
57
58
# File 'lib/flickr/base.rb', line 56

def photopool_cache_store(pool)
	@photopool_by_id[pool.id] = pool if @caching
end

#photosObject



130
# File 'lib/flickr/base.rb', line 130

def photos() @photos ||= Photos.new(self) end

#photoset_cache_lookup(id) ⇒ Object



48
# File 'lib/flickr/base.rb', line 48

def photoset_cache_lookup(id) @photoset_by_id[id] if @caching end

#photoset_cache_store(set) ⇒ Object



50
51
52
# File 'lib/flickr/base.rb', line 50

def photoset_cache_store(set)
	@photoset_by_id[set.id] = set if @caching
end

#photosetsObject



131
# File 'lib/flickr/base.rb', line 131

def photosets() @photosets ||= PhotoSets.new(self) end

#reflectionObject



132
# File 'lib/flickr/base.rb', line 132

def reflection() @reflection ||= Reflection.new(self) end

#sign(args) ⇒ Object



180
181
182
# File 'lib/flickr/base.rb', line 180

def sign(args)
	return MD5.md5(@shared_secret+args.sort.flatten.join).to_s
end

#tagsObject



135
# File 'lib/flickr/base.rb', line 135

def tags() @tags ||= Tags.new(self) end

#testObject



133
# File 'lib/flickr/base.rb', line 133

def test() @test ||= Test.new(self) end

#ticket_cache_lookup(id) ⇒ Object

CACHE ACCESSORS ###########################



20
# File 'lib/flickr/base.rb', line 20

def ticket_cache_lookup(id) @ticket_by_id[id] if @caching end

#ticket_cache_store(ticket) ⇒ Object



22
23
24
# File 'lib/flickr/base.rb', line 22

def ticket_cache_store(ticket)
	@ticket_by_id[ticket.id] = ticket if @caching
end

#todoObject



78
79
80
# File 'lib/flickr/base.rb', line 78

def todo()
	Flickr.todo+reflection.missing_methods.map{|m| 'Implement '+m}
end

#urlsObject



134
# File 'lib/flickr/base.rb', line 134

def urls() @urls ||= Urls.new(self) end