gcnovus-avatar 0.0.7 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -9,6 +9,7 @@ Avatar is a collection of Ruby utilities for displaying avatars.
9
9
 
10
10
  Avatar currently supports the following sources:
11
11
  * Gravatar (see http://www.gravatar.com)
12
+ * Initial Pavatar support (see http://pavatar.com/spec/)
12
13
  * a constant URL (e.g. http://mysite.com/images/default_icon.png)
13
14
  * parameterized URLs (e.g. http://mysite.com/images/famfamfam/user_#{color}.png)
14
15
  * file_column (see http://www.kanthak.net/opensource/file_column/)
@@ -18,7 +19,12 @@ Avatar currently supports the following views:
18
19
  * ActionView (Rails), through avatar_tag
19
20
  * AbstractView (any framework), through avatar_url_for
20
21
 
21
- == SYNOPSIS:
22
+ == BUG REPORTING:
23
+
24
+ Submit bugs to https://gcnovus.fogbugz.com/default.asp?pg=pgPublicEdit under the "Avatar Gem" project,
25
+ or discuss at http://groups.google.com/group/ruby-avatar/.
26
+
27
+ == BASICS:
22
28
 
23
29
  in RAILS_ROOT/app/helpers/people_helper.rb
24
30
  require 'avatar/view/action_view_support'
@@ -43,7 +49,7 @@ in RAILS_ROOT/app/views/people/show.html.erb:
43
49
 
44
50
  (The MIT License)
45
51
 
46
- Copyright (c) 2008 Universal Presenece, Inc.
52
+ Copyright (c) 2008 James Rosen
47
53
 
48
54
  Permission is hereby granted, free of charge, to any person obtaining
49
55
  a copy of this software and associated documentation files (the
@@ -62,4 +68,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
62
68
  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
63
69
  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
64
70
  TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
65
- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
71
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -1,6 +1,7 @@
1
1
  $:.unshift File.expand_path(File.dirname(__FILE__))
2
2
  require 'avatar/source/abstract_source'
3
3
  require 'avatar/source/gravatar_source'
4
+ require 'avatar/source/pavatar_source'
4
5
 
5
6
  # Helpers for displaying avatars.
6
7
  # Usage in Rails:
@@ -31,7 +32,7 @@ module Avatar
31
32
 
32
33
  def self.default_avatar_options=(options)
33
34
  raise ArgumentError.new("#{options} is not a Hash") unless options.kind_of?(Hash)
34
- @@options = options
35
+ @@default_avatar_options = options
35
36
  end
36
37
 
37
- end
38
+ end
@@ -0,0 +1,109 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+
4
+ module Avatar # :nodoc:
5
+ module Source # :nodoc:
6
+ # Implements http://pavatar.com/spec/
7
+ class PavatarSource
8
+ include AbstractSource
9
+
10
+ PAVATAR_REGEXP = /^((?:http|https):\/\/.+)$/ix
11
+
12
+ attr_accessor :http_connection_factory
13
+
14
+ # Create a new PAvatar source.
15
+ #
16
+ # Options:
17
+ # * <code>:http_connection_factory</code> - the Object used to start new HTTP connections. By default,
18
+ # the <code>Net::HTTP</code> class. To use a proxy, pass
19
+ # <code>:http_connection_factory => Net::HTTP::Proxy(...)</code>
20
+ def initialize(options = {})
21
+ self.http_connection_factory = options.delete(:http_connection_factory) || Net::HTTP
22
+ end
23
+
24
+ # Discovers pavatar URL. Returns nil if person is nil.
25
+ #
26
+ # Options:
27
+ # * <code>:pavatar_field (Symbol)</code> - the field to call from person. By default, <code>:blog_url</code>.
28
+ def avatar_url_for(person, options = {})
29
+ return nil if person.nil?
30
+ options = options.merge! ::Avatar.default_avatar_options
31
+ field = options.delete(:pavatar_field) || :blog_url
32
+ raise ArgumentError.new('No field specified; either specify a default field or pass in a value for :pavatar_field (probably :blog_url)') unless field
33
+
34
+ pavatar_url = autodiscover_pavatar_url_from person.send(field)
35
+
36
+ (pavatar_url.nil? || pavatar_url.to_s.blank?) ? nil : pavatar_url
37
+ end
38
+
39
+ private
40
+
41
+ # Autodiscover PAvatar URL from a +profile_url+.
42
+ #
43
+ # See http://pavatar.com/spec/#autodiscovery
44
+ #
45
+ # Returns +nil+ if +profile_url+ is blank.
46
+ def autodiscover_pavatar_url_from(profile_url)
47
+ return nil if profile_url.blank?
48
+ uri = URI.parse(profile_url)
49
+ begin
50
+ self.http_connection_factory.start(uri.host, uri.port) do |http|
51
+ return avatar_from_header(http) || avatar_from_link_element(http) || direct_avatar(profile_url, http) || nil
52
+ end
53
+ rescue Errno::ECONNREFUSED => e
54
+ nil
55
+ end
56
+ end
57
+
58
+
59
+ # Autodisciover using http headers.
60
+ #
61
+ # See http://pavatar.com/spec/#http-header
62
+ #
63
+ # Returns the URL if found; +nil+ otherwise.
64
+ def avatar_from_header(http)
65
+ resp = http.head('/')
66
+ resp.kind_of?(Net::HTTPSuccess) && resp['X-Pavatar']
67
+ end
68
+
69
+
70
+ # Autodiscover using link element.
71
+ #
72
+ # See http://pavatar.com/spec/#link-element
73
+ #
74
+ # Returns the URL if found; +nil+ otherwise.
75
+ def avatar_from_link_element(http)
76
+ resp = http.get('/')
77
+ resp.kind_of?(Net::HTTPSuccess) && parse_html_for_pavatar(resp.body)
78
+ end
79
+
80
+
81
+ # Look for a <link rel='pavatar' href='...' />
82
+ #
83
+ # Returns the URL if found; +nil+ otherwise.
84
+ def parse_html_for_pavatar(doc)
85
+ return nil if doc.nil? or doc.empty?
86
+
87
+ # 4.a 2 http://pavatar.com/spec/#autodiscovery-algorithm
88
+ # recommends following regexp /<link rel="pavatar" href="([^""]+)" ?/?>/
89
+ # but it will not always match valid xhtml link element.
90
+ #
91
+ if result = doc.match(/<link(.+)rel=["']pavatar["'](.+)>/)
92
+ result.to_a[1..-1].map { |s| s.split(/["']/) }.flatten.find { |url| url =~ PAVATAR_REGEXP }
93
+ end
94
+ end
95
+
96
+
97
+ # Autodiscover using direct URL.
98
+ #
99
+ # See http://pavatar.com/spec/#direct-url
100
+ #
101
+ # Returns the URL if found; +nil+ otherwise.
102
+ def direct_avatar(profile_url, http)
103
+ resp = http.head('/pavatar.png')
104
+ profile_url + (profile_url =~ /\/$/ ? '' : '/') + 'pavatar.png' if resp.kind_of?(Net::HTTPSuccess)
105
+ end
106
+
107
+ end
108
+ end
109
+ end
@@ -1,4 +1,5 @@
1
1
  require 'avatar/source/wrapper/abstract_source_wrapper'
2
+ require 'action_view/helpers/tag_helper'
2
3
  require 'action_view/helpers/asset_tag_helper'
3
4
 
4
5
  module Avatar # :nodoc:
@@ -33,4 +34,5 @@ module Avatar # :nodoc:
33
34
  end
34
35
  end
35
36
  end
36
- end
37
+ end
38
+
@@ -1,8 +1,8 @@
1
1
  module Avatar #:nodoc:
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
- MINOR = 0
5
- TINY = 7
4
+ MINOR = 2
5
+ TINY = 0
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gcnovus-avatar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Rosen
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-07-17 00:00:00 -07:00
12
+ date: 2009-07-16 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -24,7 +24,7 @@ extra_rdoc_files: []
24
24
  files:
25
25
  - History.txt
26
26
  - License.txt
27
- - README.txt
27
+ - README.rdoc
28
28
  - init.rb
29
29
  - rails/init.rb
30
30
  - lib/avatar
@@ -35,6 +35,7 @@ files:
35
35
  - lib/avatar/source/gravatar_source.rb
36
36
  - lib/avatar/source/nil_source.rb
37
37
  - lib/avatar/source/paperclip_source.rb
38
+ - lib/avatar/source/pavatar_source.rb
38
39
  - lib/avatar/source/source_chain.rb
39
40
  - lib/avatar/source/static_url_source.rb
40
41
  - lib/avatar/source/wrapper
@@ -49,7 +50,7 @@ files:
49
50
  - lib/avatar/view/action_view_support.rb
50
51
  - lib/avatar/view.rb
51
52
  - lib/avatar.rb
52
- has_rdoc: true
53
+ has_rdoc: false
53
54
  homepage: http://github.com/gcnovus/avatar
54
55
  post_install_message:
55
56
  rdoc_options: