gravatar-ultimate 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (127) hide show
  1. data/.gitignore +13 -0
  2. data/.travis.yml +16 -0
  3. data/Gemfile +6 -0
  4. data/Gemfile.lock +37 -0
  5. data/README.rdoc +19 -25
  6. data/Rakefile +9 -38
  7. data/environments/rails-2.3/README +243 -0
  8. data/environments/rails-2.3/Rakefile +10 -0
  9. data/environments/rails-2.3/app/controllers/application_controller.rb +10 -0
  10. data/environments/rails-2.3/app/helpers/application_helper.rb +3 -0
  11. data/environments/rails-2.3/config/boot.rb +114 -0
  12. data/environments/rails-2.3/config/database.yml +22 -0
  13. data/environments/rails-2.3/config/environment.rb +43 -0
  14. data/environments/rails-2.3/config/environments/development.rb +17 -0
  15. data/environments/rails-2.3/config/environments/production.rb +28 -0
  16. data/environments/rails-2.3/config/environments/test.rb +28 -0
  17. data/environments/rails-2.3/config/initializers/backtrace_silencers.rb +7 -0
  18. data/environments/rails-2.3/config/initializers/cookie_verification_secret.rb +7 -0
  19. data/environments/rails-2.3/config/initializers/inflections.rb +10 -0
  20. data/environments/rails-2.3/config/initializers/mime_types.rb +5 -0
  21. data/environments/rails-2.3/config/initializers/new_rails_defaults.rb +21 -0
  22. data/environments/rails-2.3/config/initializers/session_store.rb +15 -0
  23. data/environments/rails-2.3/config/locales/en.yml +5 -0
  24. data/environments/rails-2.3/config/routes.rb +43 -0
  25. data/environments/rails-2.3/db/seeds.rb +7 -0
  26. data/environments/rails-2.3/doc/README_FOR_APP +2 -0
  27. data/environments/rails-2.3/public/404.html +30 -0
  28. data/environments/rails-2.3/public/422.html +30 -0
  29. data/environments/rails-2.3/public/500.html +30 -0
  30. data/environments/rails-2.3/public/favicon.ico +0 -0
  31. data/environments/rails-2.3/public/images/rails.png +0 -0
  32. data/environments/rails-2.3/public/index.html +275 -0
  33. data/environments/rails-2.3/public/javascripts/application.js +2 -0
  34. data/environments/rails-2.3/public/javascripts/controls.js +963 -0
  35. data/environments/rails-2.3/public/javascripts/dragdrop.js +973 -0
  36. data/environments/rails-2.3/public/javascripts/effects.js +1128 -0
  37. data/environments/rails-2.3/public/javascripts/prototype.js +4320 -0
  38. data/environments/rails-2.3/public/robots.txt +5 -0
  39. data/environments/rails-2.3/script/about +4 -0
  40. data/environments/rails-2.3/script/console +3 -0
  41. data/environments/rails-2.3/script/dbconsole +3 -0
  42. data/environments/rails-2.3/script/destroy +3 -0
  43. data/environments/rails-2.3/script/generate +3 -0
  44. data/environments/rails-2.3/script/performance/benchmarker +3 -0
  45. data/environments/rails-2.3/script/performance/profiler +3 -0
  46. data/environments/rails-2.3/script/plugin +3 -0
  47. data/environments/rails-2.3/script/runner +3 -0
  48. data/environments/rails-2.3/script/server +3 -0
  49. data/environments/rails-2.3/test/performance/browsing_test.rb +9 -0
  50. data/environments/rails-2.3/test/test_helper.rb +38 -0
  51. data/environments/rails-3.1/.gitignore +15 -0
  52. data/environments/rails-3.1/README +261 -0
  53. data/environments/rails-3.1/Rakefile +7 -0
  54. data/environments/rails-3.1/app/assets/images/rails.png +0 -0
  55. data/environments/rails-3.1/app/assets/javascripts/application.js +9 -0
  56. data/environments/rails-3.1/app/assets/stylesheets/application.css +7 -0
  57. data/environments/rails-3.1/app/controllers/application_controller.rb +3 -0
  58. data/environments/rails-3.1/app/helpers/application_helper.rb +2 -0
  59. data/environments/rails-3.1/app/mailers/.gitkeep +0 -0
  60. data/environments/rails-3.1/app/models/.gitkeep +0 -0
  61. data/environments/rails-3.1/app/views/layouts/application.html.erb +14 -0
  62. data/environments/rails-3.1/config.ru +4 -0
  63. data/environments/rails-3.1/config/application.rb +48 -0
  64. data/environments/rails-3.1/config/boot.rb +6 -0
  65. data/environments/rails-3.1/config/database.yml +25 -0
  66. data/environments/rails-3.1/config/environment.rb +5 -0
  67. data/environments/rails-3.1/config/environments/development.rb +30 -0
  68. data/environments/rails-3.1/config/environments/production.rb +60 -0
  69. data/environments/rails-3.1/config/environments/test.rb +39 -0
  70. data/environments/rails-3.1/config/initializers/backtrace_silencers.rb +7 -0
  71. data/environments/rails-3.1/config/initializers/inflections.rb +10 -0
  72. data/environments/rails-3.1/config/initializers/mime_types.rb +5 -0
  73. data/environments/rails-3.1/config/initializers/secret_token.rb +7 -0
  74. data/environments/rails-3.1/config/initializers/session_store.rb +8 -0
  75. data/environments/rails-3.1/config/initializers/wrap_parameters.rb +14 -0
  76. data/environments/rails-3.1/config/locales/en.yml +5 -0
  77. data/environments/rails-3.1/config/routes.rb +58 -0
  78. data/environments/rails-3.1/db/seeds.rb +7 -0
  79. data/environments/rails-3.1/doc/README_FOR_APP +2 -0
  80. data/environments/rails-3.1/lib/assets/.gitkeep +0 -0
  81. data/environments/rails-3.1/lib/tasks/.gitkeep +0 -0
  82. data/environments/rails-3.1/public/404.html +26 -0
  83. data/environments/rails-3.1/public/422.html +26 -0
  84. data/environments/rails-3.1/public/500.html +26 -0
  85. data/environments/rails-3.1/public/favicon.ico +0 -0
  86. data/environments/rails-3.1/public/index.html +241 -0
  87. data/environments/rails-3.1/public/robots.txt +5 -0
  88. data/environments/rails-3.1/script/rails +6 -0
  89. data/environments/rails-3.1/test/fixtures/.gitkeep +0 -0
  90. data/environments/rails-3.1/test/functional/.gitkeep +0 -0
  91. data/environments/rails-3.1/test/integration/.gitkeep +0 -0
  92. data/environments/rails-3.1/test/performance/browsing_test.rb +12 -0
  93. data/environments/rails-3.1/test/test_helper.rb +13 -0
  94. data/environments/rails-3.1/test/unit/.gitkeep +0 -0
  95. data/environments/rails-3.1/vendor/assets/stylesheets/.gitkeep +0 -0
  96. data/environments/rails-3.1/vendor/plugins/.gitkeep +0 -0
  97. data/environments/rails.rb +26 -0
  98. data/gemfiles/rails-2.3.14 +5 -0
  99. data/gemfiles/rails-2.3.14.lock +55 -0
  100. data/gemfiles/rails-3.1.3 +5 -0
  101. data/gemfiles/rails-3.1.3.lock +110 -0
  102. data/gravatar-ultimate.gemspec +13 -53
  103. data/lib/gravatar.rb +68 -30
  104. data/lib/gravatar/cache.rb +27 -21
  105. data/lib/gravatar/dependencies.rb +1 -18
  106. data/lib/gravatar/test_case.rb +105 -0
  107. data/lib/gravatar/version.rb +8 -0
  108. data/spec/fixtures/image.jpg +0 -0
  109. data/spec/fixtures/responses/grav.addresses +24 -0
  110. data/spec/fixtures/responses/grav.deleteUserimage +18 -0
  111. data/spec/fixtures/responses/grav.exists +20 -0
  112. data/spec/fixtures/responses/grav.exists.invalid +20 -0
  113. data/spec/fixtures/responses/grav.exists.multiple_invalid +21 -0
  114. data/spec/fixtures/responses/grav.removeImage +20 -0
  115. data/spec/fixtures/responses/grav.saveData +18 -0
  116. data/spec/fixtures/responses/grav.saveUrl +18 -0
  117. data/spec/fixtures/responses/grav.test +22 -0
  118. data/spec/fixtures/responses/grav.useUserimage +20 -0
  119. data/spec/fixtures/responses/grav.userimages +27 -0
  120. data/spec/lib/gravatar/cache_setup_spec.rb +2 -0
  121. data/spec/lib/gravatar/{cache_and_logger_spec.rb → cache_spec.rb} +12 -1
  122. data/spec/lib/gravatar/test_case_spec.rb +40 -0
  123. data/spec/lib/gravatar_spec.rb +97 -61
  124. data/spec/spec_helper.rb +37 -9
  125. metadata +208 -38
  126. data/VERSION +0 -1
  127. data/spec/lib/gravatar/dependencies_spec.rb +0 -33
