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

#primaryObject

Returns the value of attribute primary.



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

def primary
  @primary
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



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_storeObject

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_htmlObject



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_fancyObject



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_photoObject

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_tagsObject

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



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

#to_liquidObject

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