class Gravaty

This class is a simple APi to retrieve an URL with specified options from Gravatar site.

The only needed parameter to create a Gravaty object is the reference email address.

Constants

DEFAULT_OPTIONS

The possible built in options to retrieve special avatars sizes range for the avatar image size.

Attributes

digest[R]
email[R]

Public Class Methods

new(email) click to toggle source

::new -> new_gravaty

Creates a new Gravaty described by the user's email.

Usage:

a_gravaty = Gravaty.new user@example.com

parameter email [String] is the email address to retrieve Gravatar data for.

# File lib/name/bresciani/marco/gravaty/gravaty.rb, line 38
def initialize(email)
  @email = email
  @digest = Digest::MD5.hexdigest @email.downcase
  R18n.from_env(Pathname(__FILE__).dirname.expand_path.to_s + '/i18n/')
end

Public Instance Methods

base_url(secure = false) click to toggle source
# File lib/name/bresciani/marco/gravaty/gravaty.rb, line 101
def base_url(secure = false)
  suffix = '.gravatar.com/avatar/'

  return 'http://www' + suffix if not secure
  'https://secure' + suffix
end
extension(type = nil) click to toggle source
# File lib/name/bresciani/marco/gravaty/gravaty.rb, line 90
def extension(type = nil)
  extension = ''
  case type.to_s.downcase
    when 'jpg', 'jpeg', 'png', 'gif'
      extension = '.' + type.downcase
    else
      raise ArgumentError, t.error.type(type.to_s)
  end if not type.nil?
  extension
end
force_default(force = false) click to toggle source
# File lib/name/bresciani/marco/gravaty/gravaty.rb, line 127
def force_default(force = false)
  return 'f=y' if force
  ''
end
image_param_request(type = nil, pixel_size = nil, force = false, secure = true) click to toggle source

a_gravaty.image_request -> String a_gravaty.image_request nil, nil, nil, false -> String a_gravaty.image_request nil, 640 -> String a_gravaty.image_request 'png' -> String a_gravaty.image_request 'jpg', nil, true -> String a_gravaty.image_request 'png', 42, true -> String

See #image_request but parameters shall be passed one by one. Can be used when the list is well know and fixed.

parameter type [String] is the possibly desired specific image format. Valid formats are JP(E)G, PNG or GIF. parameter pixel_size [Integer] it the size in pixel of the image. From 1 to 2048. parameter force [TrueClass || FalseClass] is a boolean to specify if the default has to be forced when no image is found. param secure [TrueClass || FalseClass] is a boolean to specify whether the resulting URL shall be HTTPS or HTTP type.

return [String] The Gravatar site URL with specified options and configuration parameters.

# File lib/name/bresciani/marco/gravaty/gravaty.rb, line 84
def image_param_request(type = nil, pixel_size = nil, force = false, secure = true)
  image_url = @digest + extension(type)
  full_url = base_url(secure) + image_url
  full_url + parameters(pixel_size, force)
end
image_request(args = {}) click to toggle source

a_gravaty.image_request -> String a_gravaty.image_request { pixel_size: 42 } -> String a_gravaty.image_request { pixel_size: 42, type: 'png' } -> String a_gravaty.image_request { secure: false } -> String a_gravaty.image_request { type: 'jpg', force: true } -> String a_gravaty.image_request { pixel_size: 42, secure: true, type: 'png' } -> String

The main method to ask for a specific Gravatar URL.

parameter type [String] is the possibly desired specific image format. Valid formats are JP(E)G, PNG or GIF. parameter pixel_size [Integer] it the size in pixel of the image. From 1 to 2048. parameter force [TrueClass || FalseClass] is a boolean to specify if the default has to be forced when no image is found. param secure [TrueClass || FalseClass] is a boolean to specify whether the resulting URL shall be HTTPS or HTTP type.

return [String] The Gravatar site URL with specified options and configuration parameters.

# File lib/name/bresciani/marco/gravaty/gravaty.rb, line 60
def image_request(args = {})
  raise ArgumentError, t.error.hash if args.nil?
  type = args[:type] || nil
  pixel_size = args[:pixel_size] || nil
  force = args[:force]
  secure = (args[:secure].nil?() ? true : args[:secure])
  image_param_request type, pixel_size, force, secure
end
parameters(pixel_size = nil, force = false) click to toggle source
# File lib/name/bresciani/marco/gravaty/gravaty.rb, line 108
def parameters(pixel_size = nil, force = false)
  parameters = ''
  parameters += size(pixel_size)

  force_param = force_default force
  unless force_param.empty?
    parameters += '&' + force_param
  end

  return ('?' + parameters) if not parameters.empty?
  parameters
end
size(pixel_size = nil) click to toggle source
# File lib/name/bresciani/marco/gravaty/gravaty.rb, line 121
def size(pixel_size = nil)
  return '' if pixel_size.nil?
  raise ArgumentError, t.error.value(pixel_size) if not (1...2048).include? pixel_size.to_i
  's=' + pixel_size.to_s if not pixel_size.nil?
end