class Ruboty::TumblrImage::Client

Constants

GOOGLE_IMAGE_API_URL

Attributes

options[R]

Public Class Methods

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

Public Instance Methods

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

Private Instance Methods

connection() click to toggle source
# File lib/ruboty/tumblr_image/client.rb, line 60
def connection
  Faraday.new do |connection|
    connection.adapter :net_http
    connection.response :json
  end
end
default_params() click to toggle source
# File lib/ruboty/tumblr_image/client.rb, line 53
def default_params
  {
    rsz: 8,
    v: "1.0",
  }
end
given_params() click to toggle source
# File lib/ruboty/tumblr_image/client.rb, line 46
def given_params
  {
    q: "site:#{options[:username]}.tumblr.com",
    safe: options[:unsafe] ? "off" : "active",
  }
end
params() click to toggle source
# File lib/ruboty/tumblr_image/client.rb, line 42
def params
  default_params.merge(given_params).reject {|key, value| value.nil? }
end
resource() click to toggle source
# File lib/ruboty/tumblr_image/client.rb, line 24
def resource
  @resource ||= begin
    if data = response.body["responseData"]
      if results = data["results"]
        results.sample
      end
    end
  end
end
response() click to toggle source
# File lib/ruboty/tumblr_image/client.rb, line 34
def response
  connection.get(url, params)
end
url() click to toggle source
# File lib/ruboty/tumblr_image/client.rb, line 38
def url
  GOOGLE_IMAGE_API_URL
end