easy_gravatar 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b929302dc73502d843f02364106af88ad8657333
4
+ data.tar.gz: 009514eca04c69c4526aca0946b3eea80835e942
5
+ SHA512:
6
+ metadata.gz: c6d673b61090d30fdeaa40c5dc88cc206b0a8d375a38e6afec0d0b7ab5d8e6dde2dc88de957864339373b8654ac4e2c41bfcfafabd33743235dc4e24c7e809bf
7
+ data.tar.gz: 94ac8c8136a144fcb9e0120436e42663e806ddab92452c399adff7a22c6efc353cff25b6e331f7b5b9b0092fe2d4ad87053104e2cc8c4ffe7f392a32cd82439d
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in easy_gravatar.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Dennis van de Hoef
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # EasyGravatar
2
+
3
+ Gravatar.com, mainly known for its avatar hosting. But users can store more than only an avatar at gravatar.
4
+
5
+ This gem also allows you to easily access it all (accounts at other social networks, more photo's, the name of the user, saved websites, cryptocurrency accounts and more)
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'easy_gravatar'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install easy_gravatar
22
+
23
+ ## Usage
24
+
25
+ Creating an instance is easy:
26
+ ```ruby
27
+ user = EasyGravatar::Gravatar.new("foo@bar.com")
28
+ ```
29
+
30
+ Or use the even shorter class Gravatar:
31
+ ```ruby
32
+ user = Gravatar.new("foo@bar.com")
33
+ ```
34
+
35
+ After creating an instance you can get some basics fast using
36
+ ```ruby
37
+ user.full_name # => the full name of the user
38
+ user.avatar # => the avatar from the user, in the default gravatar width of 80 pixels
39
+ user.avatar(150) # => the avatar from the user, in with a width of 150 pixels
40
+ ```
41
+ Feel free to contribute to the project and add your own "short getters".
42
+
43
+ All the available information is saved in a hash. Use `user.hash` to get the hash. As an alternative it is also possible to easily read from the hash.
44
+ ```ruby
45
+ user.get_value(:formattedName) # => also gives the full name
46
+ user.get_value(:currency, :bitcoin) # => gives the users bitcoin address
47
+ ```
48
+
49
+ ## Contributing
50
+
51
+ 1. Fork it ( https://github.com/dennisvandehoef/easy_gravatar/fork )
52
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
53
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
54
+ 4. Push to the branch (`git push origin my-new-feature`)
55
+ 5. Create a new Pull Request
56
+ # easy_gravatar
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'rspec/core/rake_task'
2
+ require 'bundler/gem_tasks'
3
+
4
+ # Default directory to look in is `/specs`
5
+ # Run with `rake spec`
6
+ RSpec::Core::RakeTask.new(:spec) do |task|
7
+ task.rspec_opts = ['--color', '--format', 'nested']
8
+ end
9
+
10
+ task :default => :spec
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'easy_gravatar/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "easy_gravatar"
8
+ spec.version = EasyGravatar::VERSION
9
+ spec.authors = ["Dennis van de Hoef"]
10
+ spec.email = ["dennisvdhoef@gmail.com"]
11
+ spec.summary = %q{A simple gem that gives you -all- the data avalabile for the user on gravatar.com}
12
+ spec.description = %q{Gravatar.com, mainly known for its avatar hosting. But users can store more than only an avatar at gravatar. This gem also allows you to easily access it all (accounts at other social networks, more photo's, the name of the user, saved websites, cryptocurrency accounts and more)}
13
+ spec.homepage = "http://hoefweb.nl/ruby/easy_gravatar/"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.2.0"
24
+ end
@@ -0,0 +1,7 @@
1
+ require "easy_gravatar/version"
2
+ require "easy_gravatar/gravatar"
3
+ require "easy_gravatar/json_parser"
4
+
5
+ class Gravatar < EasyGravatar::Gravatar
6
+ #bridge to module
7
+ end
@@ -0,0 +1,53 @@
1
+ require 'digest/md5'
2
+ require 'net/http'
3
+ require 'uri'
4
+
5
+ module EasyGravatar
6
+ class Gravatar
7
+
8
+ attr_reader :md5
9
+
10
+ def initialize(email)
11
+ @md5 = Digest::MD5.hexdigest(email.downcase)
12
+ end
13
+
14
+ def hash
15
+ @hash ||= EasyGravatar::JsonParser.for(get_hash).parse
16
+ end
17
+
18
+ def full_name
19
+ get_value :formattedName
20
+ end
21
+
22
+ def avatar(width = 80)
23
+ "#{get_value(:thumbnailUrl)}?w=#{width}"
24
+ end
25
+
26
+ def get_value(key, subkey = nil)
27
+ return '' unless hash[key]
28
+ return '' if subkey and !hash[key][subkey]
29
+
30
+ return hash[key][subkey] if subkey
31
+
32
+ hash[key]
33
+ end
34
+
35
+ private
36
+
37
+ def get_hash
38
+ Net::HTTP.get(URI.parse(profile_url))
39
+ end
40
+
41
+ def profile_url(url = "https://www.gravatar.com/#{@md5}.json")
42
+ response = Net::HTTP.get_response(URI.parse(url))
43
+
44
+ case response
45
+ when Net::HTTPSuccess then
46
+ url
47
+ when Net::HTTPRedirection then
48
+ new_url = response['location']
49
+ profile_url(new_url)
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,91 @@
1
+ require 'json'
2
+
3
+ module EasyGravatar
4
+ class JsonParser
5
+
6
+ def self.for(json)
7
+ new json
8
+ end
9
+
10
+ def initialize(json = '')
11
+ @json = remove_first_layers(json)
12
+ end
13
+
14
+ def parse
15
+ hash = strip_basic_fields
16
+ hash.merge! strip_name_data
17
+ hash.merge! strip_ims
18
+ hash.merge! strip_profileBackground
19
+ hash.merge! strip_phoneNumbers
20
+ hash.merge! strip_emails
21
+ hash.merge! strip_currency
22
+ end
23
+
24
+ private
25
+
26
+ def remove_first_layers(json)
27
+ JSON.parse(json)['entry'][0]
28
+ end
29
+
30
+ def strip_basic_fields
31
+ hash = Hash.new
32
+ @json.keys.each do |key|
33
+ hash[key.to_sym] = @json[key] if @json[key].class == String
34
+ end
35
+ hash
36
+ end
37
+
38
+ def strip_ims
39
+ strip_basic_group('ims')
40
+ end
41
+
42
+ def strip_phoneNumbers
43
+ strip_basic_group('phoneNumbers')
44
+ end
45
+
46
+ def strip_currency
47
+ strip_basic_group('currency')
48
+ end
49
+
50
+ def strip_name_data
51
+ hash = Hash.new
52
+ return hash unless @json['name']
53
+ @json['name'].keys.each do |key|
54
+ new_key = key
55
+ new_key = "#{key}Name" if key == 'formatted'
56
+ hash[new_key.to_sym] = @json['name'][key]
57
+ end
58
+ hash
59
+ end
60
+
61
+ def strip_profileBackground
62
+ hash = Hash.new
63
+ return hash unless @json['profileBackground']
64
+ hash[:profileBackgroundColor] = @json['profileBackground']['color']
65
+ hash
66
+ end
67
+
68
+ def strip_emails
69
+ hash = Hash.new
70
+ return hash unless @json['emails']
71
+ array = Array.new
72
+ @json['emails'].each do |h|
73
+ array.push(h['value'])
74
+ end
75
+ hash[:email] = array
76
+ hash
77
+ end
78
+
79
+ def strip_basic_group(group)
80
+ hash = Hash.new
81
+ return hash unless @json[group]
82
+
83
+ hash[group.to_sym] = Hash.new
84
+ @json[group].each do |j|
85
+ hash[group.to_sym][j['type'].to_sym] = j['value']
86
+ end
87
+ hash
88
+ end
89
+
90
+ end
91
+ end
@@ -0,0 +1,3 @@
1
+ module EasyGravatar
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe EasyGravatar::Gravatar do
4
+
5
+ before(:each) do
6
+ @gravatar_user = EasyGravatar::Gravatar.new('foo@bar.com')
7
+ end
8
+
9
+ it 'can make a vallid md5 hash' do
10
+ expect(@gravatar_user.md5).to eq('f3ada405ce890b6f8204094deb12d8a8')
11
+ end
12
+
13
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ describe EasyGravatar::JsonParser do
4
+ before(:each) do
5
+ j = '{"entry":[{"id":"67473678","hash":"274096d1a68cf5740240b6ffb789c5c7","requestHash":"18d25970791c7cfaa521824b83600570","profileUrl":"http:\/\/gravatar.com\/dvdhoef88","preferredUsername":"dvdhoef88","thumbnailUrl":"https:\/\/secure.gravatar.com\/avatar\/274096d1a68cf5740240b6ffb789c5c7","photos":[{"value":"https:\/\/secure.gravatar.com\/avatar\/274096d1a68cf5740240b6ffb789c5c7","type":"thumbnail"},{"value":"https:\/\/secure.gravatar.com\/userimage\/67473678\/7c60b54ff484fcb807e82d77dd28563f"},{"value":"https:\/\/secure.gravatar.com\/userimage\/67473678\/d00f82088e7833d44208731f999d5246"}],"profileBackground":{"color":"#82cef7"},"name":{"givenName":"Dennis","familyName":"van de Hoef","formatted":"Dennis van de Hoef"},"displayName":"dvdhoef88","aboutMe":"A dutch man living in Berlin","currentLocation":"Berlin","phoneNumbers":[{"type":"home","value":"0123456789"},{"type":"work","value":"9876543210"},{"type":"mobile","value":"0147258369"}],"emails":[{"primary":"true","value":"foo@bar.com"}],"ims":[{"type":"aim","value":"fakeAim"},{"type":"yahoo","value":"FakeYahoo"},{"type":"icq","value":"fakeICQ"},{"type":"gtalk","value":"fakeGtalk"},{"type":"skype","value":"fakeSkype"}],"accounts":[{"domain":"facebook.com","display":"View Profile","url":"http:\/\/www.facebook.com\/100001859633067","username":"100001859633067","verified":"true","shortname":"facebook"},{"domain":"linkedin.com","display":"dvdhoef","url":"https:\/\/www.linkedin.com\/in\/dvdhoef","username":"dvdhoef","verified":"true","shortname":"linkedin"},{"domain":"twitter.com","display":"@DvdHoef88","url":"http:\/\/twitter.com\/DvdHoef88","username":"DvdHoef88","verified":"true","shortname":"twitter"}],"currency":[{"type":"bitcoin","value":"14Z6yNjuqnTRevmqe5jHiNYFWfvsRaFBGm"},{"type":"litecoin","value":"LgzvJPZEiZynEs2JFdoqRSwgMELVmRpvb8"},{"type":"dogecoin","value":"DEE13B32ZHfWu4EYuZUHYRdVF3DXi42ECg"}],"urls":[{"value":"http:\/\/www.hoefweb.nl","title":"Hoefweb.nl"},{"value":"http:\/\/github.com\/dennisvandehoef","title":"Profile on github"}]}]}'
6
+ @parsed = EasyGravatar::JsonParser.for(j).parse
7
+ end
8
+
9
+ it 'removes the first layers' do
10
+ expect(@parsed[:id]).to eq('67473678')
11
+ expect(@parsed[:hash]).to eq('274096d1a68cf5740240b6ffb789c5c7')
12
+ expect(@parsed[:requestHash]).to eq('18d25970791c7cfaa521824b83600570')
13
+ expect(@parsed[:profileUrl]).to eq('http://gravatar.com/dvdhoef88')
14
+ expect(@parsed[:preferredUsername]).to eq('dvdhoef88')
15
+ expect(@parsed[:thumbnailUrl]).to eq('https://secure.gravatar.com/avatar/274096d1a68cf5740240b6ffb789c5c7')
16
+ expect(@parsed[:displayName]).to eq('dvdhoef88')
17
+ expect(@parsed[:aboutMe]).to eq('A dutch man living in Berlin')
18
+ expect(@parsed[:currentLocation]).to eq('Berlin')
19
+ end
20
+
21
+ it 'adds name data to the first layer' do
22
+ expect(@parsed[:givenName]).to eq('Dennis')
23
+ expect(@parsed[:familyName]).to eq('van de Hoef')
24
+ expect(@parsed[:formattedName]).to eq('Dennis van de Hoef')
25
+ end
26
+
27
+ it 'adds the backgroundColor to the first layer' do
28
+ expect(@parsed[:profileBackgroundColor]).to eq('#82cef7')
29
+ end
30
+
31
+ it 'reorganises the phone numbers' do
32
+ expect(@parsed[:phoneNumbers][:home]).to eq('0123456789')
33
+ expect(@parsed[:phoneNumbers][:work]).to eq('9876543210')
34
+ expect(@parsed[:phoneNumbers][:mobile]).to eq('0147258369')
35
+ end
36
+
37
+ it 'reorganises the ims' do
38
+ expect(@parsed[:ims][:aim]).to eq('fakeAim')
39
+ expect(@parsed[:ims][:yahoo]).to eq('FakeYahoo')
40
+ expect(@parsed[:ims][:icq]).to eq('fakeICQ')
41
+ expect(@parsed[:ims][:gtalk]).to eq('fakeGtalk')
42
+ expect(@parsed[:ims][:skype]).to eq('fakeSkype')
43
+ end
44
+
45
+ it 'parses the email adresses to an array' do
46
+ expect(@parsed[:email][0]).to eq('foo@bar.com')
47
+ end
48
+
49
+ it 'reorganises the currencies' do
50
+ expect(@parsed[:currency][:bitcoin]).to eq('14Z6yNjuqnTRevmqe5jHiNYFWfvsRaFBGm')
51
+ expect(@parsed[:currency][:litecoin]).to eq('LgzvJPZEiZynEs2JFdoqRSwgMELVmRpvb8')
52
+ expect(@parsed[:currency][:dogecoin]).to eq('DEE13B32ZHfWu4EYuZUHYRdVF3DXi42ECg')
53
+ end
54
+ end
@@ -0,0 +1,5 @@
1
+ require 'easy_gravatar'
2
+
3
+ RSpec.configure do |config|
4
+ config.order = 'random'
5
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: easy_gravatar
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Dennis van de Hoef
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.2.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.2.0
55
+ description: Gravatar.com, mainly known for its avatar hosting. But users can store
56
+ more than only an avatar at gravatar. This gem also allows you to easily access
57
+ it all (accounts at other social networks, more photo's, the name of the user, saved
58
+ websites, cryptocurrency accounts and more)
59
+ email:
60
+ - dennisvdhoef@gmail.com
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - ".gitignore"
66
+ - ".rspec"
67
+ - Gemfile
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - easy_gravatar.gemspec
72
+ - lib/easy_gravatar.rb
73
+ - lib/easy_gravatar/gravatar.rb
74
+ - lib/easy_gravatar/json_parser.rb
75
+ - lib/easy_gravatar/version.rb
76
+ - spec/easy_gravatar/gravatar_spec.rb
77
+ - spec/easy_gravatar/json_parser_spec.rb
78
+ - spec/spec_helper.rb
79
+ homepage: http://hoefweb.nl/ruby/easy_gravatar/
80
+ licenses:
81
+ - MIT
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.4.2
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: A simple gem that gives you -all- the data avalabile for the user on gravatar.com
103
+ test_files:
104
+ - spec/easy_gravatar/gravatar_spec.rb
105
+ - spec/easy_gravatar/json_parser_spec.rb
106
+ - spec/spec_helper.rb
107
+ has_rdoc: