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, 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)


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/flickr/base.rb', line 101

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.



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

def api_key
  @api_key
end

#asyncObject

Returns the value of attribute async.



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

def async
  @async
end

#auth_modeObject

Returns the value of attribute auth_mode.



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

def auth_mode
  @auth_mode
end

#cachingObject

Returns the value of attribute caching.



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

def caching
  @caching
end

#debug(*args) ⇒ Object

Returns the value of attribute debug.



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

def debug
  @debug
end

Class Method Details

.todoObject



87
88
89
90
91
92
93
94
95
# File 'lib/flickr/base.rb', line 87

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



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

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

#blog_cache_lookupObject



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

def blog_cache_lookup() @blog_cache if @caching end

#blog_cache_store(blogs) ⇒ Object



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

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

#blogsObject



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

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

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



188
189
190
191
192
193
194
# File 'lib/flickr/base.rb', line 188

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



154
155
156
157
# File 'lib/flickr/base.rb', line 154

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

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



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/flickr/base.rb', line 159

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



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/flickr/base.rb', line 118

def clear_cache()
	@auth = nil
	@blogs = nil
	@contacts = nil
	@favorites = nil
	@groups = nil
	@interestingness = nil
	@reflection = nil
	@people = nil
	@photos = nil
	@photosets = 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



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

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

#favoritesObject



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

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

#group_cache_lookup(id) ⇒ Object



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

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

#group_cache_store(group) ⇒ Object



80
81
82
# File 'lib/flickr/base.rb', line 80

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

#groupsObject



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

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

#interestingnessObject



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

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

#license_cache_lookupObject



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

def license_cache_lookup() @license_cache if @caching end

#license_cache_store(licenses) ⇒ Object



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

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

#mysql_date(time) ⇒ Object



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

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

#mysql_datetime(time) ⇒ Object



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

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

#parse_url(url) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/flickr/base.rb', line 200

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



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

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

#person_cache_lookup(nsid) ⇒ Object



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

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

#person_cache_store(person) ⇒ Object



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

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

#photo_cache_lookup(id) ⇒ Object



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

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

#photo_cache_store(photo) ⇒ Object



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

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

#photopool_cache_lookup(id) ⇒ Object



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

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

#photopool_cache_store(pool) ⇒ Object



74
75
76
# File 'lib/flickr/base.rb', line 74

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

#photosObject



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

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

#photoset_cache_lookup(id) ⇒ Object



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

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

#photoset_cache_store(set) ⇒ Object



68
69
70
# File 'lib/flickr/base.rb', line 68

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

#photosetsObject



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

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

#reflectionObject



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

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

#sign(args) ⇒ Object



196
197
198
# File 'lib/flickr/base.rb', line 196

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

#tagsObject



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

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

#ticket_cache_lookup(id) ⇒ Object

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



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

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

#ticket_cache_store(ticket) ⇒ Object



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

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

#todoObject



97
98
99
# File 'lib/flickr/base.rb', line 97

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

#urlsObject



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

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