class Gravaty::Gravaty

This class is a simple API to retrieve an URL with specified options from Gravatar site. It can be used to read data for avatars, profiles or for XML-RPC APi calls. The only needed parameter to create a Gravaty object is the reference email address.

Author

Marco Bresciani

Copyright

Copyright © 2013, 2014, 2015, 2016, 2017, 2018,

2019 Marco Bresciani

License

GNU General Public License version 3

Attributes

digest[R]

Provides the MD5 signature (of the small caps version) of the email address used to build the object.

email[R]

Provides the (small caps version of) email address used to build the object.

Public Class Methods

new(email_address, parser) click to toggle source

Creates a Gravaty object described by the user's email. Throws an ArgumentError exception if the supplied email address is nil or not valid according to RFC5322.

Usage

new_gravaty = Gravaty.new email, parser

Params
  • email_address, the user's email address (a syntactically

valid one).

- +parser+, a parser duck-responding to the +parse+ method with

two parameters, method name and value string

Returns

a Gravaty object for the specified email address.

Raises

ArgumentError, if the supplied email address is nil

or not valid according to RFC 5322.

   # File lib/gravaty/application.rb
65 def initialize(email_address, parser)
66   I18n.load_path = Dir[File.join(File.dirname(__FILE__),
67                                  '/locales/', '*.yml')]
68 
69   raise ArgumentError, I18n.t('error.nil') if email_address.nil?
70   raise ArgumentError, I18n.t('error.invalid', value: email_address) unless Utils::Rfc5322::EMAIL.match email_address # thanks Peter!
71 
72   @email = email_address.strip.downcase
73   @digest = Digest::MD5.hexdigest email
74   @gravaty = email
75   @parser = parser
76 end

Public Instance Methods

<=>(other_gravaty) click to toggle source
    # File lib/gravaty/application.rb
326 def <=>(other_gravaty)
327   @email <=> other_gravaty.email
328 end
avatar(args = {}) click to toggle source

Returns a string containing the URI of the gravatar associated to internal (provided) email address. Valid keys for the args hash are: :type, :pixel_size, :force, :secure, :rating, :default.

Usage
  • a_string = new_gravaty.avatar

  • a_string = new_gravaty.avatar

  • a_string = new_gravaty.avatar pixel_size: 42

  • <tt>a_string = new_gravaty.avatar pixel_size: 42, type:

png'</tt>

- <tt>a_string = new_gravaty.avatar secure: false</tt>
- <tt>a_string = new_gravaty.avatar type: 'jpg', force:

true</tt>

- <tt>a_string = new_gravaty.avatar secure: true, pixel_size:

42, type: 'png'</tt>

- <tt>a_string = new_gravaty.avatar rating: pg, type: 'gif'</tt>
- <tt>a_string = new_gravaty.avatar default: monsterid,

pixel_size: 42</tt>

Params
  • type: [String] is the possibly desired specific image

format. Valid formats are jp(e)g, png or gif. Default to no extension. Will be downcased. Raises ArgumentError for invalid formats.

- +pixel_size+: [Integer] is the size in pixel of the image.

From 1 to 2048. Default to no value. Raises ArgumentError for invalid sizes.

- +force+: [TrueClass || FalseClass] is a boolean to specify if

the default has to be forced when no image is found. Default to false.

- +secure+: [TrueClass || FalseClass] is a boolean to specify

whether the resulting URL shall be HTTPS or HTTP type. Default to true (HTTPS).

- +rating+: [String] is the rating type of the image. Valid

values are 'g', 'pg', 'r' and 'x'.

- +default+: [String] is the default type of the image (valid

values are '404', 'mm', 'identicon', 'monsterid', 'wavatar', 'retro' and 'blank'), or the URI for an own default image. For the URI validation see: it.gravatar.com/site/implement/images/#default-image .

Returns

a String containing the Gravatar site URI with

specified options and configuration parameters.

    # File lib/gravaty/application.rb
119 def avatar(args = {})
120   secure = true
121   type = nil
122 
123   array = []
124   unless args.nil?
125     type = args.fetch(:type, nil)
126     secure = args.fetch(:secure, secure)
127 
128     [:pixelsize, :force, :default, :rating].each do |param|
129       array << @parser.parse(param.to_s, args[param]) unless args[param].nil?
130     end
131   end
132 
133   build_uri(secure, true) + digest +
134       @parser.parse('type', type) + build_query_string(array)
135 end
avatar!(args = {}) click to toggle source

See avatar method. This banged version saves the resulting string as internal state.

    # File lib/gravaty/application.rb
139 def avatar!(args = {})
140   @gravaty = avatar(args)
141 end
download(filename = nil) click to toggle source

Saves a file, with specified filename, that contains the current gravaty configuration. Uses the internal state to retrieve data from the URI. Defaults to 'gravaty' with no specific extension.

Usage
  • a_string = new_gravaty.download filename

Params
  • filename: [String] is the filename to be saved.

    # File lib/gravaty/application.rb
196 def download(filename = nil)
197   filename = URI.parse(@gravaty).path.rpartition('/')
198                  .last if filename.nil?
199   Utils::Downloader::Downloader
200       .download_file @gravaty, filename # thanks Jon!
201 end
json(args = {}) click to toggle source

See profile method. Customized version for JSON-specific requests.

Usage
  • a_string = new_gravaty.json

  • a_string = new_gravaty.json callback: 'alert'

  • <tt>a_string = new_gravaty.qrcode secure: false, callback:

'alert'</tt>

Params
  • secure: [TrueClass || FalseClass] is a boolean to specify

whether the resulting URL shall be HTTPS or HTTP type. Default to true (HTTPS).

- +callback+: [String] See

