Module: Wallzilla::Runner
Constant Summary collapse
- KEYWORDS_SIZE =
10- KW_LEN_RANGE =
4...10
- DEFAULT_FILENAME =
"output.jpg"- NO_WORDS_FILE_FOUND =
"Sorry, but no one words list file found!"- COPYRIGHT =
"© 2017 Yury Batenko. Done with fun"
Instance Method Summary collapse
-
#build(kw:, result:, key:, words: nil, tiles: "4x3", bg: "black") ⇒ Object
Entry point for external calling Wallzilla image generation.
- #clean(photos) ⇒ Object
- #dict_file ⇒ Object
- #fetch(kw) ⇒ Object
- #fill ⇒ Object
- #flickr_api_key ⇒ Object
- #keywords ⇒ Object
- #maybe_dictionaries ⇒ Object
- #maybe_show_progress(msg, value = nil) ⇒ Object
- #normalize(kw) ⇒ Object
- #output_file ⇒ Object
- #process_photos(kw) ⇒ Object
- #random_word ⇒ Object
- #run!(kw, progressbar: nil) ⇒ Object
Instance Method Details
#build(kw:, result:, key:, words: nil, tiles: "4x3", bg: "black") ⇒ Object
Entry point for external calling Wallzilla image generation
13 14 15 16 17 18 19 20 21 |
# File 'lib/wallzilla/runner.rb', line 13 def build(kw:, result:, key:, words: nil, tiles: "4x3", bg: "black") @_output_file = result @_key = key @wordlist = words @tiles = tiles @_fill = bg run!(kw) end |
#clean(photos) ⇒ Object
53 54 55 |
# File 'lib/wallzilla/runner.rb', line 53 def clean(photos) photos.map { |x| x.close; x.unlink } end |
#dict_file ⇒ Object
87 88 89 90 91 92 93 94 |
# File 'lib/wallzilla/runner.rb', line 87 def dict_file return @_dict if defined?(@_dict) @_dict = maybe_dictionaries. uniq. compact. select { |f| FileTest.readable?(f) }.first || fail(NO_WORDS_FILE_FOUND) end |
#fetch(kw) ⇒ Object
57 58 59 |
# File 'lib/wallzilla/runner.rb', line 57 def fetch(kw) Wallzilla::Fetcher.fetch(kw) end |
#fill ⇒ Object
65 66 67 68 69 70 |
# File 'lib/wallzilla/runner.rb', line 65 def fill return @_fill if defined?(@_fill) @_fill = ENV["background_color"] || "black" end |
#flickr_api_key ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/wallzilla/runner.rb', line 79 def flickr_api_key return @_key if defined?(@_key) @_key = ENV.fetch("FLICKR_KEY") do |e| fail("You should provide Flickr API application key! (--key key_string)") end end |
#keywords ⇒ Object
61 62 63 |
# File 'lib/wallzilla/runner.rb', line 61 def keywords @keywords end |
#maybe_dictionaries ⇒ Object
96 97 98 99 100 |
# File 'lib/wallzilla/runner.rb', line 96 def maybe_dictionaries Array(@wordlist) + Array(ENV["words_file"]) + ["/usr/share/dict/words", "/usr/share/words"] end |
#maybe_show_progress(msg, value = nil) ⇒ Object
112 113 114 115 |
# File 'lib/wallzilla/runner.rb', line 112 def maybe_show_progress(msg, value = nil) return unless @progressbar @progressbar.call(msg, value) end |
#normalize(kw) ⇒ Object
102 103 104 105 106 |
# File 'lib/wallzilla/runner.rb', line 102 def normalize(kw) Array.new(KEYWORDS_SIZE) do |i| kw[i] || random_word end end |
#output_file ⇒ Object
72 73 74 75 76 77 |
# File 'lib/wallzilla/runner.rb', line 72 def output_file return @_output_file if defined?(@_output_file) @_output_file = ENV["result_file"] || File.join(Dir.pwd, DEFAULT_FILENAME) end |
#process_photos(kw) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/wallzilla/runner.rb', line 41 def process_photos(kw) kw.map.with_index(1) do |word, idx| fetched_photo = loop do url = fetch(word) break url if url word = random_word end maybe_show_progress(:progress, idx) fetched_photo end end |
#random_word ⇒ Object
108 109 110 |
# File 'lib/wallzilla/runner.rb', line 108 def random_word Wallzilla::Words.random_word(KW_LEN_RANGE) end |
#run!(kw, progressbar: nil) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/wallzilla/runner.rb', line 23 def run!(kw, progressbar: nil) @progressbar = maybe_show_progress(:started) @keywords = normalize(kw) photos = process_photos(keywords) Wallzilla::Montage.process!(photos, output_file, fill) maybe_show_progress(:finished) rescue => e maybe_show_progress(:interrupted) raise e ensure clean(photos) if photos end |