gravatarify 0.7.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
1
  doc/
2
2
  pkg/
3
3
  .yardoc
4
+ *.gemspec
data/README.md CHANGED
@@ -4,15 +4,19 @@ Removes any hassles building those pesky gravatar urls, it's not there arent any
4
4
  [there](http://github.com/chrislloyd/gravtastic), but none seem to support stuff like `Proc`s for the default picture url, or
5
5
  the multiple host names supported by gravatar.com (great when displaying lots of avatars).
6
6
 
7
- Best of it? It works with Rails, probably Merb and even plain old Ruby :)
7
+ And it integrates seamlessly with Rails, Merb and even plain old Ruby.
8
+
9
+ - **Source**: [http://github.com/lwe/gravatarify](http://github.com/lwe/gravatarify)
10
+ - **Docs**: [http://rdoc.info/projects/lwe/gravatarify](http://rdoc.info/projects/lwe/gravatarify)
11
+ - **Gem**: [http://gemcutter.org/gems/gravatarify](http://gemcutter.org/gems/gravatarify)
8
12
 
9
13
  ## Install
10
14
 
11
15
  Just install the gem (ensure you have gemcutter in your sources!)
12
16
 
13
- sudo gem install gravatarify
17
+ [sudo] gem install gravatarify
14
18
 
15
- Ready to go! Using Rails? Either add (to `config/environment.rb`):
19
+ Ready to go! Using Rails? Either add as gem (in `config/environment.rb`):
16
20
 
17
21
  config.gem 'gravatarify', :source => 'http://gemcutter.org'
18
22
 
@@ -20,34 +24,44 @@ or install as Rails plugin:
20
24
 
21
25
  ./script/plugin install git://github.com/lwe/gravatarify.git
22
26
 
23
- If the gem is available, just require it (for your simple ruby apps):
27
+ Of course it's also possible to just add the library onto the `$LOAD_PATH`
28
+ and then `require 'gravatarify'` it.
24
29
 
25
- require 'rubygems'
26
- require 'gravatarify'
30
+ # Usage
31
+
32
+ This library provides...
27
33
 
34
+ * ...object/model helpers, so that an object responds to `gravatar_url`, see [using the model helpers](#l_model_helpers).
35
+ Works also very well with plain old ruby objects.
36
+ * ...Rails view helpers, namely `gravatar_url` and `gravatar_tag`, see [using the view helpers](#l_view_helpers). This is rails only though!
37
+ * ...and finally, a base module which provides the gravatar url generation, ready to be integrated into
38
+ custom helpers, plain ruby code or whatever, see [back to the roots](#l_roots)
39
+
40
+ <a id="l_view_helpers"/>
28
41
  ## Using the view helpers (Rails only!)
29
42
 
30
43
  Probably one of the easiest ways to add support for gravatar images is with the included view helpers:
31
44
 
32
45
  <%= gravatar_tag @user %> # assumes @user has email or mail field!
33
-
46
+
34
47
  This builds a neat `<img/>`-tag, if you need to pass in stuff like the size etc. just:
35
48
 
36
49
  <%= gravatar_tag @user, :size => 25, :rating => :x, :class => "gravatar" %>
37
-
50
+
38
51
  This will display an "X" rated avatar which is 25x25 pixel in size and the image tag will have the class `"gravatar"`.
39
52
  If more control is required, or just the URL, well then go ahead and use `gravatar_url` instead:
40
53
 
41
- <%= image_tag gravatar_url(@user.author_email, :size => 16), :size => "16x16",
42
- :alt => @user.name, :class => "avatar avatar-16"}/
43
-
54
+ <%= image_tag gravatar_url(@user.author_email, :size => 16), :size => "16x16",
55
+ :alt => @user.name, :class => "avatar avatar-16"}/
56
+
44
57
  Using rails `image_tag` to create an `<img/>`-tag with `gravatar_url`. It's important to know that
45
58
  also an object can be passed to `gravatar_url`, if it responds to either `email` or `mail`. If not (like
46
59
  in the example above), the email address must be passed in.
47
60
 
61
+ <a id="l_model_helpers"/>
48
62
  ## Using the model helpers
49
63
 
50
- Another way (especially cool) for models is to do:
64
+ A very simple method to add `gravatar_url` support to models is by using the `gravatarify` class method.
51
65
 
52
66
  class User < ActiveRecord::Base
53
67
  gravatarify
@@ -94,6 +108,7 @@ class:
94
108
  Tadaaa! Works exactly like the model helpers, so it's now possible to call `gravatar_url` on instances
95
109
  of `PoroUser`.
96
110
 
111
+ <a id="l_roots"/>
97
112
  ## Back to the roots?
98
113
 
99
114
  No need for sophisticated stuff like view helpers and ActiveRecord integration, want to go back to the roots?
@@ -178,6 +193,27 @@ Into the block is passed the options hash and as second parameter the object its
178
193
  Not only the `:default` option accepts a Proc, but also `:secure`, can be useful to handle cases where
179
194
  it should evaluate against `request.ssl?` for example.
180
195
 
196
+ ## About the code
197
+
198
+ Eventhough this library has less than 100 LOC, it's split into four files, maybe a bit
199
+ of an overkill, though I like neat and tidy classes :)
200
+
201
+ lib/gravatarify.rb # loads the other files from lib/gravatarify
202
+ # and hooks the necessary modules into
203
+ # ActionView, ActiveRecord and DataMapper
204
+ # (if available)
205
+
206
+ lib/gravatarify/base.rb # Provides all logic required to generate
207
+ # gravatar.com urls from an email address.
208
+ # Check out Gravatarify::Base.build_gravatar_url,
209
+ # this is the absolute core method.
210
+
211
+ lib/gravatarify/object_support.rb # Module which (when) included provides the
212
+ # gravatarify class method to add a gravatar_url
213
+ # to any object.
214
+
215
+ lib/gravatarify/view_helper.rb # Defines rails view helpers.
216
+
181
217
  ## Licence
182
218
 
183
219
  Copyright (c) 2009 Lukas Westermann
data/Rakefile CHANGED
@@ -2,6 +2,10 @@ require 'rake'
2
2
  require 'rake/testtask'
3
3
  require 'yard'
4
4
 
5
+ def gravatarify_version
6
+ @gravatarify_version ||= (tmp = YAML.load(File.read('VERSION.yml'))) && [tmp[:major], tmp[:minor], tmp[:patch]] * '.'
7
+ end
8
+
5
9
  desc 'Default: run unit tests.'
6
10
  task :default => :test
7
11
 
@@ -18,7 +22,7 @@ YARD::Rake::YardocTask.new(:doc) do |t|
18
22
  t.files = ['lib/**/*.rb']
19
23
  t.options = [
20
24
  "--readme", "README.md",
21
- "--title", "Gravatarify API Documentation"
25
+ "--title", "gravatarify (v#{gravatarify_version}) API Documentation"
22
26
  ]
23
27
  end
24
28
 
@@ -26,10 +30,17 @@ begin
26
30
  require 'jeweler'
27
31
  Jeweler::Tasks.new do |gemspec|
28
32
  gemspec.name = "gravatarify"
29
- gemspec.summary = "Gravatar URLs for your ruby"
33
+ gemspec.summary = "Awesome gravatar support for ruby (and rails)."
34
+ description = <<-DESC
35
+ Yet another ruby/rails gravatar plugin - though, one with unique options like
36
+ `Proc`s for default images, or support for gravatar.com's multiple host names.
37
+ DESC
38
+ gemspec.description = description.strip
30
39
  gemspec.email = "lukas.westermann@gmail.com"
31
40
  gemspec.homepage = "http://github.com/lwe/gravatarify"
32
41
  gemspec.authors = ["Lukas Westermann"]
42
+ gemspec.licenses = %w{LICENSE}
43
+ gemspec.extra_rdoc_files = %w{README.md}
33
44
  end
34
45
  Jeweler::GemcutterTasks.new
35
46
  rescue LoadError
@@ -40,6 +51,7 @@ desc 'Clean all generated files (.yardoc and doc/*)'
40
51
  task :clean do |t|
41
52
  FileUtils.rm_rf "doc"
42
53
  FileUtils.rm_rf "pkg"
54
+ FileUtils.rm_rf "gravatarify.gemspec"
43
55
  FileUtils.rm_rf ".yardoc"
44
56
  end
45
57
 
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :patch: 0
3
- :major: 0
4
- :minor: 7
3
+ :major: 1
4
+ :minor: 0
@@ -2,9 +2,11 @@ require 'digest/md5'
2
2
  require 'cgi'
3
3
 
4
4
  module Gravatarify
5
+ # Subdomains used for balancing
6
+ GRAVATAR_SUBDOMAINS = %w{ 0 1 2 www }
5
7
 
6
- # Hosts used for balancing
7
- GRAVATAR_HOSTS = %w{ 0 1 2 www }
8
+ # Fallback if no subdomain is found
9
+ GRAVATAR_DEFAULT_SUBDOMAIN = 'www'
8
10
 
9
11
  # If no size is specified, gravatar.com returns 80x80px images
10
12
  GRAVATAR_DEFAULT_SIZE = 80
@@ -18,8 +20,43 @@ module Gravatarify
18
20
  # Hash of :ultra_long_option_name => 'abbrevated option'
19
21
  GRAVATAR_ABBREV_OPTIONS = { :default => 'd', :rating => 'r', :size => 's' }
20
22
 
21
- # Options which can be globally overriden by the application
22
- def self.options; @options ||= {} end
23
+ class << self
24
+ # Globally define options which are then merged on every call to
25
+ # +build_gravatar_url+, this is useful to e.g. define the default image.
26
+ #
27
+ # Setting global options should be done (for Rails apps) in an initializer:
28
+ #
29
+ # # set the default image using a Proc
30
+ # Gravatarify.options[:default] = Proc.new { |*args| "http://example.com/avatar-#{args.first[:size] || 80}.jpg" }
31
+ #
32
+ # # or set a custom default rating
33
+ # Gravatarify.options[:rating] = :R
34
+ #
35
+ def options; @options ||= {} end
36
+
37
+ # Globally overide subdomains used to build gravatar urls, normally
38
+ # +gravatarify+ picks from either +0.gravatar.com+, +1.gravatar.com+,
39
+ # +2.gravatar.com+ or +www.gravatar.com+ when building hosts, to use a custom
40
+ # set of subdomains (or none!) do something like:
41
+ #
42
+ # Gravatarify.subdomains = %w{ 0 www } # only use 0.gravatar.com and www.gravatar.com
43
+ #
44
+ # Gravatarify.subdomain = 'www' # only use www! (PS: subdomain= is an alias)
45
+ #
46
+ def subdomains=(subdomains) @subdomains = [*subdomains] end
47
+ alias_method :subdomain=, :subdomains=
48
+
49
+ # Shortcut method to reset subdomains to only build +www.gravatar.com+ urls,
50
+ # i.e. disable host balancing!
51
+ def use_www_only!; self.subdomains = %w{ www } end
52
+
53
+ # Access currently defined subdomains, defaults are +GRAVTAR_SUBDOMAINS+.
54
+ def subdomains; @subdomains ||= GRAVATAR_SUBDOMAINS end
55
+
56
+ # Get subdomain for supplied string or returns +GRAVATAR_DEFAULT_SUBDOMAIN+ if none is
57
+ # defined.
58
+ def subdomain(str); subdomains[str.hash % subdomains.size] || GRAVATAR_DEFAULT_SUBDOMAIN end
59
+ end
23
60
 
24
61
  # Provides core support to build gravatar urls based on supplied e-mail strings.
25
62
  module Base
@@ -70,7 +107,7 @@ module Gravatarify
70
107
  private
71
108
  def build_gravatar_host(str_hash, secure = false)
72
109
  secure = secure.call(self) if secure.respond_to?(:call)
73
- secure ? "https://secure.gravatar.com" : "http://#{GRAVATAR_HOSTS[str_hash.hash % GRAVATAR_HOSTS.size] || 'www'}.gravatar.com"
110
+ secure ? "https://secure.gravatar.com" : "http://#{Gravatarify.subdomain(str_hash)}.gravatar.com"
74
111
  end
75
112
 
76
113
  def build_gravatar_options(url_options = {})
@@ -1,3 +1,5 @@
1
+ # The view helper, especially for the +gravatar_tag+ helper, requires
2
+ # rails!
1
3
  module Gravatarify::ViewHelper
2
4
  include Gravatarify::Base
3
5
 
@@ -8,10 +8,7 @@ end
8
8
  class GravatarifyBaseTest < Test::Unit::TestCase
9
9
  include Gravatarify::Base
10
10
 
11
- def setup
12
- # just ensure that no global options are defined when starting next test
13
- Gravatarify.options.clear
14
- end
11
+ def setup; reset_gravatarify! end
15
12
 
16
13
  context "#build_gravatar_url, but without any options yet" do
17
14
  should "generate correct url for hash without options" do
@@ -4,7 +4,7 @@ begin; require 'dm-core'; rescue LoadError; end
4
4
  require 'gravatarify'
5
5
 
6
6
  class GravatarifyIntegrationTest < Test::Unit::TestCase
7
- def setup; Gravatarify.options.clear end
7
+ def setup; reset_gravatarify! end
8
8
 
9
9
  context "ActiveRecord::Base" do
10
10
  if defined?(ActiveRecord)
@@ -3,7 +3,7 @@ require 'gravatarify/base'
3
3
  require 'gravatarify/object_support'
4
4
 
5
5
  class GravatarifyObjectSupportTest < Test::Unit::TestCase
6
- def setup; Gravatarify.options.clear end
6
+ def setup; reset_gravatarify! end
7
7
 
8
8
  context "#gravatarify" do
9
9
  should "add support for #gravatar_url to POROs (plain old ruby objects, yeah POJO sounds better!)" do
@@ -0,0 +1,36 @@
1
+ require 'test_helper'
2
+ require 'gravatarify/base'
3
+
4
+ class GravatarifySubdomainTest < Test::Unit::TestCase
5
+ include Gravatarify::Base
6
+
7
+ def setup; reset_gravatarify! end
8
+
9
+ context "changing hosts through Gravatarify#subdomains" do
10
+ should "override default subdomains (useful to e.g. switch back to 'www' only)" do
11
+ Gravatarify.subdomains = ['0', '1']
12
+ assert_equal "http://0.gravatar.com/avatar/4979dd9653e759c78a81d4997f56bae2.jpg", build_gravatar_url('info@initech.com')
13
+ assert_equal "http://1.gravatar.com/avatar/d4489907918035d0bc6ff3f6c76e760d.jpg", build_gravatar_url('support@initech.com')
14
+ end
15
+
16
+ should "take in a string only argument, like www (and be aliased to 'subdomain' to singularize it :D)" do
17
+ Gravatarify.subdomain = 'www'
18
+ assert_equal "http://www.gravatar.com/avatar/4979dd9653e759c78a81d4997f56bae2.jpg", build_gravatar_url('info@initech.com')
19
+ assert_equal "http://www.gravatar.com/avatar/d4489907918035d0bc6ff3f6c76e760d.jpg", build_gravatar_url('support@initech.com')
20
+ end
21
+
22
+ should "still work as expected if passed in `nil` and return urls with default subdomain `www`" do
23
+ Gravatarify.subdomain = nil
24
+ assert_equal "http://www.gravatar.com/avatar/4979dd9653e759c78a81d4997f56bae2.jpg", build_gravatar_url('info@initech.com')
25
+ assert_equal "http://www.gravatar.com/avatar/d4489907918035d0bc6ff3f6c76e760d.jpg", build_gravatar_url('support@initech.com')
26
+ end
27
+ end
28
+
29
+ context "Gravatarify#use_only_www!" do
30
+ should "only generate www.gravatar.com urls" do
31
+ Gravatarify.use_www_only!
32
+ assert_equal "http://www.gravatar.com/avatar/4979dd9653e759c78a81d4997f56bae2.jpg", build_gravatar_url('info@initech.com')
33
+ assert_equal "http://www.gravatar.com/avatar/d4489907918035d0bc6ff3f6c76e760d.jpg", build_gravatar_url('support@initech.com')
34
+ end
35
+ end
36
+ end
@@ -1,6 +1,7 @@
1
1
  require 'test_helper'
2
2
  require 'active_support'
3
3
  require 'action_view/helpers'
4
+ require 'gravatarify'
4
5
  require 'gravatarify/view_helper'
5
6
 
6
7
  class GravatarifyViewHelperTest < Test::Unit::TestCase
@@ -9,7 +10,7 @@ class GravatarifyViewHelperTest < Test::Unit::TestCase
9
10
 
10
11
  def setup
11
12
  # just ensure that no global options are defined when starting next test
12
- Gravatarify.options.clear
13
+ reset_gravatarify!
13
14
  end
14
15
 
15
16
  context "#gravatar_tag helper" do
data/test/test_helper.rb CHANGED
@@ -4,3 +4,9 @@ require 'shoulda'
4
4
  require 'rr'
5
5
 
6
6
  Test::Unit::TestCase.send :include, RR::Adapters::TestUnit
7
+
8
+ # Reset +Gravatarify+ to default hosts and cleared options
9
+ def reset_gravatarify!
10
+ Gravatarify.options.clear
11
+ Gravatarify.subdomains = Gravatarify::GRAVATAR_SUBDOMAINS
12
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gravatarify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lukas Westermann
@@ -9,18 +9,19 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-09-29 00:00:00 +02:00
12
+ date: 2009-10-02 00:00:00 +02:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
16
- description:
16
+ description: |-
17
+ Yet another ruby/rails gravatar plugin - though, one with unique options like
18
+ `Proc`s for default images, or support for gravatar.com's multiple host names.
17
19
  email: lukas.westermann@gmail.com
18
20
  executables: []
19
21
 
20
22
  extensions: []
21
23
 
22
24
  extra_rdoc_files:
23
- - LICENSE
24
25
  - README.md
25
26
  files:
26
27
  - .gitignore
@@ -36,12 +37,13 @@ files:
36
37
  - test/gravatarify_base_test.rb
37
38
  - test/gravatarify_integration_test.rb
38
39
  - test/gravatarify_object_support_test.rb
40
+ - test/gravatarify_subdomain_test.rb
39
41
  - test/gravatarify_view_helper_test.rb
40
42
  - test/test_helper.rb
41
43
  has_rdoc: true
42
44
  homepage: http://github.com/lwe/gravatarify
43
- licenses: []
44
-
45
+ licenses:
46
+ - LICENSE
45
47
  post_install_message:
46
48
  rdoc_options:
47
49
  - --charset=UTF-8
@@ -65,10 +67,11 @@ rubyforge_project:
65
67
  rubygems_version: 1.3.5
66
68
  signing_key:
67
69
  specification_version: 3
68
- summary: Gravatar URLs for your ruby
70
+ summary: Awesome gravatar support for ruby (and rails).
69
71
  test_files:
70
72
  - test/gravatarify_base_test.rb
71
73
  - test/gravatarify_integration_test.rb
72
74
  - test/gravatarify_object_support_test.rb
75
+ - test/gravatarify_subdomain_test.rb
73
76
  - test/gravatarify_view_helper_test.rb
74
77
  - test/test_helper.rb