Class: Jekyll::Photoset

Inherits:
Object
  • Object
show all
Defined in:
lib/badpixxel-jekyll-flickr/photoset.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_dirObject

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_fileObject

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_updateObject

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_strObject

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

#descriptionObject

Returns the value of attribute description.



18
19
20
# File 'lib/badpixxel-jekyll-flickr/photoset.rb', line 18

def description
  @description
end

#idObject

Returns the value of attribute id.



18
19
20
# File 'lib/badpixxel-jekyll-flickr/photoset.rb', line 18

def id
  @id
end

#photosObject

Returns the value of attribute photos.



18
19
20
# File 'lib/badpixxel-jekyll-flickr/photoset.rb', line 18

def photos
  @photos
end

#photos_from_cacheObject

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_flickrObject

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

#slugObject

Returns the value of attribute slug.



18
19
20
# File 'lib/badpixxel-jekyll-flickr/photoset.rb', line 18

def slug
  @slug
end

#titleObject

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



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/badpixxel-jekyll-flickr/photoset.rb', line 82

def cache_load(site, file)
    cached = YAML::load(File.read(file))
    self.id = cached['id']
    self.title = cached['title']
    self.description = cached['description']
    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



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/badpixxel-jekyll-flickr/photoset.rb', line 67

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_storeObject

Store Photoset in Cache



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/badpixxel-jekyll-flickr/photoset.rb', line 100

def cache_store
    cached = Hash.new
    cached['id'] = self.id
    cached['title'] = self.title
    cached['description'] = self.description
    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
# 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.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_htmlObject



177
178
179
180
181
182
183
# File 'lib/badpixxel-jekyll-flickr/photoset.rb', line 177

def gen_html
    content = ''
    self.photos.each do |photo|
        content += photo.gen_thumb_html
    end
    return content
end

#gen_html_fancyObject



185
186
187
188
189
190
191
# File 'lib/badpixxel-jekyll-flickr/photoset.rb', line 185

def gen_html_fancy
    content = ''
    self.photos.each do |photo|
        content += photo.gen_thumb_html_fancy
    end
    return content
end

#get_photos_array(max = false) ⇒ Object

Get List of All Photoset Photos



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/badpixxel-jekyll-flickr/photoset.rb', line 161

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_tagsObject

Get List of All Photoset Tags



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/badpixxel-jekyll-flickr/photoset.rb', line 139

def get_tags()
    # Build List of tags [ "tag" => count ]
    all_tags = {}
    self.photos.each do |photo|
        photo.tags.each do |tag|
            if all_tags.has_key?(tag)
                all_tags[tag] += 1
            else
                all_tags[tag] = 1
            end
        end
    end
    # Build Hashed Tags List
    hashed_tags = []
    all_tags.each do |tag_str, count|
        hashed_tags += [ "tag" => tag_str, "hash" => tag_str.hash, "count" => count ]
    end

    return hashed_tags
end

#get_top_tags(count) ⇒ Object

Get List of Most used tags



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/badpixxel-jekyll-flickr/photoset.rb', line 115

def get_top_tags(count)
    # Build List of tags [ "tag" => count ]
    all_tags = {}
    self.photos.each do |photo|
        photo.tags.each do |tag|
            if all_tags.has_key?(tag)
                all_tags[tag] += 1
            else
                all_tags[tag] = 1
            end
        end
    end
    # Sort & Filter tags
    sorted_tags = all_tags.sort_by {|_key, value| value}.reverse.first(count).to_h
    # Build Hashed Tags List
    hashed_tags = []
    sorted_tags.each do |tag_str, count|
        hashed_tags += [ "tag" => tag_str, "hash" => tag_str.hash, "count" => count ]
    end

    return hashed_tags
end