Class: Jekyll::Photoset
- Inherits:
-
Object
- Object
- Jekyll::Photoset
- Defined in:
- lib/badpixxel-jekyll-flickr/photoset.rb
Instance Attribute Summary collapse
-
#cache_dir ⇒ Object
Returns the value of attribute cache_dir.
-
#cache_file ⇒ Object
Returns the value of attribute cache_file.
-
#date_update ⇒ Object
Returns the value of attribute date_update.
-
#date_update_str ⇒ Object
Returns the value of attribute date_update_str.
-
#description ⇒ Object
Returns the value of attribute description.
-
#id ⇒ Object
Returns the value of attribute id.
-
#photos ⇒ Object
Returns the value of attribute photos.
-
#photos_from_cache ⇒ Object
Returns the value of attribute photos_from_cache.
-
#photos_from_flickr ⇒ Object
Returns the value of attribute photos_from_flickr.
-
#primary ⇒ Object
Returns the value of attribute primary.
-
#slug ⇒ Object
Returns the value of attribute slug.
-
#title ⇒ Object
Returns the value of attribute title.
Instance Method Summary collapse
-
#cache_load(site, file) ⇒ Object
Load Photoset from Cache.
-
#cache_outdated(flickr_photoset) ⇒ Object
Check if Photoset Cachge is Outdated.
-
#cache_store ⇒ Object
Store Photoset in Cache.
-
#flickr_load(site, flickr_photoset) ⇒ Object
Load Photoset from Flckr (if needed).
- #gen_html ⇒ Object
- #gen_html_fancy ⇒ Object
-
#get_photo(photo_id) ⇒ Object
Get Photoset Photo.
-
#get_photos_array(max = false) ⇒ Object
Get List of All Photoset Photos.
-
#get_primary_photo ⇒ Object
Get Photoset Primary Photo.
-
#get_tags ⇒ Object
Get List of All Photoset Tags.
-
#get_top_tags(count) ⇒ Object
Get List of Most used tags.
-
#initialize(site, photoset) ⇒ Photoset
constructor
A new instance of Photoset.
-
#to_liquid ⇒ Object
Convert PhotoSet to liquid Format.
Constructor Details
#initialize(site, photoset) ⇒ Photoset
Returns a new instance of Photoset.
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/badpixxel-jekyll-flickr/photoset.rb', line 20 def initialize(site, photoset) self.photos = Array.new self.photos_from_cache = 0 self.photos_from_flickr = 0 if photoset.is_a? String self.cache_load(site, photoset) else self.flickr_load(site, photoset) end self.photos.sort! {|left, right| left.position <=> right.position} end |
Instance Attribute Details
#cache_dir ⇒ Object
Returns the value of attribute cache_dir.
18 19 20 |
# File 'lib/badpixxel-jekyll-flickr/photoset.rb', line 18 def cache_dir @cache_dir end |
#cache_file ⇒ Object
Returns the value of attribute cache_file.
18 19 20 |
# File 'lib/badpixxel-jekyll-flickr/photoset.rb', line 18 def cache_file @cache_file end |
#date_update ⇒ Object
Returns the value of attribute date_update.
18 19 20 |
# File 'lib/badpixxel-jekyll-flickr/photoset.rb', line 18 def date_update @date_update end |
#date_update_str ⇒ Object
Returns the value of attribute date_update_str.
18 19 20 |
# File 'lib/badpixxel-jekyll-flickr/photoset.rb', line 18 def date_update_str @date_update_str end |
#description ⇒ Object
Returns the value of attribute description.
18 19 20 |
# File 'lib/badpixxel-jekyll-flickr/photoset.rb', line 18 def description @description end |
#id ⇒ Object
Returns the value of attribute id.
18 19 20 |
# File 'lib/badpixxel-jekyll-flickr/photoset.rb', line 18 def id @id end |
#photos ⇒ Object
Returns the value of attribute photos.
18 19 20 |
# File 'lib/badpixxel-jekyll-flickr/photoset.rb', line 18 def photos @photos end |
#photos_from_cache ⇒ Object
Returns the value of attribute photos_from_cache.
18 19 20 |
# File 'lib/badpixxel-jekyll-flickr/photoset.rb', line 18 def photos_from_cache @photos_from_cache end |
#photos_from_flickr ⇒ Object
Returns the value of attribute photos_from_flickr.
18 19 20 |
# File 'lib/badpixxel-jekyll-flickr/photoset.rb', line 18 def photos_from_flickr @photos_from_flickr end |
#primary ⇒ Object
Returns the value of attribute primary.
18 19 20 |
# File 'lib/badpixxel-jekyll-flickr/photoset.rb', line 18 def primary @primary end |
#slug ⇒ Object
Returns the value of attribute slug.
18 19 20 |
# File 'lib/badpixxel-jekyll-flickr/photoset.rb', line 18 def slug @slug end |
#title ⇒ Object
Returns the value of attribute title.
18 19 20 |
# File 'lib/badpixxel-jekyll-flickr/photoset.rb', line 18 def title @title end |
Instance Method Details
#cache_load(site, file) ⇒ Object
Load Photoset from Cache
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/badpixxel-jekyll-flickr/photoset.rb', line 83 def cache_load(site, file) cached = YAML::load(File.read(file)) self.id = cached['id'] self.title = cached['title'] self.description = cached['description'] self.primary = cached['primary'] self.date_update = cached['date_update'] self.date_update_str = cached['date_update_str'] self.slug = cached['slug'] self.cache_dir = cached['cache_dir'] self.cache_file = cached['cache_file'] file_photos = Dir.glob(File.join(self.cache_dir, '*.yml')) file_photos.each_with_index do |file_photo, pos| self.photos << Photo.new(site, self, file_photo, pos) end end |
#cache_outdated(flickr_photoset) ⇒ Object
Check if Photoset Cachge is Outdated
68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/badpixxel-jekyll-flickr/photoset.rb', line 68 def cache_outdated(flickr_photoset) # Check if Photoset is Already in Cache if !File.exists?(self.cache_file) return true end # Load Cached Values cached = YAML::load(File.read(cache_file)) if !cached["date_update"] or (cached["date_update"] != flickr_photoset.date_update) return true end return false end |
#cache_store ⇒ Object
Store Photoset in Cache
102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/badpixxel-jekyll-flickr/photoset.rb', line 102 def cache_store cached = Hash.new cached['id'] = self.id cached['title'] = self.title cached['description'] = self.description cached['primary'] = self.primary cached['date_update'] = self.date_update cached['date_update_str'] = self.date_update_str cached['slug'] = self.slug cached['cache_dir'] = self.cache_dir cached['cache_file'] = self.cache_file File.open(self.cache_file, 'w') {|f| f.print(YAML::dump(cached))} end |
#flickr_load(site, flickr_photoset) ⇒ Object
Load Photoset from Flckr (if needed)
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 |
# File 'lib/badpixxel-jekyll-flickr/photoset.rb', line 33 def flickr_load(site, flickr_photoset) self.id = flickr_photoset.id self.title = flickr_photoset.title self.description = flickr_photoset.description self.primary = flickr_photoset.primary self.date_update = flickr_photoset.date_update self.date_update_str = DateTime.strptime(flickr_photoset.date_update, '%s').to_s self.slug = self.title.downcase.gsub(/ /, '-').gsub(/[^a-z\-]/, '') self.cache_dir = File.join(site.config['flickr']['cache_dir'], self.slug) self.cache_file = File.join(site.config['flickr']['cache_dir'], "#{self.slug}.yml") # write to cache if self.cache_outdated(flickr_photoset) self.cache_store end # create cache directory if !Dir.exists?(self.cache_dir) Dir.mkdir(self.cache_dir) end # photos flickr_photos = flickr.photosets.getPhotos(:photoset_id => self.id, :extras => "date_taken, last_update").photo flickr_photos.each_with_index do |flickr_photo, pos| photo = Photo.new(site, self, flickr_photo, pos) self.photos << photo if photo.from_cache self.photos_from_cache += 1 else self.photos_from_flickr += 1 end end end |
#gen_html ⇒ Object
213 214 215 216 217 218 219 |
# File 'lib/badpixxel-jekyll-flickr/photoset.rb', line 213 def gen_html content = '' self.photos.each do |photo| content += photo.gen_thumb_html end return content end |
#gen_html_fancy ⇒ Object
221 222 223 224 225 226 227 |
# File 'lib/badpixxel-jekyll-flickr/photoset.rb', line 221 def gen_html_fancy content = '' self.photos.each do |photo| content += photo.gen_thumb_html_fancy end return content end |
#get_photo(photo_id) ⇒ Object
Get Photoset Photo
203 204 205 206 207 208 209 210 211 |
# File 'lib/badpixxel-jekyll-flickr/photoset.rb', line 203 def get_photo(photo_id) self.photos.each do |photo| if photo.id == photo_id return photo end end return nil end |
#get_photos_array(max = false) ⇒ Object
Get List of All Photoset Photos
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/badpixxel-jekyll-flickr/photoset.rb', line 176 def get_photos_array(max = false) # Build List of Photos [ "tag" => count ] if max != false photos = self.photos.first(max) else photos = self.photos end collection = [] photos.each do |photo| collection += photo.to_liquid end return collection end |
#get_primary_photo ⇒ Object
Get Photoset Primary Photo
193 194 195 196 197 198 199 200 |
# File 'lib/badpixxel-jekyll-flickr/photoset.rb', line 193 def get_primary_photo() photo = self.get_photo(self.primary) if photo return photo end return self.photos.first() end |
#get_tags ⇒ Object
Get List of All Photoset Tags
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/badpixxel-jekyll-flickr/photoset.rb', line 154 def () # Build List of tags [ "tag" => count ] = {} self.photos.each do |photo| photo..each do |tag| if .has_key?(tag) [tag] += 1 else [tag] = 1 end end end # Build Hashed Tags List = [] .each do |tag_str, count| += [ "tag" => tag_str, "hash" => tag_str.hash, "count" => count ] end return end |
#get_top_tags(count) ⇒ Object
Get List of Most used tags
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/badpixxel-jekyll-flickr/photoset.rb', line 130 def (count) # Build List of tags [ "tag" => count ] = {} self.photos.each do |photo| photo..each do |tag| if .has_key?(tag) [tag] += 1 else [tag] = 1 end end end # Sort & Filter tags = .sort_by {|_key, value| value}.reverse.first(count).to_h # Build Hashed Tags List = [] .each do |tag_str, count| += [ "tag" => tag_str, "hash" => tag_str.hash, "count" => count ] end return end |
#to_liquid ⇒ Object
Convert PhotoSet to liquid Format
118 119 120 121 122 123 124 125 126 127 |
# File 'lib/badpixxel-jekyll-flickr/photoset.rb', line 118 def to_liquid return [ 'id' => self.id, 'title' => self.title, 'description' => self.description, 'primary' => self.primary, 'slug' => self.slug, 'date_update' => self.date_update, ] end |