Class: Gravylicious

Inherits:
Object
  • Object
show all
Defined in:
lib/gravylicious.rb

Constant Summary collapse

@@common_filters =

A hash to store commonly used filters like sanitize_url for escaping urls.

{'sanitize_url' => proc{|link| URI.escape(link, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))},
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(email, use_https = false) ⇒ Gravylicious

The constructor takes the email address and an optional boolean parameter which specifies whether or not to use https requests, by deafault it’s set to fasle. It stores the email address in @email and sets up a collection of default filters in @param_filters.



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/gravylicious.rb', line 20

def initialize(email, use_https = false)
  @email = email.strip
  @use_https = use_https
  
  @params = Hash.new
  
  @param_filters = {'d' => @@common_filters['sanitize_url'],
                    's' => proc{|s| raise("Invalid avatar size: The requested size is #{s}, valid range 1 to 512") if (s < 1 || s > 512); s},
                    'r' => proc{|r| raise("Invalid avatar rating: Select from g, pg, r and x") unless r =~ /^(p?)g$|^r$|^x$/; r}
                   }
end

Instance Attribute Details

#emailObject

Returns the value of attribute email.



9
10
11
# File 'lib/gravylicious.rb', line 9

def email
  @email
end

#param_filtersObject

Returns the value of attribute param_filters.



9
10
11
# File 'lib/gravylicious.rb', line 9

def param_filters
  @param_filters
end

#paramsObject

Returns the value of attribute params.



9
10
11
# File 'lib/gravylicious.rb', line 9

def params
  @params
end

#use_httpsObject

Returns the value of attribute use_https.



9
10
11
# File 'lib/gravylicious.rb', line 9

def use_https
  @use_https
end

Class Method Details

.common_filtersObject

Class function that returns the common_filters hash.



33
34
35
# File 'lib/gravylicious.rb', line 33

def self.common_filters
  @@common_filters
end

Instance Method Details

#avatar_defaultObject

Returns the link to the default avatar.



82
83
84
# File 'lib/gravylicious.rb', line 82

def avatar_default
  @params['d']
end

#avatar_default=(d) ⇒ Object

Sets the default avatar. Example gravatar.avatar_default = “



99
100
101
# File 'lib/gravylicious.rb', line 99

def avatar_default=(d)
  @params['d'] = d
end

#avatar_force_loadObject

Used for loading the fallback(default) avatar by defaut.



65
66
67
68
# File 'lib/gravylicious.rb', line 65

def avatar_force_load
  @params['f'] = 'y'
  self
end

#avatar_ratingObject

Returns the avatar rating.



77
78
79
# File 'lib/gravylicious.rb', line 77

def avatar_rating
  @params['r']
end

#avatar_rating=(r) ⇒ Object

Sets the rating of the avatar. Example gravatar.avatar_rating = ‘pg’ # Choose from g, pg, r and x.



93
94
95
# File 'lib/gravylicious.rb', line 93

def avatar_rating=(r)
  @params['r'] = r
end

#avatar_sizeObject

Returns the size of the avatar.



87
88
89
# File 'lib/gravylicious.rb', line 87

def avatar_size
  @params['s']
end

#avatar_size=(s) ⇒ Object

Sets the size of the avatar in px. Example gravatar.avatar_size = 128 # Should not exceed 512



105
106
107
# File 'lib/gravylicious.rb', line 105

def avatar_size=(s)
  @params['s'] = s
end

#avatar_unforce_loadObject

Unsets the force_load options.



71
72
73
74
# File 'lib/gravylicious.rb', line 71

def avatar_unforce_load
  @params.delete('f') if @params.has_key?('f')
  self
end

#avatar_urlObject

Returns the link to the desired Gravatar.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/gravylicious.rb', line 43

def avatar_url
  g_url = "http#{'s' if @use_https}://#{'secure.' if @use_https}gravatar.com/avatar/#{email_hash}"
  
  if params.size > 0    # If the list of parameters is not empty then append
    g_url += "?"
    
    @params.each do |param|
      # If the value of the parameter is not nil then either apply filter or append the orginal value (ie. if it has no filter).
      
      if param[1]
        g_url += param[0] + "=" + ((@param_filters[param[0]].call(param[1]) if @param_filters[param[0]]) || param[1]).to_s + "&"
      end
    
    end
    
    g_url = g_url[0...-1]
  end
  
  g_url
end

#email_hashObject

Returns the MD5 hash of the email address.



38
39
40
# File 'lib/gravylicious.rb', line 38

def email_hash
  Digest::MD5.hexdigest(@email.downcase)
end