class Ruboty::Lgtm::Client

Constants

DEFAULT_ENDPOINT
GOOGLE_IMAGE_API_URL

Public Class Methods

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

Public Instance Methods

generate() click to toggle source
# File lib/ruboty/lgtm/client.rb, line 15
def generate
  url = get

  if url
    File.join(endpoint, url)
  end
end

Private Instance Methods

connection() click to toggle source
# File lib/ruboty/lgtm/client.rb, line 74
def connection
  Faraday.new do |connection|
    connection.adapter :net_http
    connection.response :json
  end
end
default_params() click to toggle source
# File lib/ruboty/lgtm/client.rb, line 63
def default_params
  {
    rsz: 8,
    safe: 'active',
    v: '1.0',
    as_filetype: 'gif',
    imgsz: 'large',
    as_sitesearch: 'tumblr.com'
  }
end
endpoint() click to toggle source
# File lib/ruboty/lgtm/client.rb, line 81
def endpoint
  @options[:endpoint] || DEFAULT_ENDPOINT
end
get() click to toggle source
# File lib/ruboty/lgtm/client.rb, line 25
def get
  resource['unescapedUrl'] if resource
rescue => exception
  Ruboty.logger.error("Error: #{self}##{__method__} - #{exception}")
  nil
end
given_params() click to toggle source
# File lib/ruboty/lgtm/client.rb, line 57
def given_params
  {
    q: @options[:query],
  }
end
params() click to toggle source
# File lib/ruboty/lgtm/client.rb, line 53
def params
  default_params.merge(given_params).reject {|key, value| value.nil? }
end
resource() click to toggle source
# File lib/ruboty/lgtm/client.rb, line 32
def resource
  return @resource if defined?(@resource)

  @resource = nil
  if data = response.body['responseData']
    if results = data['results']
      @resource = results.sample
    end
  end

  @resource
end
response() click to toggle source
# File lib/ruboty/lgtm/client.rb, line 45
def response
  connection.get(url, params)
end
url() click to toggle source
# File lib/ruboty/lgtm/client.rb, line 49
def url
  GOOGLE_IMAGE_API_URL
end