Sha256: 679f4168088bbe50a705ba9d3353e16b0b855061b2986e5fd67a152438c5c579

Contents?: true

Size: 1.27 KB

Versions: 2

Compression:

Stored size: 1.27 KB

Contents

require "faraday"
require "faraday_middleware"

module Ellen
  module GoogleImage
    class Client
      GOOGLE_IMAGE_API_URL = "http://ajax.googleapis.com/ajax/services/search/images"

      attr_reader :options

      def initialize(options)
        @options = options
      end

      def get
        resource["unescapedUrl"] if resource
      rescue => exception
        Ellen.logger.error("Error: #{self}##{__method__} - #{exception}")
        nil
      end

      private

      def resource
        @resource ||= begin
          if data = response.body["responseData"]
            if results = data["results"]
              results.sample
            end
          end
        end
      end

      def response
        connection.get(url, params)
      end

      def url
        GOOGLE_IMAGE_API_URL
      end

      def params
        default_params.merge(given_params).reject {|key, value| value.nil? }
      end

      def given_params
        {
          q: options[:query],
        }
      end

      def default_params
        {
          rsz: 8,
          safe: "active",
          v: "1.0",
        }
      end

      def connection
        Faraday.new do |connection|
          connection.adapter :net_http
          connection.response :json
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ellen-google_image-0.0.6 lib/ellen/google_image/client.rb
ellen-google_image-0.0.5 lib/ellen/google_image/client.rb