gravatarify 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2009 Lukas Westermann, Zurich (CHE)
1
+ Copyright (c) 2009 Lukas Westermann
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -8,9 +8,24 @@ Best of it? It works with Rails, probably Merb and even plain old Ruby :)
8
8
 
9
9
  ## Install
10
10
 
11
- TODO: need to gemify it...
11
+ Just install the gem (ensure you have gemcutter in your sources!)
12
12
 
13
- ## Using the view helpers
13
+ sudo gem install gravatarify
14
+
15
+ Ready to go! Using Rails? Either add (to `config/environment.rb`):
16
+
17
+ config.gem 'gravatarify', :source => 'http://gemcutter.org'
18
+
19
+ or install as Rails plugin:
20
+
21
+ ./script/plugin install git://github.com/lwe/gravatarify.git
22
+
23
+ If the gem is available, just require it (for your simple ruby apps):
24
+
25
+ require 'rubygems'
26
+ require 'gravatarify'
27
+
28
+ ## Using the view helpers (Rails only!)
14
29
 
15
30
  Probably one of the easiest ways to add support for gravatar images is with the included view helpers:
16
31
 
@@ -79,3 +94,109 @@ class:
79
94
  Tadaaa! Works exactly like the model helpers, so it's now possible to call `gravatar_url` on instances
80
95
  of `PoroUser`.
81
96
 