@@ -1,13 +1,11 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
1
  # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "gravatar/version"
5
4
 
6
5
  Gem::Specification.new do |s|
7
6
  s.name = %q{gravatar-ultimate}
8
- s.version = "1.0.2"
7
+ s.version = Gravatar::VERSION
9
8
 
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
9
  s.authors = ["Colin MacKenzie IV"]
12
10
  s.date = %q{2010-08-30}
13
11
  s.description = %q{The Ultimate Gravatar Gem!
@@ -20,58 +18,20 @@ rather than just a URL to that data. This saves you the extra step of having to
20
18
  "LICENSE",
21
19
  "README.rdoc"
22
20
  ]
23
- s.files = [
24
- ".document",
25
- ".gitignore",
26
- "LICENSE",
27
- "README.rdoc",
28
- "Rakefile",
29
- "VERSION",
30
- "gravatar-ultimate.gemspec",
31
- "lib/gravatar-ultimate.rb",
32
- "lib/gravatar.rb",
33
- "lib/gravatar/cache.rb",
34
- "lib/gravatar/dependencies.rb",
35
- "lib/gravatar_ultimate.rb",
36
- "spec/credentials.yml.example",
37
- "spec/fixtures/image.jpg",
38
- "spec/lib/gravatar/cache_and_logger_spec.rb",
39
- "spec/lib/gravatar/cache_setup_spec.rb",
40
- "spec/lib/gravatar/dependencies_spec.rb",
41
- "spec/lib/gravatar_spec.rb",
42
- "spec/spec.opts",
43
- "spec/spec_helper.rb"
44
- ]
21
+ s.files = `git ls-files`.split("\n")
45
22
  s.homepage = %q{http://www.thoughtsincomputation.com/}
46
23
  s.rdoc_options = ["--charset=UTF-8"]
47
24
  s.require_paths = ["lib"]
48
25
  s.rubygems_version = %q{1.3.7}
49
26
  s.summary = %q{A gem for interfacing with the entire Gravatar API: not just images, but the XML-RPC API too!}
50
- s.test_files = [
51
- "spec/spec_helper.rb",
52
- "spec/lib/gravatar_spec.rb",
53
- "spec/lib/gravatar/dependencies_spec.rb",
54
- "spec/lib/gravatar/cache_and_logger_spec.rb",
55
- "spec/lib/gravatar/cache_setup_spec.rb"
56
- ]
27
+ s.test_files = `git ls-files -- spec/*`.split("\n")
28
+ s.executables = `git ls-files -- bin/*`.split("\n")
57
29
 
58
- if s.respond_to? :specification_version then
59
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
60
- s.specification_version = 3
61
-
62
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
63
- s.add_runtime_dependency(%q<sc-core-ext>, [">= 1.2.0"])
64
- s.add_development_dependency(%q<rspec>, [">= 1.3.0"])
65
- s.add_development_dependency(%q<fakeweb>, [">= 1.2.8"])
66
- else
67
- s.add_dependency(%q<sc-core-ext>, [">= 1.2.0"])
68
- s.add_dependency(%q<rspec>, [">= 1.3.0"])
69
- s.add_dependency(%q<fakeweb>, [">= 1.2.8"])
70
- end
71
- else
72
- s.add_dependency(%q<sc-core-ext>, [">= 1.2.0"])
73
- s.add_dependency(%q<rspec>, [">= 1.3.0"])
74
- s.add_dependency(%q<fakeweb>, [">= 1.2.8"])
75
- end
30
+ # s.add_runtime_dependency('sc-core-ext', ">= 1.2.0")
31
+ s.add_development_dependency('rspec', ">= 1.3.0")
32
+ s.add_development_dependency('fakeweb', ">= 1.2.8")
33
+ s.add_development_dependency('activesupport', '>= 2.3.14')
34
+ s.add_development_dependency('i18n', '~> 0.6.0')
35
+ s.add_development_dependency('rake', '~> 0.9.2.2')
36
+ s.add_development_dependency('rdoc', '~> 3.11')
76
37
  end
77
-
@@ -1,3 +1,5 @@
1
+ require 'active_support'
2
+ require 'active_support/core_ext'
1
3
  require File.expand_path('../gravatar/dependencies', __FILE__)
2
4
  require File.expand_path("../gravatar/cache", __FILE__)
3
5
 
@@ -14,7 +16,15 @@ require File.expand_path("../gravatar/cache", __FILE__)
14
16
  # -100 Misc error (see text)
15
17
  #
16
18
  class Gravatar
17
- attr_reader :email
19
+ API_METHODS = [
20
+ :exists?, :addresses, :user_images, :save_data!, :save_image!, :save_url!, :use_image!, :use_user_image!,
21
+ :remove_image!, :delete_user_image!, :test, :image_url, :image_data
22
+ ]
23
+ autoload :TestCase, File.expand_path('gravatar/test_case', File.dirname(__FILE__))
24
+
25
+ attr_reader :email, :api
26
+
27
+ delegate :rescue_errors, :rescue_errors=, :to => :cache
18
28
 
19
29
  # Creates a new instance of Gravatar. Valid options include:
20
30
  # :password => the password for this account, to be used instead of :api_key (don't supply both)
@@ -29,24 +39,35 @@ class Gravatar
29
39
  raise ArgumentError, "Expected :email" unless email
30
40
  @options = options || {}
31
41
  @email = email
32
-
42
+
33
43
  pw_or_key = auth.keys.first || :none
34
44
  @cache = Gravatar::Cache.new(self.class.cache, options[:duration] || self.class.duration,
35
45
  "gravatar-#{email_hash}-#{pw_or_key}", options[:logger] || self.class.logger)
46
+ self.rescue_errors = options[:rescue_errors]
36
47
 
37
- if !auth.empty?
38
- @api = XMLRPC::Client.new("secure.gravatar.com", "/xmlrpc?user=#{email_hash}", 443, nil, nil, nil, nil, true)
39
- end
48
+ self.auth_with auth unless auth.empty?
49
+ end
50
+
51
+ def host
52
+ "secure.gravatar.com"
53
+ end
54
+
55
+ def url
56
+ File.join("https://#{host}", path)
57
+ end
58
+
59
+ def path
60
+ "/xmlrpc?user=#{email_hash}"
40
61
  end
41
62
 
42
63
  # The duration of the cache for this instance of Gravatar, independent of any other instance
43
64
  def cache_duration
44
- @cache.duration
65
+ cache.duration
45
66
  end
46
67
 
47
68
  # Sets the duration of the cache for this instance of Gravatar, independent of any other instance
48
69
  def cache_duration=(time)
49
- @cache.duration = time
70
+ cache.duration = time
50
71
  end
51
72
 
52
73
  # Check whether one or more email addresses have corresponding avatars. If no email addresses are
@@ -85,7 +106,7 @@ class Gravatar
85
106
  end
86
107
  end
87
108
  end
88
-
109
+
89
110
  # Returns a hash of user images for this account in the following format:
90
111
  # { user_img_hash => [rating, url] }
91
112
  #
@@ -120,12 +141,11 @@ class Gravatar
120
141
  # This method is not cached.
121
142
  #
122
143
  # This method will clear out the cache, since it may have an effect on what the API methods respond with.
123
- def use_user_image!(image_hash, *email_addresses)
124
- hashed_email_addresses = normalize_email_addresses(email_addresses)
125
- hash = call('grav.useUserimage', :userimage => image_hash, :addresses => hashed_email_addresses)
126
- dehashify_emails(hash, email_addresses) { |value| boolean(value) }.tap do
127
- expire_cache!
128
- end
144
+ def use_user_image!(image_hash, emails)
145
+ emails = [emails] unless emails.is_a?(Array)
146
+ hash = call('grav.useUserimage', :userimage => image_hash, :addresses => emails)
147
+ expire_cache!
148
+ return hash
129
149
  end
130
150
  alias use_image! use_user_image!
131
151
 
@@ -136,12 +156,11 @@ class Gravatar
136
156
  # This method is not cached.
137
157
  #
138
158
  # This method will clear out the cache, since it may have an effect on what the API methods respond with.
139
- def remove_image!(*emails)
140
- hashed_email_addresses = normalize_email_addresses(emails)
141
- hash = call('grav.removeImage', :addresses => hashed_email_addresses)
142
- dehashify_emails(hash, emails) { |value| boolean(value) }.tap do
143
- expire_cache!
144
- end
159
+ def remove_image!(emails)
160
+ emails = [emails] unless emails.is_a?(Array)
161
+ hash = call('grav.removeImage', :addresses => emails)
162
+ expire_cache!
163
+ return hash
145
164
  end
146
165
 
147
166
  # Remove a userimage from the account and any email addresses with which it is associated. Returns
@@ -175,7 +194,9 @@ class Gravatar
175
194
  # :default or :d a default URL for this image to display if the specified user has no image;
176
195
  # or this can be one of [ :identicon, :monsterid, :wavatar, 404 ]. By default a generic
177
196
  # Gravatar image URL will be returned.
178
- # :filetype an extension such as :jpg or :png. Default is omitted.
197
+ # :filetype or :ext an extension such as :jpg or :png. Default is omitted.
198
+ # :forcedefault or :f force a default image and ignore the user's specified image. Can be one of
199
+ # [ :identicon, :monsterid, :wavatar, 404 ].
179
200
  #
180
201
  # See http://en.gravatar.com/site/implement/url for much more detailed information.
181
202
  def image_url(options = {})
@@ -195,17 +216,18 @@ class Gravatar
195
216
  cache(url) { OpenURI.open_uri(URI.parse(url)).read }
196
217
  end
197
218
 
198
- def self.version
199
- @version ||= File.read(File.join(File.dirname(__FILE__), "../VERSION")).chomp
200
- end
201
-
202
- private
219
+ # If no arguments are given, the cache object for this instance is returned. Otherwise, the arguments
220
+ # are passed into Gravatar::Cache#cache.
203
221
  def cache(*key, &block)
204
- @cache.call(*key, &block)
222
+ if key.empty? and not block_given?
223
+ @cache
224
+ else
225
+ @cache.call(*key, &block)
226
+ end
205
227
  end
206
228
 
207
229
  def expire_cache!
208
- @cache.clear!
230
+ cache.clear!
209
231
  end
210
232
 
211
233
  def dehashify_emails(response, emails)
@@ -226,10 +248,12 @@ class Gravatar
226
248
 
227
249
  def rating(i)
228
250
  case i
251
+ when -1, '-1' then :unknown
229
252
  when 0, '0' then :g
230
253
  when 1, '1' then :pg
231
254
  when 2, '2' then :r
232
255
  when 3, '3' then :x
256
+ when :unknown then -1
233
257
  when :g then 0
234
258
  when :pg then 1
235
259
  when :r then 2
@@ -244,10 +268,23 @@ class Gravatar
244
268
  end
245
269
 
246
270
  def call(name, args_hash = {})
271
+ raise "No authentication data given" unless @api
247
272
  r = @api.call(name, auth.merge(args_hash))
248
273
  r = r.with_indifferent_access if r.kind_of?(Hash)
249
274
  r
250
275
  end
276
+
277
+ # Authenticates with the given API key or password, returning self.
278
+ def auth_with(options)
279
+ @options.delete(:apikey)
280
+ @options.delete(:api_key)
281
+ @options.delete(:key)
282
+ @options.merge! options
283
+ if !auth.empty?
284
+ @api = XMLRPC::Client.new(host, path, 443, nil, nil, nil, nil, true)
285
+ end
286
+ self
287
+ end
251
288
 
252
289
  def auth
253
290
  api_key ? {:apikey => api_key} : (password ? {:password => password} : {})
@@ -267,7 +304,7 @@ class Gravatar
267
304
 
268
305
  def query_for_image(options)
269
306
  query = ''
270
- [:rating, :size, :default, :r, :s, :d].each do |key|
307
+ [:rating, :size, :default, :forcedefault, :r, :s, :d, :f].each do |key|
271
308
  if options.key?(key)
272
309
  query.blank? ? query.concat("?") : query.concat("&")
273
310
  query.concat("#{key}=#{CGI::escape options[key].to_s}")
@@ -277,6 +314,7 @@ class Gravatar
277
314
  end
278
315
 
279
316
  def extension_for_image(options)
280
- options.key?(:filetype) ? "." + (options[:filetype] || "jpg").to_s : ""
317
+ options.key?(:filetype) || options.key?(:ext) ? "." + (options[:filetype] || options[:ext] || "jpg").to_s : ""
281
318
  end
282
319
  end
320
+
@@ -3,12 +3,18 @@ class Gravatar
3
3
  class Cache
4
4
  attr_reader :real_cache, :namespace
5
5
  attr_accessor :duration, :logger
6
+
7
+ # If true, any errors encountered while communicating with the server will be rescued
8
+ # and the error message will be written to #logger, then the cached copy of the result
9
+ # (if any) will be returned. If false, the error will be raised.
10
+ attr_accessor :rescue_errors
6
11
 
7
12
  def initialize(real_cache, duration, namespace = nil, logger = Gravatar.logger)
8
13
  @duration = duration
9
14
  @real_cache = real_cache
10
15
  @namespace = namespace
11
16
  @logger = logger
17
+ @rescue_errors = true
12
18
  end
13
19
 
14
20
  # Provide a series of arguments to be used as a cache key, and a block to be executed when the cache
@@ -20,17 +26,19 @@ class Gravatar
20
26
  #
21
27
  def call(*key, &block)
22
28
  cached_copy = read_cache(*key)
29
+ cached_copy &&= cached_copy[:object]
30
+
23
31
  if expired?(*key) && block_given?
24
32
  begin
25
- returning(yield) do |object|
33
+ yield.tap do |object|
26
34
  write_cache(object, *key)
27
35
  end
28
36
  rescue
29
- log_error($!)
30
- cached_copy.nil? ? nil : cached_copy[:object]
37
+ log_error $!
38
+ cached_copy
31
39
  end
32
40
  else
33
- cached_copy.nil? ? nil : cached_copy[:object]
41
+ cached_copy
34
42
  end
35
43
  end
36
44
 
@@ -57,6 +65,11 @@ class Gravatar
57
65
  def read_cache(*key)
58
66
  @real_cache.read(cache_key(*key))
59
67
  end
68
+
69
+ def cached(*key)
70
+ copy = read_cache(*key)
71
+ copy &&= copy[:object]
72
+ end
60
73
 
61
74
  # Writes an object to the cache based on th cache key constructed from *key.
62
75
  def write_cache(object, *key)
@@ -67,36 +80,29 @@ class Gravatar
67
80
  def cache_key(*args)
68
81
  ActiveSupport::Cache.expand_cache_key(args, @namespace)
69
82
  end
70
-
83
+
71
84
  # Logs an error message, as long as self.logger responds to :error or :write.
72
85
  # Otherwise, re-raises the error.
73
86
  def log_error(error)
87
+ raise error unless rescue_errors
74
88
  if logger.respond_to?(:error)
75
89
  logger.error error.message
76
90
  error.backtrace.each { |line| logger.error " #{line}" }
77
91
  elsif logger.respond_to?(:write)
78
- logger.write(([error.message]+error.backtrace).join("\n "))
79
- logger.write("\n")
80
- else raise error
92
+ logger.write(([error.message] + error.backtrace).join("\n ") + "\n")
93
+ else
94
+ raise error
81
95
  end
82
96
  end
83
97
  end
84
98
 
85
99
  class << self
86
100
  def default_cache_instance
87
- if defined?(Rails)
88
- Rails.cache
89
- else
90
- ActiveSupport::Cache::FileStore.new("tmp/cache")
91
- end
101
+ defined?(Rails) ? Rails.cache : ActiveSupport::Cache::FileStore.new("tmp/cache")
92
102
  end
93
-
103
+
94
104
  def default_logger_instance
95
- if defined?(Rails)
96
- Rails.logger
97
- else
98
- $stdout
99
- end
105
+ defined?(Rails) ? Rails.logger : $stderr
100
106
  end
101
107
 
102
108
  def cache
@@ -106,11 +112,11 @@ class Gravatar
106
112
  def cache=(instance)
107
113
  @cache = instance
108
114
  end
109
-
115
+
110
116
  def logger
111
117
  @logger ||= default_logger_instance
112
118
  end
113
-
119
+
114
120
  def logger=(logger)
115
121
  @logger = logger
116
122
  end
@@ -1,22 +1,5 @@
1
- if defined?(Rails) and Rails::VERSION::STRING < '3.0.0'
2
- Rails.configuration.gem "sc-core-ext", ">= 1.2.0"
3
- elsif !defined?(Gem)
4
- require 'rubygems'
5
- gem 'activesupport', '>= 2.3.5'
6
- gem 'sc-core-ext', '>= 1.2.0'
7
- end
8
-
9
- unless defined?(Rails) # because Rails will load it later and we don't really need it quite yet.
10
- unless defined?(ActiveSupport::Cache)
11
- require 'active_support'
12
- end
13
-
14
- unless defined?(ScCoreExt)
15
- require 'sc-core-ext'
16
- end
17
- end
18
-
19
1
  # The rest of this is core Ruby stuff so it's safe to load immediately, even if Rails is running the show.
2
+ require 'cgi'
20
3
  require 'open-uri'
21
4
  require "digest/md5"
22
5
  require 'xmlrpc/client'
@@ -0,0 +1,105 @@
1
+ require 'fakeweb'
2
+
3
+ module Gravatar::TestCase
4
+ class << self
5
+ attr_accessor :fixtures_path
6
+ end
7
+
8
+ Gravatar::API_METHODS.each do |method_name|
9
+ define_method method_name do |*args, &block|
10
+ allow_net_connect = FakeWeb.allow_net_connect?
11
+ begin
12
+ apply_current_mock!
13
+ grav.send(method_name, *args, &block).tap do
14
+ # mock only lasts 1 request -- this should prevent the wrong
15
+ # mock being used on other APIs
16
+ mock_response nil
17
+ end
18
+ ensure
19
+ FakeWeb.allow_net_connect = allow_net_connect
20
+ end
21
+ end
22
+ end
23
+
24
+ delegate :auth_with, :email_hash, :to => :grav
25
+ delegate :fixtures_path, :to => "self.class"
26
+
27
+ def self.included(base)
28
+ base.instance_eval do
29
+ # Sets or gets the default email address to create Gravs with
30
+ # default: "generic@example.com"
31
+ def default_email(new_one = nil)
32
+ !new_one ? @default_email ||= "generic@example.com" : @default_email = new_one
33
+ end
34
+
35
+ def fixtures_path
36
+ @fixtures_path || Gravatar::TestCase.fixtures_path
37
+ end
38
+
39
+ def fixtures_path=(a)
40
+ @fixtures_path = a
41
+ end
42
+ end
43
+ end
44
+
45
+ # Sets the filename of the mock response to be used for the next request.
46
+ # Set to nil to disable mock responses entirely.
47
+ #
48
+ # If name is omitted, the contents of the current mock response, if any, are
49
+ # returned.
50
+ def mock_response(name = :__unassigned)
51
+ if name == :__unassigned
52
+ @mock_response
53
+ else
54
+ if name.nil?
55
+ FakeWeb::Registry.instance.uri_map.each do |uri, methods|
56
+ if uri.to_s == grav.url
57
+ methods.delete :post
58
+ end
59
+ end
60
+
61
+ @mock_response = nil
62
+ else
63
+ if fixtures_path.nil?
64
+ raise "fixtures_path is not set! Try setting that first."
65
+ end
66
+ @mock_response = File.read(File.join(fixtures_path, name))
67
+ end
68
+ end
69
+ end
70
+
71
+ def apply_current_mock!
72
+ if mock_response
73
+ FakeWeb.allow_net_connect = false
74
+ FakeWeb.register_uri :post, grav.url, :response => mock_response
75
+ else
76
+ # don't set to true, instead leave it set to whatever
77
+ # the user has it set to. This way if the user manually
78
+ # sets it to false, they don't have a 4-hour-long WTF.
79
+ # FakeWeb.allow_net_connect = false
80
+ end
81
+ end
82
+
83
+ # Set or get the Gravatar instance to test against.
84
+ #
85
+ # Examples:
86
+ # grav # return the default grav with bogus email
87
+ # grav(email) # replace with a new grav with given email
88
+ # grav # return the current grav with above email
89
+ #
90
+ def grav(*args)
91
+ if args.empty?
92
+ @grav ||= Gravatar.new(self.class.default_email, :rescue_errors => false)
93
+ else
94
+ options = args.extract_options!
95
+ options.reverse_merge! :rescue_errors => false
96
+ @grav = Gravatar.new(*args + [options])
97
+ end
98
+ end
99
+
100
+ def clear_account(g = grav)
101
+ g.user_images.each do |usrimg_hash, (rating, url)|
102
+ g.delete_user_image!(usrimg_hash)
103
+ end
104
+ end
105
+ end