Sha256: 0acdae3f23c2fd757648a32598a8dfc3f7d041c8a1b355e8b5aba74f31cbb47e

Contents?: true

Size: 755 Bytes

Versions: 1

Compression:

Stored size: 755 Bytes

Contents

# -*- encoding: utf-8 -*-

require 'google_image_api'
require 'open-uri'

module Rack
  class GoogleImage
    def initialize(app, options = {})
      @app = app
    end

    def call(env)
      status, headers, response = @app.call(env)

      if env['PATH_INFO'] =~ (/\/(.*)\.jpg$/)
        response = search_image(response, Rack::Utils::unescape($1))
        [status, headers, response]
      end

      [status, headers, response]
    end

    private
    def search_image(response, image_name)
      results = GoogleImageApi.find(image_name, {
        imgsz:       "large",
        rsz:         8,
        start:       8,
        as_filetype: "jpg"
      })
      url = results.images.sample['url']
      [ open(url) {|f| f.read } ]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rack-middleware-google-image-0.0.3 lib/rack-middleware-google-image.rb