vishnu 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f6982dc66f9e649a14ff319acef50fd6807ab767
4
+ data.tar.gz: 84be13069a4e230c0b406a77de2e51dd53c8edaa
5
+ SHA512:
6
+ metadata.gz: 2e4c09569e39cec6492d69c1c0354e3cd6f52977d008f321daa404ad3d08238addb9af7cfb2c2768320b2dd23cb66979dc030d5832b67c5ef76fd762d17c9896
7
+ data.tar.gz: dad9a5b0b60fd395a52f9ae094819a49c9e3aa075e766ad76d45affd6f8aae7844a8dafc08b28dd21089c57b765b217832b6d128f434a1220cefabec6daad8e5
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
@@ -0,0 +1,22 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2011 Kang-min Liu
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
22
+
@@ -0,0 +1,20 @@
1
+ = libravatar
2
+
3
+ http://libravatar.org is an avatar service to let their users associate avatar images
4
+ with their emails or openids. This rubygem generates their avatar URL.
5
+
6
+ == Contributing to libravatar
7
+
8
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
9
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
10
+ * Fork the project
11
+ * Start a feature/bugfix branch
12
+ * Commit and push until you are happy with your contribution
13
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
14
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
15
+
16
+ == Copyright
17
+
18
+ Copyright (c) 2011 Kang-min Liu. See LICENSE.txt for
19
+ further details.
20
+
@@ -0,0 +1,46 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ require 'jeweler'
5
+ Jeweler::Tasks.new do |gem|
6
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
7
+ gem.name = "vishnu"
8
+ gem.homepage = "http://github.com/sandfoxme/libravatar"
9
+ gem.license = "MIT"
10
+ gem.summary = %Q{Avatar URL Generation wih libravatar.org}
11
+ gem.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}
12
+ gem.email = "sandfox@sandfox.me"
13
+ gem.authors = ['Anton Smirnov', "Kang-min Liu"]
14
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
15
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
16
+ # gem.add_runtime_dependency 'digest/sha2', '> 0.1'
17
+ # gem.add_runtime_dependency 'uri', '> 0.1'
18
+ gem.add_development_dependency 'shoulda', '> 1.2.3'
19
+ end
20
+ Jeweler::RubygemsDotOrgTasks.new
21
+
22
+ require 'rake/testtask'
23
+ Rake::TestTask.new(:test) do |test|
24
+ test.libs << 'lib' << 'test'
25
+ test.pattern = 'test/**/test_*.rb'
26
+ test.verbose = true
27
+ end
28
+
29
+ # require 'rcov/rcovtask'
30
+ # Rcov::RcovTask.new do |test|
31
+ # test.libs << 'test'
32
+ # test.pattern = 'test/**/test_*.rb'
33
+ # test.verbose = true
34
+ # end
35
+
36
+ task :default => :test
37
+
38
+ require 'rake/rdoctask'
39
+ Rake::RDocTask.new do |rdoc|
40
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
41
+
42
+ rdoc.rdoc_dir = 'rdoc'
43
+ rdoc.title = "vishnu #{version}"
44
+ rdoc.rdoc_files.include('README*')
45
+ rdoc.rdoc_files.include('lib/**/*.rb')
46
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.2.1
@@ -0,0 +1,3 @@
1
+ require 'vishnu'
2
+
3
+ class Libravatar < Vishnu; end
@@ -0,0 +1,128 @@
1
+ #
2
+ # The Libravatar class generates the avatar URL provided by the libravatar
3
+ # web service at http://www.libravatar.org
4
+ #
5
+ # Users may associate their avatar images with multiple OpenIDs and Emails.
6
+ #
7
+ # Author:: Kang-min Liu (http://gugod.org)
8
+ # Copyright:: Copyright (c) 2011 Kang-min Liu
9
+ # License:: MIT
10
+ # Contributors:: https://github.com/gugod/libravatar/contributors
11
+ #
12
+
13
+ require 'digest/md5'
14
+ require 'digest/sha2'
15
+ require 'uri'
16
+ require 'resolv'
17
+
18
+ class Vishnu
19
+ attr_accessor :email, :openid, :size, :default, :https
20
+
21
+ # The options should contain :email or :openid values. If both are
22
+ # given, email will be used. The value of openid and email will be
23
+ # normalized by the rule described in http://www.libravatar.org/api
24
+ #
25
+ # List of option keys:
26
+ #
27
+ # - :email
28
+ # - :openid
29
+ # - :size An integer ranged 1 - 512, default is 80.
30
+ # - :https Set to true to serve avatars over SSL
31
+ # - :default URL to redirect missing avatars to, or one of these specials: "404", "mm", "identicon", "monsterid", "wavatar", "retro"
32
+ #
33
+ def initialize(options = {})
34
+ @email = options[:email]
35
+ @openid = options[:openid]
36
+ @size = options[:size]
37
+ @default = options[:default]
38
+ @https = options[:https]
39
+ end
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 srv_lookup
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 [nil, nil] unless rrs.any?
66
+
67
+
68
+ min_priority = rrs.map{ |r| r.priority }.min
69
+ rrs.delete_if{ |r| r.priority != min_priority }
70
+
71
+ weight_sum = rrs.inject(0) { |a,r| a+r.weight }.to_f
72
+
73
+ r = rrs.max_by { |r| r.weight == 0 ? 0 : rand ** (weight_sum / r.weight) }
74
+
75
+ return [r.target, r.port]
76
+ end
77
+ end
78
+
79
+ def get_base_url
80
+ profile = @@profiles[ @https ? 1 : 0 ]
81
+ target, port = srv_lookup
82
+
83
+ if (target && port)
84
+ port_fragment = port != profile[:port] ? ':' + port.to_s : ''
85
+ return profile[:scheme] + target.to_s + port_fragment
86
+ else
87
+ return profile[:scheme] + profile[:host]
88
+ end
89
+ end
90
+
91
+ # Generate the libravatar URL
92
+ def to_s
93
+ if @email
94
+ @email.downcase!
95
+ id = Digest::MD5.hexdigest(@email)
96
+ else
97
+ id = Digest::SHA2.hexdigest(normalize_openid(@openid))
98
+ end
99
+ s = @size ? "s=#{@size}" : nil
100
+ d = @default ? "d=#{@default}" : nil
101
+
102
+ query = [s,d].reject{|x|!x}.join("&")
103
+ query = "?#{query}" unless query == ""
104
+ baseurl = get_base_url() + '/avatar/'
105
+ return baseurl + id + query
106
+ end
107
+
108
+ private
109
+
110
+ def sanitize_srv_lookup(hostname, port)
111
+ unless hostname.match(/^[0-9a-zA-Z\-.]+$/) && 1 <= port && port <= 65535
112
+ return [nil, nil]
113
+ end
114
+
115
+ return [hostname, port]
116
+ end
117
+
118
+ # Normalize an openid URL following the description on libravatar.org
119
+ def normalize_openid(s)
120
+ x = URI.parse(s)
121
+ x.host.downcase!
122
+ x.scheme = x.scheme.downcase
123
+ if (x.path == "" && x.fragment == nil)
124
+ x.path = "/"
125
+ end
126
+ return x.to_s
127
+ end
128
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'libravatar'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,51 @@
1
+ require 'helper'
2
+
3
+ class TestLibravatar < Test::Unit::TestCase
4
+ should "Generate url from email" do
5
+ # echo -n "user@example.com"|md5sum
6
+ # => b58996c504c5638798eb6b511e6f49af
7
+ avatar = Libravatar.new(:email => "user@example.com")
8
+ assert_equal avatar.to_s, "http://cdn.libravatar.org/avatar/b58996c504c5638798eb6b511e6f49af"
9
+
10
+ assert_equal Libravatar.new(:email => "USER@ExAmPlE.CoM").to_s, "http://cdn.libravatar.org/avatar/b58996c504c5638798eb6b511e6f49af"
11
+
12
+ assert_equal Libravatar.new(:email => "user@example.com", :https => true).to_s, "https://seccdn.libravatar.org/avatar/b58996c504c5638798eb6b511e6f49af"
13
+
14
+ assert_equal Libravatar.new(:email => "user@example.com", :https => false).to_s, "http://cdn.libravatar.org/avatar/b58996c504c5638798eb6b511e6f49af"
15
+
16
+ assert_equal Libravatar.new(:email => "USER@ExAmPlE.CoM", :default => "http://example.com/avatar.png").to_s, "http://cdn.libravatar.org/avatar/b58996c504c5638798eb6b511e6f49af?d=http://example.com/avatar.png"
17
+
18
+ assert_equal Libravatar.new(:email => "USER@ExAmPlE.CoM", :size => 512, :default => "mm").to_s, "http://cdn.libravatar.org/avatar/b58996c504c5638798eb6b511e6f49af?s=512&d=mm"
19
+ end
20
+
21
+ should "Generate url from openid" do
22
+ # echo -n "http://example.com/id/Bob"|shasum -a 256
23
+ # => 80cd0679bb52beac4d5d388c163016dbc5d3f30c262a4f539564236ca9d49ccd
24
+ avatar = Libravatar.new(:openid => "http://example.com/id/Bob")
25
+ assert_equal avatar.to_s, "http://cdn.libravatar.org/avatar/80cd0679bb52beac4d5d388c163016dbc5d3f30c262a4f539564236ca9d49ccd"
26
+
27
+ avatar = Libravatar.new(:openid => "hTTp://EXAMPLE.COM/id/Bob")
28
+ assert_equal avatar.to_s, "http://cdn.libravatar.org/avatar/80cd0679bb52beac4d5d388c163016dbc5d3f30c262a4f539564236ca9d49ccd"
29
+
30
+ avatar = Libravatar.new(:openid => "hTTp://EXAMPLE.COM/id/Bob", :size => 512)
31
+ assert_equal avatar.to_s, "http://cdn.libravatar.org/avatar/80cd0679bb52beac4d5d388c163016dbc5d3f30c262a4f539564236ca9d49ccd?s=512"
32
+ end
33
+
34
+ should "Normalize OpenID" do
35
+ x = Libravatar.new
36
+ assert_equal x.send(:normalize_openid, "HTTP://EXAMPLE.COM/id/Bob"), "http://example.com/id/Bob"
37
+
38
+ assert_equal x.send(:normalize_openid, "HTTP://EXAMPLE.COM"), "http://example.com/"
39
+ end
40
+
41
+ should "Return 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
45
+
46
+ should "Sanitize the SRV lookup result" do
47
+ avatar = Libravatar.new
48
+ assert_equal ["hosntame.abcde.fghi.com", 12345], avatar.send(:sanitize_srv_lookup, "hosntame.abcde.fghi.com", 12345)
49
+ assert_equal [nil, nil], avatar.send(:sanitize_srv_lookup, "FNORD IMPUNTK *#(*$#&", 65348283)
50
+ end
51
+ end
@@ -0,0 +1,50 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+ # stub: vishnu 1.2.1 ruby lib
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = "vishnu"
9
+ s.version = "1.2.1"
10
+
11
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
+ s.require_paths = ["lib"]
13
+ s.authors = ["Anton Smirnov", "Kang-min Liu"]
14
+ s.date = "2016-03-25"
15
+ s.description = "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"
16
+ s.email = "sandfox@sandfox.me"
17
+ s.extra_rdoc_files = [
18
+ "LICENSE.txt",
19
+ "README.rdoc"
20
+ ]
21
+ s.files = [
22
+ ".document",
23
+ "LICENSE.txt",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "lib/libravatar.rb",
28
+ "lib/vishnu.rb",
29
+ "libravatar.gemspec",
30
+ "test/helper.rb",
31
+ "test/test_libravatar.rb"
32
+ ]
33
+ s.homepage = "http://github.com/sandfoxme/libravatar"
34
+ s.licenses = ["MIT"]
35
+ s.rubygems_version = "2.4.8"
36
+ s.summary = "Avatar URL Generation wih libravatar.org"
37
+
38
+ if s.respond_to? :specification_version then
39
+ s.specification_version = 4
40
+
41
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
42
+ s.add_development_dependency(%q<shoulda>, ["> 1.2.3"])
43
+ else
44
+ s.add_dependency(%q<shoulda>, ["> 1.2.3"])
45
+ end
46
+ else
47
+ s.add_dependency(%q<shoulda>, ["> 1.2.3"])
48
+ end
49
+ end
50
+
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vishnu
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.2.1
5
+ platform: ruby
6
+ authors:
7
+ - Anton Smirnov
8
+ - Kang-min Liu
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2016-03-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: shoulda
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - '>'
19
+ - !ruby/object:Gem::Version
20
+ version: 1.2.3
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - '>'
26
+ - !ruby/object:Gem::Version
27
+ version: 1.2.3
28
+ description: libravatar.org provides avatar image hosting (like gravatar.com). Their
29
+ users may associate avatar images with email or openid. This rubygem can be used
30
+ to generate libravatar avatar image URL
31
+ email: sandfox@sandfox.me
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files:
35
+ - LICENSE.txt
36
+ - README.rdoc
37
+ files:
38
+ - .document
39
+ - LICENSE.txt
40
+ - README.rdoc
41
+ - Rakefile
42
+ - VERSION
43
+ - lib/libravatar.rb
44
+ - lib/vishnu.rb
45
+ - test/helper.rb
46
+ - test/test_libravatar.rb
47
+ - vishnu.gemspec
48
+ homepage: http://github.com/sandfoxme/libravatar
49
+ licenses:
50
+ - MIT
51
+ metadata: {}
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubyforge_project:
68
+ rubygems_version: 2.4.8
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: Avatar URL Generation wih libravatar.org
72
+ test_files: []