Class: FlickrDownloader::Generator

Inherits:
Jekyll::Generator
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/flickr_downloader.rb

Constant Summary collapse

LOG_TOPIC =
'Flickr Downloader:'
JEKYLL_ROOT =
T.let(Jekyll.configuration.source({}), String)
MANIFEST_FILE_PATH =
T.let("#{JEKYLL_ROOT}/_data/flickr_photosets.yaml", String)
ConfigType =
T.type_alias { T::Hash[String, T.untyped] }
ManifestType =
T.type_alias { T::Hash[String, T::Hash[String, String]] }
DEFAULTS =
T.let(
  {
    'enabled' => false,
    'image_root' => 'assets/images/flickr_photosets',
    'force_refresh' => false,
    'image_size' => 'url_k'
  }.freeze,
  ConfigType
)

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Generator

Returns a new instance of Generator.



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/flickr_downloader.rb', line 34

def initialize(config = {})
  super

  @config = T.let(DEFAULTS.merge(config.fetch('flickr_downloader', {})), ConfigType)

  @flickr = T.let(nil, T.nilable(Flickr))

  return unless enabled?

  @flickr = Flickr.new(@config['api_key'], @config['shared_secret'])
  @flickr.access_token = @config['access_token']
  @flickr.access_secret = @config['access_secret']
end

Instance Method Details

#enabled?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/flickr_downloader.rb', line 49

def enabled?
  @config['enabled']
end

#generate(site) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/flickr_downloader.rb', line 54

def generate(site)
  unless enabled?
    Jekyll.logger.warn(LOG_TOPIC, 'Plugin disabled via config!')
    return
  end

  posts = site.posts.docs.select do |post|
    !post.data.fetch('disable_flickr_download', false) && post.data.fetch('flickr_photoset_id', nil)
  end

  FileUtils.touch(MANIFEST_FILE_PATH) if posts && !File.exist?(MANIFEST_FILE_PATH)

  posts.each do |post|
    flickr_photoset_id = post.data['flickr_photoset_id']

    if flickr_photoset_id.is_a?(Array)
      flickr_photoset_id.each { |photoset_id| download_photoset(photoset_id.to_s, post) }
    elsif flickr_photoset_id
      download_photoset(flickr_photoset_id.to_s, post)
    end
  end
end