class Ruboty::GoogleImage::Client

Constants

GOOGLE_IMAGE_API_URL

Attributes

options[R]

Public Class Methods

new(options) click to toggle source
# File lib/ruboty/google_image/client.rb, line 11
def initialize(options)
  @options = options
end

Public Instance Methods

get() click to toggle source
# File lib/ruboty/google_image/client.rb, line 15
def get
  resource["link"] if resource
rescue => exception
  Ruboty.logger.error("Error: #{self}##{__method__} - #{exception}")
  nil
end

Private Instance Methods

connection() click to toggle source
# File lib/ruboty/google_image/client.rb, line 60
def connection
  Faraday.new do |connection|
    connection.response :json
    connection.adapter :net_http
  end
end
default_params() click to toggle source
# File lib/ruboty/google_image/client.rb, line 50
def default_params
  {
    searchType: 'image',
    safe: 'high',
    fields: 'items(link)',
    cx: ENV['GOOGLE_CSE_ID'],
    key: ENV['GOOGLE_CSE_KEY']
  }
end
given_params() click to toggle source
# File lib/ruboty/google_image/client.rb, line 44
def given_params
  {
    q: options[:query],
  }
end
params() click to toggle source
# File lib/ruboty/google_image/client.rb, line 40
def params
  default_params.merge(given_params).reject {|key, value| value.nil? }
end
resource() click to toggle source
# File lib/ruboty/google_image/client.rb, line 24
def resource
  @resource ||= begin
    if items = response.body["items"]
      items.sample
    end
  end
end
response() click to toggle source
# File lib/ruboty/google_image/client.rb, line 32
def response
  connection.get(url, params)
end
url() click to toggle source
# File lib/ruboty/google_image/client.rb, line 36
def url
  GOOGLE_IMAGE_API_URL
end