gravylicious 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/lib/gravylicious.rb +24 -2
  2. data/lib/gravylicious.rb~ +24 -4
  3. metadata +6 -6
data/lib/gravylicious.rb CHANGED
@@ -1,34 +1,45 @@
1
+ # A ruby library for generating Gravatar urls.
2
+ # Author:: Utsav Gupta
3
+ # Copyright:: Copyright (c) 2011 Utsav Gupta
4
+ # License:: MIT License
1
5
  require 'digest/md5'
2
6
  require 'uri'
3
7
 
4
8
  class Gravylicious
5
9
  attr_accessor :email, :params, :param_filters, :use_https
6
10
 
7
- # A hash to store commonly used filters
11
+ # A hash to store commonly used filters
12
+ # like sanitize_url for escaping urls.
8
13
  @@common_filters = {'sanitize_url' => proc{|link| URI.escape(link, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))},
9
14
  }
10
15
 
16
+ # The constructor takes the email address and an optional boolean
17
+ # parameter which specifies whether or not to use https requests,
18
+ # by deafault it's set to fasle. It stores the email address in @email
19
+ # and sets up a collection of default filters in @param_filters.
11
20
  def initialize(email, use_https = false)
12
21
  @email = email.strip
13
22
  @use_https = use_https
14
23
 
15
24
  @params = Hash.new
16
25
 
17
- # Define the default filters
18
26
  @param_filters = {'d' => @@common_filters['sanitize_url'],
19
27
  's' => proc{|s| raise("Invalid avatar size: The requested size is #{s}, valid range 1 to 512") if (s < 1 || s > 512); s},
20
28
  'r' => proc{|r| raise("Invalid avatar rating: Select from g, pg, r and x") unless r =~ /^(p?)g$|^r$|^x$/; r}
21
29
  }
22
30
  end
23
31
 
32
+ # Class function that returns the common_filters hash.
24
33
  def self.common_filters
25
34
  @@common_filters
26
35
  end
27
36
 
37
+ # Returns the MD5 hash of the email address.
28
38
  def email_hash
29
39
  Digest::MD5.hexdigest(@email.downcase)
30
40
  end
31
41
 
42
+ # Returns the link to the desired Gravatar.
32
43
  def avatar_url
33
44
  g_url = "http#{'s' if @use_https}://#{'secure.' if @use_https}gravatar.com/avatar/#{email_hash}"
34
45
 
@@ -50,36 +61,47 @@ class Gravylicious
50
61
  g_url
51
62
  end
52
63
 
64
+ # Used for loading the fallback(default) avatar by defaut.
53
65
  def avatar_force_load
54
66
  @params['f'] = 'y'
55
67
  self
56
68
  end
57
69
 
70
+ # Unsets the force_load options.
58
71
  def avatar_unforce_load
59
72
  @params.delete('f') if @params.has_key?('f')
60
73
  self
61
74
  end
62
75
 
76
+ # Returns the avatar rating.
63
77
  def avatar_rating
64
78
  @params['r']
65
79
  end
66
80
 
81
+ # Returns the link to the default avatar.
67
82
  def avatar_default
68
83
  @params['d']
69
84
  end
70
85
 
86
+ # Returns the size of the avatar.
71
87
  def avatar_size
72
88
  @params['s']
73
89
  end
74
90
 
91
+ # Sets the rating of the avatar.
92
+ # Example gravatar.avatar_rating = 'pg' # Choose from g, pg, r and x.
75
93
  def avatar_rating=(r)
76
94
  @params['r'] = r
77
95
  end
78
96
 
97
+ # Sets the default avatar.
98
+ # Example gravatar.avatar_default = "http://example.com/yourpic.jpg"
79
99
  def avatar_default=(d)
80
100
  @params['d'] = d
81
101
  end
82
102
 
103
+ # Sets the size of the avatar in px.
104
+ # Example gravatar.avatar_size = 128 # Should not exceed 512
83
105
  def avatar_size=(s)
84
106
  @params['s'] = s
85
107
  end
data/lib/gravylicious.rb~ CHANGED
@@ -1,36 +1,45 @@
1
- #!/usr/bin/env ruby
2
-
1
+ # A ruby library for generating Gravatar urls.
2
+ # Author:: Utsav Gupta
3
+ # Copyright:: Copyright (c) 2011 Utsav Gupta
4
+ # License:: MIT License
3
5
  require 'digest/md5'
4
6
  require 'uri'
5
7
 
