Class: Flickr

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

Defined Under Namespace

Classes: APIBase, Auth, BaseTokenCache, Blog, Blogs, Category, Comment, Comments, Contacts, Context, Count, Exif, Favorites, FileTokenCache, 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)


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

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.



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

def api_key
  @api_key
end

#asyncObject

Returns the value of attribute async.



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

def async
  @async
end

#auth_modeObject

Returns the value of attribute auth_mode.



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

def auth_mode
  @auth_mode
end

#cachingObject

Returns the value of attribute caching.



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

def caching
  @caching
end

#debug(*args) ⇒ Object

Returns the value of attribute debug.



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

def debug
  @debug
end

Class Method Details

.todoObject



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

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



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

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

#blog_cache_lookupObject



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

def blog_cache_lookup() @blog_cache if @caching end

#blog_cache_store(blogs) ⇒ Object



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

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

#blogsObject



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

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

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



175
176
177
178
179
180
181
# File 'lib/flickr/base.rb', line 175

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



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

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

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



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
171
172
173
# File 'lib/flickr/base.rb', line 146

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



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

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
  @comments = 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

#commentsObject



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

def comments() @comments ||= Comments.new(self) end

#contactsObject



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

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

#favoritesObject



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

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

#group_cache_lookup(id) ⇒ Object



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

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

#group_cache_store(group) ⇒ Object



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

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

#groupsObject



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

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

#interestingnessObject



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

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

#license_cache_lookupObject



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

def license_cache_lookup() @license_cache if @caching end

#license_cache_store(licenses) ⇒ Object



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

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

#mysql_date(time) ⇒ Object



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

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

#mysql_datetime(time) ⇒ Object



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

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

#parse_url(url) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/flickr/base.rb', line 187

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



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

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

#person_cache_lookup(nsid) ⇒ Object



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

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

#person_cache_store(person) ⇒ Object



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

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

#photo_cache_lookup(id) ⇒ Object



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

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

#photo_cache_store(photo) ⇒ Object



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

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

#photopool_cache_lookup(id) ⇒ Object



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

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

#photopool_cache_store(pool) ⇒ Object



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

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

#photosObject



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

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

#photoset_cache_lookup(id) ⇒ Object



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

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

#photoset_cache_store(set) ⇒ Object



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

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

#photosetsObject



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

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

#reflectionObject



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

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

#sign(args) ⇒ Object



183
184
185
# File 'lib/flickr/base.rb', line 183

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

#tagsObject



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

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

#testObject



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

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

#ticket_cache_lookup(id) ⇒ Object

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



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

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

#ticket_cache_store(ticket) ⇒ Object



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

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

#todoObject



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

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

#urlsObject



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

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