secure.gravatar.com/site/implement/profiles/json/#request-options. No check on parameter meaning or validity, except for nil.

Returns

a String containing the Gravatar site URI with

specified options and configuration parameters, in JSON format.

    # File lib/gravaty/application.rb
254 def json(args = {})
255   secure = true
256   callback = nil
257 
258   unless args.nil?
259     callback = args.fetch(:callback, nil)
260     secure = args.fetch(:secure, secure)
261   end
262 
263   profile format: 'json', secure: secure, callback: callback
264 end
json!(args = {}) click to toggle source

See json method. This banged version saves the resulting string as internal state.

    # File lib/gravaty/application.rb
268 def json!(args = {})
269   @gravaty = json(args)
270 end
profile(args = {}) click to toggle source

Returns a string containing the URI of the gravatar profile associated to internal (provided) email address. Valid keys for the args hash are: :secure, :format.

Usage
  • a_string = new_gravaty.profile

  • a_string = new_gravaty.profile secure: false

  • <tt>a_string = new_gravaty.profile format: 'php', secure:

true</tt>

Params
  • format: [String] is the possibly desired specific profile

format. Valid formats are 'json', 'xml', 'php', 'vcf' and 'qr'. Default to no extension. Will be downcased. Defaults to no extension for invalid formats.

- +secure+: [TrueClass || FalseClass] is a boolean to specify

whether the resulting URL shall be HTTPS or HTTP type. Default to true (HTTPS).

Returns

a String containing the Gravatar site URI with

specified options and configuration parameters.

    # File lib/gravaty/application.rb
162 def profile(args = {})
163   secure = true
164   format = nil
165 
166   array = []
167   unless args.nil?
168     format = args[:format].downcase unless args[:format].nil?
169     secure = args.fetch(:secure, secure)
170 
171     unless (format.nil?) and (PROFILES.include? format)
172       selected = (format == 'json' ? 'callback' : 'pixelsize')
173 
174       array << @parser.parse(selected, args[selected.to_sym]) unless args[selected.to_sym].nil?
175     end
176   end
177 
178   build_uri(secure, false) + digest +
179       @parser.parse('format', format) + build_query_string(array)
180 end
profile!(args = {}) click to toggle source

See profile method. This banged version saves the resulting strin as internal state.

    # File lib/gravaty/application.rb
184 def profile!(args = {})
185   @gravaty = profile(args)
186 end
qrcode(args = {}) click to toggle source

See profile method. Customized version for QRCode-specific requests.

Usage
  • a_string = new_gravaty.qrcode

  • a_string = new_gravaty.qrcode pixel_size: 42

  • <tt>a_string = new_gravaty.qrcode secure: false, pixel_size:

42</tt>

Params
  • secure: [TrueClass || FalseClass] is a boolean to specify

whether the resulting URL shall be HTTPS or HTTP type. Default to true (HTTPS).

- +pixel_size+: [Integer] is the size in pixel of the image.

From 1 to 2048. Default to no value. Raises ArgumentError for invalid sizes.

Returns

a String containing the Gravatar site URI with

specified options and configuration parameters, in QRCode format.

    # File lib/gravaty/application.rb
220 def qrcode(args = {})
221   secure = true
222   size = nil
223 
224   unless args.nil?
225     size = args.fetch(:pixelsize, nil)
226     secure = args.fetch(:secure, secure)
227   end
228 
229   profile format: 'qr', secure: secure, pixelsize: size
230 end
qrcode!(args = {}) click to toggle source

See qrcode method. This banged version saves the resulting string as internal state.

    # File lib/gravaty/application.rb
234 def qrcode!(args = {})
235   @gravaty = qrcode(args)
236 end
reset() click to toggle source

Restores the original status cleaning up the (possibly) previously saved URIs restoring the default to_s.

    # File lib/gravaty/application.rb
274 def reset
275   @gravaty = email
276 end
to_json() click to toggle source

Returns a JSon object representing the Gravaty object.

Usage
  • a_json = new_gravaty.to_json

Returns

a JSON containing the Gravaty object representation

with currently saved options and configuration parameters.

    # File lib/gravaty/application.rb
308 def to_json
309   result = Hash.new
310   result[email] = digest
311 
312   result.to_json
313 end
to_s() click to toggle source

Returns a string representing the Gravaty object.

Usage
  • a_string = new_gravaty.to_s

Returns

a String containing the Gravaty object representation

with currently saved options and configuration parameters.

    # File lib/gravaty/application.rb
322 def to_s
323   @gravaty.to_s
324 end
xmlrpc(a_method = RPC_TEST_METHOD, password = nil, args = {}) click to toggle source

Interfaces with the Gravatar XML-RPC API.

Usage
  • <tt>response = new_gravaty.xmlrpc 'grav.test',

my_password</tt>

Params
  • a_method: [String] is a valid method supported by Gravatar

XML-RPC API. See en.gravatar.com/site/implement/xmlrpc/ .

- +password+: [String] is a valid password associated to user's

email_address specified to build the Gravaty object.

- +args+: See https://en.gravatar.com/site/implement/xmlrpc/ for

possible parameters, depending on called method.

Returns

See en.gravatar.com/site/implement/xmlrpc/ for

possible return values, depending on called method.

    # File lib/gravaty/application.rb
292 def xmlrpc(a_method = RPC_TEST_METHOD, password = nil, args = {})
293   raise ArgumentError, I18n.t('error.nil') if password.nil?
294   raise ArgumentError, I18n.t('error.invalid', value: a_method) unless RPC_METHODS.include? a_method
295 
296   @rpc_connector = Utils::RpcConnector::RpcConnector
297                        .new(digest, password) if @rpc_connector.nil?
298   @rpc_connector.call a_method, args unless @rpc_connector.nil?
299 end