6
8
  class Gravylicious
7
9
  attr_accessor :email, :params, :param_filters, :use_https
8
10
 
9
- # A hash to store commonly used filters
11
+ # A hash to store commonly used filters
12
+ # like sanitize_url for escaping urls.
10
13
  @@common_filters = {'sanitize_url' => proc{|link| URI.escape(link, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))},
11
14
  }
12
15
 
16
+ # The constructor takes the email address and an optional boolean
17
+ # parameter which specifies whether or not to use https requests,
18
+ # by deafault it's set to fasle. It stores the email address in @email
19
+ # and sets up a collection of default filters in @param_filters.
13
20
  def initialize(email, use_https = false)
14
21
  @email = email.strip
15
22
  @use_https = use_https
16
23
 
17
24
  @params = Hash.new
18
25
 
19
- # Define the default filters
20
26
  @param_filters = {'d' => @@common_filters['sanitize_url'],
21
27
  's' => proc{|s| raise("Invalid avatar size: The requested size is #{s}, valid range 1 to 512") if (s < 1 || s > 512); s},
22
28
  'r' => proc{|r| raise("Invalid avatar rating: Select from g, pg, r and x") unless r =~ /^(p?)g$|^r$|^x$/; r}
23
29
  }
24
30
  end
25
31
 
32
+ # Class function that returns the common_filters hash.
26
33
  def self.common_filters
27
34
  @@common_filters
28
35
  end
29
36
 
37
+ # Returns the MD5 hash of the email address.
30
38
  def email_hash
31
39
  Digest::MD5.hexdigest(@email.downcase)
32
40
  end
33
41
 
42
+ # Returns the link to the desired Gravatar.
34
43
  def avatar_url
35
44
  g_url = "http#{'s' if @use_https}://#{'secure.' if @use_https}gravatar.com/avatar/#{email_hash}"
36
45
 
@@ -52,36 +61,47 @@ class Gravylicious
52
61
  g_url
53
62
  end
54
63
 
64
+ # Used for loading the fallback(default) avatar by defaut.
55
65
  def avatar_force_load
56
66
  @params['f'] = 'y'
57
67
  self
58
68
  end
59
69
 
70
+ # Unsets the force_load options.
60
71
  def avatar_unforce_load
61
72
  @params.delete('f') if @params.has_key?('f')
62
73
  self
63
74
  end
64
75
 
76
+ # Returns the avatar rating.
65
77
  def avatar_rating
66
78
  @params['r']
67
79
  end
68
80
 
81
+ # Returns the link to the default avatar.
69
82
  def avatar_default
70
83
  @params['d']
71
84
  end
72
85
 
86
+ # Returns the size of the avatar.
73
87
  def avatar_size
74
88
  @params['s']
75
89
  end
76
90
 
91
+ # Sets the rating of the avatar.
92
+ # Example gravatar.avatar_rating = 'pg' # Choose from g, pg, r and x.
77
93
  def avatar_rating=(r)
78
94
  @params['r'] = r
79
95
  end
80
96
 
97
+ # Sets the default avatar.
98
+ # Example gravatar.avatar_default = "http://example.com/yourpic.jpg"
81
99
  def avatar_default=(d)
82
100
  @params['d'] = d
83
101
  end
84
102
 
103
+ # Sets the size of the avatar in px.
104
+ # Example gravatar.avatar_size = 128 # Should not exceed 512
85
105
  def avatar_size=(s)
86
106
  @params['s'] = s
87
107
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gravylicious
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 1
10
- version: 1.0.1
9
+ - 2
10
+ version: 1.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Utsav Gupta
@@ -15,7 +15,7 @@ autorequire: gravylicious
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-06-27 00:00:00 Z
18
+ date: 2011-07-19 00:00:00 Z
19
19
  dependencies: []
20
20
 
21
21
  description:
@@ -30,7 +30,7 @@ files:
30
30
  - lib/gravylicious.rb
31
31
  - lib/gravylicious.rb~
32
32
  - tests/tc_gravylicious.rb
33
- homepage: https://github.com/utsavgupta
33
+ homepage: https://github.com/utsavgupta/gravylicious
34
34
  licenses: []
35
35
 
36
36
  post_install_message:
@@ -65,4 +65,4 @@ specification_version: 3
65
65
  summary: A library for generating Gravatar urls.
66
66
  test_files:
67
67
  - tests/tc_gravylicious.rb
68
- has_rdoc:
68
+ has_rdoc: true