Module: Gravity::InstanceMethods

Defined in:
lib/gravity.rb

Instance Method Summary collapse

Instance Method Details

#get(url) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/gravity.rb', line 62

def get(url)
  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)
  if uri.scheme == 'https'
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end
  request = Net::HTTP::Get.new(uri.request_uri)
  response = http.request(request)

  if response.kind_of?(Net::HTTPRedirection)
    get response['location']
  else
    response.body
  end
end

#gravatar_hash(email) ⇒ Object



26
27
28
# File 'lib/gravity.rb', line 26

def gravatar_hash(email)
  Digest::MD5.hexdigest email.to_s.strip.downcase
end

#gravatar_image(options = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/gravity.rb', line 34

def gravatar_image(options = {})
  options = self.class.gravatar_options.merge options
  email = self.send options.delete(:email)
  secure = options.delete :secure
  url = gravatar_url(secure) + 'avatar/' + gravatar_hash(email)

  if options.has_key?(:default)
    options[:default] = CGI::encode options[:default] if options[:default].match(/^http/)
  end

  if !options.empty?
    url += '?' + options.map{|o, v| "#{o}=#{v.to_s}"}.join('&')
  end

  url
end

#gravatar_profile(options = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/gravity.rb', line 51

def gravatar_profile(options = {})
  options = self.class.gravatar_options.merge options

  email = self.send options.delete(:email)
  secure = options.delete :secure
  url = gravatar_url(secure) + gravatar_hash(email) + '.json'

  json_profile = JSON.parse get(url)
  json_profile['entry'].first
end

#gravatar_url(secure) ⇒ Object



30
31
32
# File 'lib/gravity.rb', line 30

def gravatar_url(secure)
  'http' + (secure ? 's://secure.' : '://') + 'gravatar.com/'
end