97
+ ## Back to the roots?
98
+
99
+ No need for sophisticated stuff like view helpers and ActiveRecord integration, want to go back to the roots?
100
+ Then feel free to use `Gravatarify::Base#build_gravatar_url` directly.
101
+
102
+ For example, want to use `build_gravatar_url` in a Sinatra app?
103
+
104
+ helpers Gravatarify::Base
105
+
106
+ Yeah, that should work :). See {Gravatarify::Base#build_gravatar_url} for more informations and usage examples.
107
+
108
+ ## Need more control?
109
+
110
+ <table>
111
+ <tr>
112
+ <th>Option</th>
113
+ <th>Type</th>
114
+ <th>Description</th>
115
+ <th>Default</th>
116
+ </tr>
117
+ <tr>
118
+ <td><tt>:default</tt></td>
119
+ <td>String, Proc</td>
120
+ <td>Fully qualified URL to an image, which is used if gravatar.com has no image for the supplied email.
121
+ <tt>Proc</tt>s can be used to return e.g. an image based on the request size (see Advanced stuff).
122
+ Furthermore gravatar.com provides several "special values" which generate icons, these are "wavatar",
123
+ "monsterid" and "identicon", finally if set to <tt>404</tt> gravatar.com returns the <tt>HTTP 404 Not Found</tt> error.
124
+ If nothing is specified gravatar.com returns it's gravatar icon.
125
+ </td>
126
+ <td>-</td>
127
+ </tr>
128
+ <tr>
129
+ <td><tt>:rating</tt></td>
130
+ <td>String, Symbol</td>
131
+ <td>Each avatar at gravatar.com has a rating associated (which is based on MPAAs rating system), valid values are:<br/>
132
+ <b>g</b> - general audiences, <b>pg</b> - parental guidance suggested, <b>r</b> - restricted and <b>x</b> - x-rated :).
133
+ Gravatar.com returns <b>g</b>-rated avatars, unless anything else is specified.
134
+ </td>
135
+ <td>-</td>
136
+ </tr>
137
+ <tr>
138
+ <td><tt>:size</tt></td>
139
+ <td>Integer</td>
140
+ <td>Avatars are square, so <tt>:size</tt> defines the length of the sides in pixel, if nothing is specified gravatar.com
141
+ returns 80x80px images.</td>
142
+ <td>-</td>
143
+ </tr>
144
+ <tr>
145
+ <td><tt>:secure</tt></td>
146
+ <td>Boolean, Proc</td>
147
+ <td>If set to <tt>true</tt> gravatars secure host (<i>https://secure.gravatar.com/</i>) is used to serve the avatars
148
+ from. Can be a Proc to inflect wheter or not to use the secure host based on request parameters.</td>
149
+ <td><tt>false</tt></td>
150
+ </tr>
151
+ <tr>
152
+ <td><tt>:filetype</tt></td>
153
+ <td>String, Symbol</td>
154
+ <td>Change image type, gravatar.com supports <tt>:gif</tt>, <tt>:png</tt> and <tt>:jpg</tt>.</td>
155
+ <td><tt>:jpg</tt></td>
156
+ </tr>
157
+ </table>
158
+
159
+ ## Not yet enough?
160
+
161
+ The `:default` option can be passed in a `Proc`, so this is certainly useful to for example
162
+ to generate an image url, based on the request size:
163
+
164
+ # in an initializer
165
+ Gravatarify.options[:default] = Proc.new do |options, object|
166
+ "http://example.com/avatar-#{options[:size] || 80}.jpg"
167
+ end
168
+
169
+ # now each time a gravatar url is generated, the Proc is evaluated:
170
+ @user.gravatar_url
171
+ # => "http://0.gravatar.com/...jpg?d=http%3A%2F%2Fexample.com%2Fgravatar-80.jpg"
172
+ @user.gravatar_url(:size => 16)
173
+ # => "http://0.gravatar.com/...jpg?d=http%3A%2F%2Fexample.com%2Fgravatar-16.jpg&s=16"
174
+
175
+ Into the block is passed the options hash and as second parameter the object itself, so in the example above
176
+ `object` would be `@user`, might be useful!? Never used it, so I might remove the second argument...
177
+
178
+ Not only the `:default` option accepts a Proc, but also `:secure`, can be useful to handle cases where
179
+ it should evaluate against `request.ssl?` for example.
180
+
181
+ ## Licence
182
+
183
+ Copyright (c) 2009 Lukas Westermann
184
+
185
+ Permission is hereby granted, free of charge, to any person obtaining
186
+ a copy of this software and associated documentation files (the
187
+ "Software"), to deal in the Software without restriction, including
188
+ without limitation the rights to use, copy, modify, merge, publish,
189
+ distribute, sublicense, and/or sell copies of the Software, and to
190
+ permit persons to whom the Software is furnished to do so, subject to
191
+ the following conditions:
192
+
193
+ The above copyright notice and this permission notice shall be
194
+ included in all copies or substantial portions of the Software.
195
+
196
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
197
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
198
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
199
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
200
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
201
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
202
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :patch: 0
3
3
  :major: 0
4
- :minor: 6
4
+ :minor: 7
@@ -10,7 +10,7 @@ module Gravatarify
10
10
  GRAVATAR_DEFAULT_SIZE = 80
11
11
 
12
12
  # Default filetype is JPG
13
- GRAVATAR_DEFAULT_FILETYPE = 'jpg'
13
+ GRAVATAR_DEFAULT_FILETYPE = :jpg
14
14
 
15
15
  # List of known and valid gravatar options (includes shortened options).
16
16
  GRAVATAR_OPTIONS = [ :default, :d, :rating, :r, :size, :s, :secure, :filetype ]
@@ -69,7 +69,7 @@ module Gravatarify
69
69
 
70
70
  private
71
71
  def build_gravatar_host(str_hash, secure = false)
72
- secure = secure.call(respond_to?(:request) ? request : nil) if secure.respond_to?(:call)
72
+ secure = secure.call(self) if secure.respond_to?(:call)
73
73
  secure ? "https://secure.gravatar.com" : "http://#{GRAVATAR_HOSTS[str_hash.hash % GRAVATAR_HOSTS.size] || 'www'}.gravatar.com"
74
74
  end
75
75
 
@@ -77,7 +77,7 @@ module Gravatarify
77
77
  params = []
78
78
  url_options.each_pair do |key, value|
79
79
  key = GRAVATAR_ABBREV_OPTIONS[key] if GRAVATAR_ABBREV_OPTIONS.include?(key) # shorten key!
80
- value = value.call(url_options) if key.to_s == 'd' and value.respond_to?(:call)
80
+ value = value.call(url_options, self) if key.to_s == 'd' and value.respond_to?(:call)
81
81
  params << "#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}" if value
82
82
  end
83
83
  "?#{params.sort * '&'}" unless params.empty?
@@ -55,7 +55,7 @@ class GravatarifyBaseTest < Test::Unit::TestCase
55
55
  end
56
56
 
57
57
  should "handle Procs as :default, to easily generate default urls based on supplied :size" do
58
- default = Proc.new { |o| "http://example.com/gravatar#{o[:size] ? '-' + o[:size].to_s : ''}.jpg" }
58
+ default = Proc.new { |*args| "http://example.com/gravatar#{args.first[:size] ? '-' + args.first[:size].to_s : ''}.jpg" }
59
59
  assert_equal "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg?d=http%3A%2F%2Fexample.com%2Fgravatar.jpg",
60
60
  build_gravatar_url('bella@gmail.com', :default => default)
61
61
  assert_equal "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg?d=http%3A%2F%2Fexample.com%2Fgravatar-25.jpg&s=25",
@@ -104,7 +104,7 @@ class GravatarifyBaseTest < Test::Unit::TestCase
104
104
  end
105
105
 
106
106
  should "allow Procs for :secure option, enables pretty cool stuff for stuff like request.ssl?" do
107
- Gravatarify.options[:secure] = Proc.new { |request| request.respond_to?(:ssl?) ? request.ssl? : false }
107
+ Gravatarify.options[:secure] = Proc.new { |obj| obj.request.ssl? }
108
108
 
109
109
  mock_ssl = MockView.new
110
110
  mock(mock_ssl).request.stub!.ssl? { true }
@@ -88,7 +88,7 @@ class GravatarifyObjectSupportTest < Test::Unit::TestCase
88
88
  should "allow multiple sources to be defined, yet still handle options!" do
89
89
  poro = Class.new do
90
90
  include Gravatarify::ObjectSupport
91
- gravatarify :email, :employee_email, :default => Proc.new { |o| "http://initech.com/avatar-#{o[:size] || 80}.jpg" }
91
+ gravatarify :email, :employee_email, :default => Proc.new { |*args| "http://initech.com/avatar-#{args.first[:size] || 80}.jpg" }
92
92
  def email; "info@initech.com" end
93
93
  def employee_email; "peter.gibbons@initech.com" end
94
94
  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.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lukas Westermann