This repository has been archived by the owner on Apr 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
flickr_photo.rb
91 lines (80 loc) · 2.33 KB
/
flickr_photo.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
class FlickrPhoto
def initialize(id)
@id = id
end
def attributes
@attributes ||= get_attributes
end
# FIXME need to allow some way for attributes to be pre-loaded during creation.
# If these are the only attributes accessed, there is no need to load them.
# However, if other attributes are accessed, then get_attributes is called
# and the attributes are retrieved from flickr.
# Idea! Will probably need to dynamically generate all of those accessor methods to
# achieve the above.
def id
@id
end
def title
attributes[:title]
end
def description
attributes[:description]
end
def taken_at
attributes[:taken_at]
end
def flickr_posted_at
attributes[:flickr_posted_at]
end
def flickr_updated_at
attributes[:flickr_updated_at]
end
def square_source_url
attributes[:square_source_url]
end
def thumb_source_url
attributes[:thumb_source_url]
end
def medium_source_url
attributes[:medium_source_url]
end
def fullsize_source_url
attributes[:fullsize_source_url]
end
# :user_id => user_nsid, :tags => tag
def self.find_all(options)
if !options.empty?
flickr.photos.search({:sort => 'date-posted-desc'}.merge(options)).collect { |photo| new(photo.id) }
else
# TODO return error because we can't have optionless searches
end
end
protected
def get_attributes
attrs = {}
info = flickr.photos.getInfo(:photo_id => @id)
urls = flickr.photos.getSizes(:photo_id => @id)
attrs[:title] = info.title
attrs[:description] = info.description
attrs[:taken_at] = info.dates.taken ? Time.parse(info.dates.taken) : nil # not everything has exif data
attrs[:flickr_posted_at] = Time.at(info.dates.posted.to_i)
# FIXME wrap lastupdate in time.parse?
attrs[:flickr_updated_at] = Time.at(info.dates.lastupdate.to_i)
urls.each do |u|
case u.label
when 'Square':
attrs[:square_source_url] = fix_url(u.source)
when 'Thumbnail':
attrs[:thumb_source_url] = fix_url(u.source)
when 'Medium':
attrs[:medium_source_url] = fix_url(u.source)
when 'Original':
attrs[:fullsize_source_url] = fix_url(u.source)
end
end
attrs
end
def fix_url(str)
str.gsub('\\','').gsub('http: ', 'http:')
end
end