Class: Jekyll::Photo

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, photoset, photo, pos) ⇒ Photo

Returns a new instance of Photo.



20
21
22
23
24
25
26
27
# File 'lib/badpixxel-jekyll-flickr/photo.rb', line 20

def initialize(site, photoset, photo, pos)
    self.from_cache = false
    if photo.is_a? String
        self.cache_load(photo)
    else
        self.flickr_load(site, photoset, photo, pos)
    end
end

Instance Attribute Details

#cache_fileObject

Returns the value of attribute cache_file.



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

def cache_file
  @cache_file
end

#dateObject

Returns the value of attribute date.



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

def date
  @date
end

#date_updateObject

Returns the value of attribute date_update.



18
19
20
# File 'lib/badpixxel-jekyll-flickr/photo.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/photo.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/photo.rb', line 18

def description
  @description
end

#from_cacheObject

Returns the value of attribute from_cache.



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

def from_cache
  @from_cache
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#positionObject

Returns the value of attribute position.



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

def position
  @position
end

#slugObject

Returns the value of attribute slug.



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

def slug
  @slug
end

#tagsObject

Returns the value of attribute tags.



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

def tags
  @tags
end

#titleObject

Returns the value of attribute title.



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

def title
  @title
end

#url_fullObject

Returns the value of attribute url_full.



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

def url_full
  @url_full
end

#url_thumbObject

Returns the value of attribute url_thumb.



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

def url_thumb
  @url_thumb
end

Instance Method Details

#cache_exists(photoset, flickr_photo) ⇒ Object



79
80
81
82
# File 'lib/badpixxel-jekyll-flickr/photo.rb', line 79

def cache_exists(photoset, flickr_photo)
    # Check if Photo is Already in Cache
    return File.exists?(File.join(photoset.cache_dir, "#{flickr_photo.id}.yml"))
end

#cache_load(file) ⇒ Object



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

def cache_load(file)
    cached = YAML::load(File.read(file))
    self.id = cached['id']
    self.title = cached['title']
    self.slug = cached['slug']
    self.date = cached['date']
    self.description = cached['description']
    self.tags = cached['tags']
    self.url_full = cached['url_full']
    self.url_thumb = cached['url_thumb']
    self.cache_file = cached['cache_file']
    self.position = cached['position']
    self.date_update = cached['date_update']
    self.date_update_str = cached['date_update_str']
    self.from_cache = true
end

#cache_outdated(photoset, flickr_photo) ⇒ Object



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

def cache_outdated(photoset, flickr_photo)
    cache_file = File.join(photoset.cache_dir, "#{flickr_photo.id}.yml")
    # Check if Photo is Already in Cache
    if !File.exists?(cache_file)
        return true
    end
    # Load Cached Values
    cached = YAML::load(File.read(cache_file))
    if !cached["date_update"] or (cached["date_update"] != flickr_photo.lastupdate)
        return true
    end

    return false
end

#cache_storeObject



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

def cache_store
    cached = Hash.new
    cached['id'] = self.id
    cached['title'] = self.title
    cached['slug'] = self.slug
    cached['date'] = self.date
    cached['description'] = self.description
    cached['tags'] = self.tags
    cached['url_full'] = self.url_full
    cached['url_thumb'] = self.url_thumb
    cached['cache_file'] = self.cache_file
    cached['position'] = self.position
    cached['date_update'] = self.date_update
    cached['date_update_str'] = self.date_update_str
    
    File.open(self.cache_file, 'w') {|f| f.print(YAML::dump(cached))}
end

#flickr_load(site, photoset, flickr_photo, pos) ⇒ Object



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
# File 'lib/badpixxel-jekyll-flickr/photo.rb', line 29

def flickr_load(site, photoset, flickr_photo, pos)

    # Check if Photo is Alreday in cache & not outdated
    if !self.cache_outdated(photoset, flickr_photo)
        photo = File.join(photoset.cache_dir, "#{flickr_photo.id}.yml")
        self.cache_load(photo)
        return
    end

    # init
    self.id = flickr_photo.id
    self.title = flickr_photo.title
    self.slug = self.title.downcase.gsub(/ /, '-').gsub(/[^a-z\-]/, '') + '-' + self.id
    self.date = ''
    self.description = ''
    self.tags = Array.new
    self.url_full = ''
    self.url_thumb = ''
    self.cache_file = File.join(photoset.cache_dir, "#{self.id}.yml")
    self.position = pos
    self.date_update = flickr_photo.lastupdate
    self.date_update_str = DateTime.strptime(flickr_photo.lastupdate, '%s').to_s        
    
    # sizes request
    flickr_sizes = flickr.photos.getSizes(:photo_id => self.id)
    if flickr_sizes
        size_full = flickr_sizes.find {|s| s.label == site.config['flickr']['size_full']}
        if size_full
            self.url_full = size_full.source
        end
        
        size_thumb = flickr_sizes.find {|s| s.label == site.config['flickr']['size_thumb']}
        if size_thumb
            self.url_thumb = size_thumb.source
        end
    end
    
    # other info request
    flickr_info = flickr.photos.getInfo(:photo_id => self.id)
    if flickr_info
        self.date = DateTime.strptime(flickr_info.dates.posted, '%s').to_s
        self.description = flickr_info.description
        flickr_info.tags.each do |tag|
            self.tags << tag.raw
        end
    end
    
    cache_store
end

#gen_full_htmlObject



171
172
173
174
175
176
177
178
179
180
# File 'lib/badpixxel-jekyll-flickr/photo.rb', line 171

def gen_full_html
    content = ''
    if self.url_full and self.url_thumb
        content = "<p><a href=\"#{self.url_full}\" data-lightbox=\"photoset\"><img src=\"#{self.url_full}\" alt=\"#{self.title}\" title=\"#{self.title}\" class=\"photo full\" /></a></p>\n<p>#{self.description}</p>\n"
        if self.tags
            content += "<p>Tagged <i>" + self.tags.join(", ") + ".</i></p>\n"
        end
    end
    return content
end

#gen_thumb_htmlObject



155
156
157
158
159
160
161
# File 'lib/badpixxel-jekyll-flickr/photo.rb', line 155

def gen_thumb_html
    content = ''
    if self.url_full and self.url_thumb
        content = "<a href=\"#{self.url_full}\" data-lightbox=\"photoset\"><img src=\"#{self.url_thumb}\" alt=\"#{self.title}\" title=\"#{self.title}\" class=\"photo thumbnail\" width=\"75\" height=\"75\" /></a>\n"
    end
    return content
end

#gen_thumb_html_fancyObject



163
164
165
166
167
168
169
# File 'lib/badpixxel-jekyll-flickr/photo.rb', line 163

def gen_thumb_html_fancy
    content = ''
    if self.url_full and self.url_thumb
        content = "<a href=\"#{self.url_full}\" data-fancybox=\"photoset\"><img src=\"#{self.url_thumb}\" alt=\"#{self.title}\" title=\"#{self.title}\" class=\"img-fluid thumbnail\" /></a>\n"
    end
    return content
end

#to_liquidObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/badpixxel-jekyll-flickr/photo.rb', line 134

def to_liquid

    tags_hash = []
    self.tags.each  do |tag|
        tags_hash += [tag.hash]
    end

    return [
        'id' => self.id,
        'title' => self.title,
        'date' => self.date,
        'decription' => self.description,
        'tags' => self.tags,
        'tags_hash' => tags_hash,
        'url_full' =>  self.url_full,
        'url_thumb' =>  self.url_thumb,
        'position' =>  self.position,
        'date_update' =>  self.date_update,
    ]
end