Module: ETFC::Runner

Defined in:
lib/etfc/runner.rb

Class Method Summary collapse

Class Method Details

.check_api_credentialsObject



58
59
60
61
62
63
# File 'lib/etfc/runner.rb', line 58

def check_api_credentials
  return if ETFC::FLICKR_API_KEY || ETFC::FLICKR_SHARED_SECRET
  puts 'Please set FLICKR_API_KEY and FLICKR_SHARED_SECRET as ' \
       'environment variables'
  exit 1
end

.run(keywords, output) ⇒ Object

Public: create a collage of images found on flickr for a list of keywords

keywords - array of keywords (up to 10) output - name of the resulting collage

Example:

run(['banana', 'monkey'], 'collage.jpg')
# the collage will be saved to collage.jpg in your current directory

Returns Magick::ImageList; can be discarded as the collage will be saved to the supplied file name on disk



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/etfc/runner.rb', line 17

def run(keywords, output)
  check_api_credentials

  images_urls = ten_img_urls(keywords)

  images_urls.each_with_index do |url, idx|
    Image.download(url, "#{idx}.jpg")
  end

  img_list = Dir[TMP_DIR + '/**.jpg']

  img_list.each { |img| Image.crop(img) }

  Collage.create(img_list, output)
end

.ten_img_urls(keywords) ⇒ Object

Public: get ten image urls for keywords; if less than 10 keywords are supplied, random keywords will be selected from the system dictionary

keywords - array of keywords (up to 10)

Example:

ten_img_urls(['banana'])
#=> ['http://example.com/123.jpog', '...]

Returns an array of ten image URLs



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/etfc/runner.rb', line 44

def ten_img_urls(keywords)
  images = keywords.map do |keyword|
    Flickr.search(keyword)
  end.reject(&:nil?)

  dict = Dictionary.new
  while images.length < 10
    result = Flickr.search(dict.random)
    images << result unless result.nil?
  end

  images.take(10)
end