gravylicious 1.0.0

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.
@@ -0,0 +1,88 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'digest/md5'
4
+ require 'uri'
5
+
6
+ class Gravylicious
7
+ attr_accessor :email, :params, :param_filters, :use_https
8
+
9
+ # A hash to store commonly used filters
10
+ @@common_filters = {'sanitize_url' => proc{|link| URI.escape(link, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))},
11
+ }
12
+
13
+ def initialize(email, use_https = false)
14
+ @email = email.strip
15
+ @use_https = use_https
16
+
17
+ @params = Hash.new
18
+
19
+ # Define the default filters
20
+ @param_filters = {'d' => @@common_filters['sanitize_url'],
21
+ 's' => proc{|s| raise("Invalid avatar size: The requested size is #{s}, valid range 1 to 512") if (s < 1 || s > 512); s},
22
+ 'r' => proc{|r| raise("Invalid avatar rating: Select from g, pg, r and x") unless r =~ /^(p?)g$|^r$|^x$/; r}
23
+ }
24
+ end
25
+
26
+ def self.common_filters
27
+ @@common_filters
28
+ end
29
+
30
+ def email_hash
31
+ Digest::MD5.hexdigest(@email.downcase)
32
+ end
33
+
34
+ def avatar_url
35
+ g_url = "http#{'s' if @use_https}://#{'secure.' if @use_https}gravatar.com/avatar/#{email_hash}"
36
+
37
+ if params.size > 0 # If the list of parameters is not empty then append
38
+ g_url += "?"
39
+
40
+ @params.each do |param|
41
+ # If the value of the parameter is not nil then either apply filter or append the orginal value (ie. if it has no filter).
42
+
43
+ if param[1]
44
+ g_url += param[0] + "=" + ((@param_filters[param[0]].call(param[1]) if @param_filters[param[0]]) || param[1]).to_s + "&"
45
+ end
46
+
47
+ end
48
+
49
+ g_url = g_url[0...-1]
50
+ end
51
+
52
+ g_url
53
+ end
54
+
55
+ def avatar_force_load
56
+ @params['f'] = 'y'
57
+ self
58
+ end
59
+
60
+ def avatar_unforce_load
61
+ @params.delete('f') if @params.has_key?('f')
62
+ self
63
+ end
64
+
65
+ def avatar_rating
66
+ @params['r']
67
+ end
68
+
69
+ def avatar_default
70
+ @params['d']
71
+ end
72
+
73
+ def avatar_size
74
+ @params['s']
75
+ end
76
+
77
+ def avatar_rating=(r)
78
+ @params['r'] = r
79
+ end
80
+
81
+ def avatar_default=(d)
82
+ @params['d'] = d
83
+ end
84
+
85
+ def avatar_size=(s)
86
+ @params['s'] = s
87
+ end
88
+ end
@@ -0,0 +1,86 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'digest/md5'
4
+ require 'uri'
5
+
6
+ class Gravylicious
7
+ attr_accessor :email, :params, :param_filters, :use_https
8
+
9
+ # A hash to store commonly used filters
10
+ @@common_filters = {'sanitize_url' => proc{|link| URI.escape(link, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))},
11
+ }
12
+
13
+ def initialize(email, use_https = false)
14
+ @email = email.strip
15
+ @use_https = use_https
16
+
17
+ @params = Hash.new
18
+
19
+ # Define the default filters
20
+ @param_filters = {'d' => @@common_filters['sanitize_url'],
21
+ 's' => proc{|s| raise("Invalid avatar size: The requested size is #{s}, valid range 1 to 512") if (s < 1 || s > 512); s},
22
+ 'r' => proc{|r| raise("Invalid avatar rating: Select from g, pg, r and x") unless r =~ /^(p?)g$|^r$|^x$/; r}
23
+ }
24
+ end
25
+
26
+ def self.common_filters
27
+ @@common_filters
28
+ end
29
+
30
+ def email_hash
31
+ Digest::MD5.hexdigest(@email.downcase)
32
+ end
33
+
34
+ def avatar_url
35
+ g_url = "http#{'s' if @use_https}://#{'secure.' if @use_https}gravatar.com/avatar/#{email_hash}"
36
+
37
+ if params.size > 0 # If the list of parameters is not empty then append
38
+ g_url += "?"
39
+
40
+ @params.each do |param|
41
+ # If the value of the parameter is not nil then either apply filter or append the orginal value (ie. if it has no filter).
42
+ if param[1]
43
+ g_url += param[0] + "=" + ((@param_filters[param[0]].call(param[1]) if @param_filters[param[0]]) || param[1]).to_s + "&"
44
+ end
45
+ end
46
+
47
+ g_url = g_url[0...-1]
48
+ end
49
+
50
+ g_url
51
+ end
52
+
53
+ def avatar_force_load
54
+ @params['f'] = 'y'
55
+ self
56
+ end
57
+
58
+ def avatar_unforce_load
59
+ @params.delete('f') if @params.has_key?('f')
60
+ self
61
+ end
62
+
63
+ def avatar_rating
64
+ @params['r']
65
+ end
66
+
67
+ def avatar_default
68
+ @params['d']
69
+ end
70
+
71
+ def avatar_size
72
+ @params['s']
73
+ end
74
+
75
+ def avatar_rating=(r)
76
+ @params['r'] = r
77
+ end
78
+
79
+ def avatar_default=(d)
80
+ @params['d'] = d
81
+ end
82
+
83
+ def avatar_size=(s)
84
+ @params['s'] = s
85
+ end
86
+ end
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'sinatra'
5
+ require 'gravylicious'
6
+
7
+ get '/' do
8
+ gravatar = Gravylicious.new("you@example.com")
9
+ gravatar.avatar_size = 128
10
+ gravatar.avatar_default = "identicon"
11
+ gravatar.avatar_rating = 'g'
12
+ "<img src=\"#{gravatar.avatar_url}\" />"
13
+ end
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'test/unit'
4
+ require 'gravylicious'
5
+
6
+ class TestGravylicious < Test::Unit::TestCase
7
+ @@gravatar = Gravylicious.new("god@heaven.com\n", true)
8
+
9
+ def test_email_hash
10
+ assert_equal(@@gravatar.email_hash, "f1260e36d2bbf84ed621e387410c4a61")
11
+ assert_equal(Gravylicious.new(" man@earth.com ").email_hash, "f8872d66ac8c25ebd5a84e027873629b")
12
+ end
13
+
14
+ def test_use_https
15
+ assert_match(/https:\/\/secure\./, @@gravatar.avatar_url)
16
+ @@gravatar.use_https = false
17
+ assert_match(/http:\/\//, @@gravatar.avatar_url)
18
+ end
19
+
20
+ def test_filters
21
+ assert_equal(Gravylicious.common_filters['sanitize_url'].call('http://example.com/album?p=picture_1.jpg'), 'http%3A%2F%2Fexample.com%2Falbum%3Fp%3Dpicture_1.jpg')
22
+ end
23
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gravylicious
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Utsav Gupta
14
+ autorequire: gravylicious
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-06-26 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: sinatra
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 19
29
+ segments:
30
+ - 1
31
+ - 2
32
+ - 6
33
+ version: 1.2.6
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ description:
37
+ email: utsavgupta89@gmail.com
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files: []
43
+
44
+ files:
45
+ - lib/gravylicious.rb
46
+ - lib/gravylicious.rb~
47
+ - tests/tc_gravylicious.rb
48
+ - tests/sinatra_webpage.rb
49
+ homepage: https://github.com/utsavgupta
50
+ licenses: []
51
+
52
+ post_install_message:
53
+ rdoc_options: []
54
+
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ hash: 3
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ hash: 3
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ requirements: []
76
+
77
+ rubyforge_project:
78
+ rubygems_version: 1.7.2
79
+ signing_key:
80
+ specification_version: 3
81
+ summary: A library for generating Gravatar urls.
82
+ test_files:
83
+ - tests/tc_gravylicious.rb
84
+ - tests/sinatra_webpage.rb
85
+ has_rdoc: