libravatar 1.0.1 → 1.1.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.
- data/VERSION +1 -1
- data/lib/libravatar.rb +44 -1
- data/libravatar.gemspec +2 -6
- data/test/test_libravatar.rb +5 -0
- metadata +6 -7
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0
|
1
|
+
1.1.0
|
data/lib/libravatar.rb
CHANGED
@@ -7,11 +7,13 @@
|
|
7
7
|
# Author:: Kang-min Liu (http://gugod.org)
|
8
8
|
# Copyright:: Copyright (c) 2011 Kang-min Liu
|
9
9
|
# License:: MIT
|
10
|
+
# Contributors:: https://github.com/gugod/libravatar/contributors
|
10
11
|
#
|
11
12
|
|
12
13
|
require 'digest/md5'
|
13
14
|
require 'digest/sha2'
|
14
15
|
require 'uri'
|
16
|
+
require 'resolv'
|
15
17
|
|
16
18
|
class Libravatar
|
17
19
|
attr_accessor :email, :openid, :size, :default, :https
|
@@ -36,6 +38,47 @@ class Libravatar
|
|
36
38
|
@https = options[:https]
|
37
39
|
end
|
38
40
|
|
41
|
+
def get_target_domain
|
42
|
+
return @email.split('@')[1] if @email
|
43
|
+
return URI.parse(@openid).host
|
44
|
+
end
|
45
|
+
|
46
|
+
# All the values which are different between HTTP and HTTPS methods.
|
47
|
+
@@profiles = [
|
48
|
+
{ :scheme => 'http://',
|
49
|
+
:host => 'cdn.libravatar.org',
|
50
|
+
:srv => '_avatars._tcp.',
|
51
|
+
:port => 80 },
|
52
|
+
{ :scheme => 'https://',
|
53
|
+
:host => 'seccdn.libravatar.org',
|
54
|
+
:srv => '_avatars-sec._tcp.',
|
55
|
+
:port => 443 }
|
56
|
+
]
|
57
|
+
|
58
|
+
# Grab the DNS SRV records associated with the target domain,
|
59
|
+
# and choose one according to RFC2782.
|
60
|
+
def get_base_url
|
61
|
+
profile = @@profiles[ @https ? 1 : 0 ]
|
62
|
+
Resolv::DNS::open do |dns|
|
63
|
+
rrs = dns.getresources(profile[:srv] + get_target_domain(),
|
64
|
+
Resolv::DNS::Resource::IN::SRV).to_a
|
65
|
+
return profile[:scheme] + profile[:host] unless rrs.any?
|
66
|
+
|
67
|
+
min_priority = rrs.map{ |r| r.priority }.min
|
68
|
+
rrs.delete_if{ |r| r.priority != min_priority }
|
69
|
+
rrs = rrs.select{ |r| r.weight == 0 } +
|
70
|
+
rrs.select{ |r| r.weight > 0 }.shuffle
|
71
|
+
|
72
|
+
weight_sum = rrs.inject(0) { |a,r| a+r.weight }
|
73
|
+
value = rand( weight_sum + 1 )
|
74
|
+
rrs.each do |r|
|
75
|
+
port_fragment = r.port != profile[:port] ? ':' + r.port : ''
|
76
|
+
return profile[:scheme] + r.target.to_s + port_fragment if r.weight <= value
|
77
|
+
value -= r.weight
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
39
82
|
# Generate the libravatar URL
|
40
83
|
def to_s
|
41
84
|
if @email
|
@@ -49,7 +92,7 @@ class Libravatar
|
|
49
92
|
|
50
93
|
query = [s,d].reject{|x|!x}.join("&")
|
51
94
|
query = "?#{query}" unless query == ""
|
52
|
-
baseurl =
|
95
|
+
baseurl = get_base_url() + '/avatar/'
|
53
96
|
return baseurl + id + query
|
54
97
|
end
|
55
98
|
|
data/libravatar.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{libravatar}
|
8
|
-
s.version = "1.0
|
8
|
+
s.version = "1.1.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Kang-min Liu"]
|
12
|
-
s.date = %q{2011-
|
12
|
+
s.date = %q{2011-06-26}
|
13
13
|
s.description = %q{libravatar.org provides avatar image hosting (like gravatar.com). Their users may associate avatar images with email or openid. This rubygem can be used to generate libravatar avatar image URL}
|
14
14
|
s.email = %q{gugod@gugod.org}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -32,10 +32,6 @@ Gem::Specification.new do |s|
|
|
32
32
|
s.require_paths = ["lib"]
|
33
33
|
s.rubygems_version = %q{1.6.1}
|
34
34
|
s.summary = %q{Avatar URL Generation wih libravatar.org}
|
35
|
-
s.test_files = [
|
36
|
-
"test/helper.rb",
|
37
|
-
"test/test_libravatar.rb"
|
38
|
-
]
|
39
35
|
|
40
36
|
if s.respond_to? :specification_version then
|
41
37
|
s.specification_version = 3
|
data/test/test_libravatar.rb
CHANGED
@@ -37,4 +37,9 @@ class TestLibravatar < Test::Unit::TestCase
|
|
37
37
|
|
38
38
|
assert_equal x.send(:normalize_openid, "HTTP://EXAMPLE.COM"), "http://example.com/"
|
39
39
|
end
|
40
|
+
|
41
|
+
should "Retured the federated URI" do
|
42
|
+
avatar = Libravatar.new(:email => 'invalid@catalyst.net.nz')
|
43
|
+
assert_equal avatar.to_s, 'http://static.avatars.catalyst.net.nz/avatar/f924d1e9f2c10ee9efa7acdd16484c2f'
|
44
|
+
end
|
40
45
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: libravatar
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
|
-
- 0
|
9
8
|
- 1
|
10
|
-
|
9
|
+
- 0
|
10
|
+
version: 1.1.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Kang-min Liu
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-06-26 00:00:00 +08:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -87,6 +87,5 @@ rubygems_version: 1.6.1
|
|
87
87
|
signing_key:
|
88
88
|
specification_version: 3
|
89
89
|
summary: Avatar URL Generation wih libravatar.org
|
90
|
-
test_files:
|
91
|
-
|
92
|
-
- test/test_libravatar.rb
|
90
|
+
test_files: []
|
91
|
+
|