class Flickr

Constants

IMAGE_HEIGHT
IMAGE_WIDTH
NO_IMAGES
PROGRESSBAR

Public Class Methods

create_collage(file_name) click to toggle source
# File lib/flickr.rb, line 45
def self.create_collage(file_name)
        bg_image = read_bg_image
        result = loop_images(bg_image)
        
        write_image_to_file(result, file_name)
end
new(keyword, index) click to toggle source
# File lib/flickr.rb, line 9
def initialize(keyword, index)
        call_progress_bar
        @options = 
                { query: 
                        {
                                method: 'flickr.photos.search',
                                text: keyword,
                                sort: 'interestingness-asc',
                                nojsoncallback: 1,
                                api_key: '38f8b5be4529ce3f039a5d9b94f360cd',
                                per_page: 1
                        }
                }
        @index = index
        @url = nil
end

Private Class Methods

loop_images(result) click to toggle source
# File lib/flickr.rb, line 59
def self.loop_images(result)
        @i,@y=0,0
        (0..1).each do |j|
                @x = -200
                5.times{
                        img = MiniMagick::Image.new("tmp/#{@i}.jpg")
                        result = make_composite_image(result, img)
                }
                @y += 300
        end
        result
end
make_composite_image(result, img) click to toggle source
# File lib/flickr.rb, line 72
def self.make_composite_image(result, img)
        result = result.composite(img) do |c|
                c.compose "Over"
                @x += 200
                @i += 1
                c.geometry "+#{@x}+#{@y}"
        end
end
read_bg_image() click to toggle source
# File lib/flickr.rb, line 91
def self.read_bg_image
        File.open("tmp/bg.jpg", "wb") do |f| 
                f.write HTTParty.get("https://s30.postimg.org/l9fwb3to1/image.jpg").parsed_response
        end
        MiniMagick::Image.new("tmp/bg.jpg")
end
write_image_to_file(result, file_name) click to toggle source
# File lib/flickr.rb, line 54
def self.write_image_to_file(result, file_name)
        Dir.mkdir 'images' unless File.exists?('images')
        result.write "images/#{file_name}.jpg"
end

Public Instance Methods

fetch() click to toggle source
# File lib/flickr.rb, line 36
def fetch
        return false unless @url
        File.open("tmp/#{@index}.jpg", "wb") do |f| 
                image = f.write HTTParty.get(@url).parsed_response
        end
        image = Magick::ImageList.new("tmp/#{@index}.jpg").resize_to_fill!(IMAGE_WIDTH, IMAGE_HEIGHT)
        image.write("tmp/#{@index}.jpg")
end

Private Instance Methods

call_progress_bar() click to toggle source
# File lib/flickr.rb, line 87
def call_progress_bar
        5.times{PROGRESSBAR.increment; sleep 0.01}
end
set_image_url(response) click to toggle source
# File lib/flickr.rb, line 81
def set_image_url(response)
        photo = response["rsp"]["photos"]["photo"]
        return false unless photo
        @url = "http://farm#{photo['farm']}.staticflickr.com/#{photo['server']}/#{photo['id']}_#{photo['secret']}.jpg"
end