email_to_face 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -3,32 +3,82 @@ EmailToFace
3
3
 
4
4
  Installation
5
5
  ---
6
+
7
+ Easy:
8
+
9
+ [sudo|rvm] gem install email_to_face
10
+
6
11
  Using Bundler:
7
12
 
8
13
  gem "email_to_face"
9
14
 
10
15
  Facebok Graph API
11
16
  ----
12
- The Graph API is the simple, slick new interface to Facebook's data. Using it to get a user image with EmailToFace is quite straightforward:
13
17
 
14
- @email_to_face = EmailToFace::App.new(:facebook_user_token => oauth_access_token)
15
- @email_to_face.convert('user@email.com')
16
- => { :url => 'https://graph.facebook.com/111111111/picture?type=large' }
18
+ The Graph API is the simple, slick new interface to Facebook's data. Using it to get a user image with EmailToFace is quite straightforward.
19
+ See [developers.facebook.com/docs/authentication/](https://developers.facebook.com/docs/authentication/) for more information on obtaining access tokens.
20
+
21
+ ``` ruby
22
+ @email_to_face = EmailToFace::App.new(:facebook_user_token => oauth_access_token)
23
+ @email_to_face.convert('user@email.com')
24
+ => { :url => 'https://graph.facebook.com/111111111/picture?type=large' }
25
+ ```
17
26
 
18
27
 
19
28
  Gravatar
20
29
  ----
21
- If the user is not found via the Graph API, or if an access token is not passed, the gravatar api will be called.
22
30
 
23
- @email_to_face = EmailToFace::App.new()
24
- @email_to_face.convert('user@email.com')
25
- => { :url => 'http://www.gravatar.com/avatar.php?gravatar_id=c44b0f24cfce9aacc7c1969c5666cfae&d=404' }
31
+ If the user is not found via the Facebook Graph API, or if an access token is not passed, the Gravatar API will be called.
32
+
33
+ ``` ruby
34
+ @email_to_face = EmailToFace::App.new()
35
+ @email_to_face.convert('user@email.com')
36
+ => { :url => 'http://www.gravatar.com/avatar.php?gravatar_id=c44b0f24cfce9aacc7c1969c5666cfae&d=404&s=200' }
37
+ ```
26
38
 
27
39
  Face.com
28
40
  ----
41
+
29
42
  If face.com credentials are passed, the convert method will return the location of the users's face in the image.
30
43
  See [developers.face.com](http://developers.face.com/) for more information.
31
44
 
32
- @email_to_face = EmailToFace::App.new(:facebook_user_token => oauth_access_token, :face_api_key => key, :face_api_secret => secret)
33
- @email_to_face.convert('user@email.com')
34
- => { :url => 'https://graph.facebook.com/111111111/picture?type=large', :x => 48.89, :y => 38.1 }
45
+ ``` ruby
46
+ @email_to_face = EmailToFace::App.new(
47
+ :facebook_user_token => oauth_access_token,
48
+ :face_api_key => key,
49
+ :face_api_secret => secret)
50
+
51
+ @email_to_face.convert('user@email.com')
52
+ => { :url => 'https://graph.facebook.com/111111111/picture?type=large', :x => 48.89, :y => 38.1 }
53
+ ```
54
+
55
+
56
+ Optional settings
57
+ ----
58
+
59
+ By default Gravatar always returns square centered images. If you want to use face.com to find the center of the face anyway, you can use the `:use_face_for_gravatar` option (default is false):
60
+
61
+ ``` ruby
62
+ @email_to_face = EmailToFace::App.new(
63
+ :use_face_for_gravatar => true,
64
+ :face_api_key => key,
65
+ :face_api_secret => secret)
66
+
67
+ @email_to_face.convert('user@email.com')
68
+ => { :url => 'http://www.gravatar.com/avatar.php?gravatar_id=c44b0f24cfce9aacc7c1969c5666cfae&d=404&s=200', :x => 48.89, :y => 38.1 }
69
+ ```
70
+
71
+ The Facebook Graph API lets you specify the picture size you want with the type argument, which should be one of square (50x50), small (50 pixels wide, variable height), normal (100 pixels wide, variable height), and large (about 200 pixels wide, variable height). You can assign this via the `:facebook_image_type` option (default is 'large', Gravatar will match):
72
+
73
+ ``` ruby
74
+ @email_to_face = EmailToFace::App.new(
75
+ :facebook_image_type => 'small'
76
+ :facebook_user_token => oauth_access_token
77
+ :)
78
+
79
+ @email_to_face.convert('facebook@email.com')
80
+ => { :url => 'https://graph.facebook.com/111111111/picture?type=small' }
81
+
82
+ @email_to_face.convert('gravatar@email.com')
83
+ => { :url => 'http://www.gravatar.com/avatar.php?gravatar_id=c44b0f24cfce9aacc7c1969c5666cfae&d=404&s=50' }
84
+ ```
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
9
9
  s.email = ["jatescher@gmail.com"]
10
10
  s.homepage = "https://github.com/jtescher/EmailToFace"
11
11
  s.summary = %q{ Email to user image tool }
12
- s.description = %q{ A way to simply obtain a facebook or gravatar image from an email. }
12
+ s.description = %q{ A way to simply obtain a Facebook or Gravatar image from an email. }
13
13
 
14
14
  s.rubyforge_project = "email_to_face"
15
15
 
data/lib/email_to_face.rb CHANGED
@@ -7,8 +7,9 @@ require 'email_to_face/app'
7
7
  module EmailToFace
8
8
  class Facebook
9
9
 
10
- def self.init(fb_access_token)
10
+ def self.init(fb_access_token, facebook_image_type)
11
11
  @access_token = fb_access_token
12
+ @image_type = facebook_image_type
12
13
  end
13
14
 
14
15
  def self.user_image(email)
@@ -31,16 +32,17 @@ module EmailToFace
31
32
  raise result["error"]["message"] if result["error"]
32
33
 
33
34
  # Return either the url, or nil
34
- result['data'] == [] ? nil : "https://graph.facebook.com/#{result['data'][0]['id']}/picture?type=large"
35
+ result['data'] == [] ? nil : "https://graph.facebook.com/#{result['data'][0]['id']}/picture?type=#{@image_type || 'large'}"
35
36
  end
36
37
 
37
38
  end
38
39
 
39
40
  class Gravatar
40
41
 
41
- def self.user_image(email)
42
+ def self.user_image(email, fb_type=nil)
43
+ fb_types = { 'square' => 50, 'small' => 50, 'normal' => 100, 'large' => 200 }
42
44
  begin
43
- url = "http://www.gravatar.com/avatar.php?gravatar_id=#{Digest::MD5::hexdigest(email)}&d=404"
45
+ url = "http://www.gravatar.com/avatar.php?gravatar_id=#{Digest::MD5::hexdigest(email)}&d=404&s=#{fb_types[fb_type] || 200}"
44
46
  response = Net::HTTP.get_response(URI.parse(url))
45
47
  response.code == '200' ? url : nil
46
48
  rescue Exception => e
@@ -53,11 +55,13 @@ module EmailToFace
53
55
 
54
56
  class FaceAPI
55
57
 
56
- def self.init(face_api_key, face_api_secret)
58
+ def self.init(face_api_key, face_api_secret, use_face_for_gravatar)
57
59
  @client = Face.get_client(:api_key => face_api_key, :api_secret => face_api_secret)
60
+ @use_face_for_gravatar = use_face_for_gravatar || false
58
61
  end
59
62
 
60
63
  def self.get_center(url)
64
+ return if url.match(/gravatar.com/) and @use_face_for_gravatar == false
61
65
  begin
62
66
  result = @client.faces_detect(:urls => url)
63
67
  result['photos'][0]['tags'].empty? ? nil : result['photos'][0]['tags'][0]['center']
@@ -1,18 +1,20 @@
1
1
  module EmailToFace
2
2
  class App
3
- attr_accessor :fb_init, :face_init
3
+ attr_accessor :fb_init, :face_init, :fb_type
4
4
 
5
5
  def initialize(options={})
6
6
 
7
7
  # Initialize Facebook if params present
8
8
  if options[:facebook_user_token]
9
- Facebook.init(options[:facebook_user_token])
9
+
10
+ Facebook.init(options[:facebook_user_token], options[:facebook_image_type])
10
11
  @fb_init = true
12
+ @fb_type = options[:facebook_image_type]
11
13
  end
12
14
 
13
15
  # Initialize Face.com API if params present
14
16
  if options[:face_api_key] and options[:face_api_secret]
15
- FaceAPI.init(options[:face_api_key], options[:face_api_secret])
17
+ FaceAPI.init(options[:face_api_key], options[:face_api_secret], options[:use_face_for_gravatar])
16
18
  @face_init = true
17
19
  end
18
20
  end
@@ -27,15 +29,14 @@ module EmailToFace
27
29
 
28
30
  # If not found and twitter is initialized
29
31
  if image_url.nil?
30
- image_url = Gravatar.user_image(email)
32
+ image_url = Gravatar.user_image(email, @fb_type)
31
33
  end
32
34
 
33
35
  # If still no image found, return nil
34
36
  return nil if image_url.nil?
35
37
 
36
38
  # If present, grab face x,y from face.com
37
- if @face_init
38
- xy = FaceAPI.get_center(image_url)
39
+ if @face_init and xy = FaceAPI.get_center(image_url)
39
40
  { :url => image_url, :x => xy['x'], :y => xy['y'] }
40
41
  else
41
42
  { :url => image_url }
@@ -1,3 +1,3 @@
1
1
  module EmailToFace
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -3,17 +3,19 @@ require 'spec_helper'
3
3
  describe EmailToFace::App do
4
4
  before :all do
5
5
  @app = EmailToFace::App.new(
6
- :facebook_user_token => ENV['FB_USER_TOKEN'],
7
- :face_api_key => ENV['FACE_API_KEY'],
8
- :face_api_secret => ENV['FACE_API_SECRET'])
6
+ :facebook_user_token => ENV['FB_USER_TOKEN'],
7
+ :face_api_key => ENV['FACE_API_KEY'],
8
+ :face_api_secret => ENV['FACE_API_SECRET'])
9
9
  end
10
10
 
11
11
  describe "#convert" do
12
+
12
13
  it "should raise an error if email is nil or malformed" do
13
14
  expect { @app.convert(nil) } .to raise_error(ArgumentError)
14
15
  expect { @app.convert('@email.com') } .to raise_error(ArgumentError)
15
16
  expect { @app.convert('good@email.com') } .to_not raise_error(ArgumentError)
16
17
  end
18
+
17
19
  context 'for facebook' do
18
20
  it "should return an object with a url and center x,y if valid" do
19
21
  result = @app.convert("pat2man@gmail.com")
@@ -21,15 +23,41 @@ describe EmailToFace::App do
21
23
  result[:x].should == 48.89
22
24
  result[:y].should == 42.29
23
25
  end
26
+
27
+ it "should return a :facebook_image_type facebook image if :facebook_image_type is set" do
28
+ @app = EmailToFace::App.new(
29
+ :facebook_user_token => ENV['FB_USER_TOKEN'],
30
+ :facebook_image_type => 'small')
31
+
32
+ result = @app.convert("pat2man@gmail.com")
33
+ result[:url].should == 'https://graph.facebook.com/518026574/picture?type=small'
34
+ end
24
35
  end
36
+
25
37
  context 'for gravatar' do
26
- it "should return an object with a url and center x,y if valid" do
38
+ it "should return an object with a url and center x,y if valid and :use_face_for_gravatar set" do
39
+ @app = EmailToFace::App.new(
40
+ :use_face_for_gravatar => true,
41
+ :facebook_user_token => ENV['FB_USER_TOKEN'],
42
+ :face_api_key => ENV['FACE_API_KEY'],
43
+ :face_api_secret => ENV['FACE_API_SECRET'])
44
+ result = @app.convert("virulent@gmail.com")
45
+ result[:url].should == 'http://www.gravatar.com/avatar.php?gravatar_id=c44b0f24cfce9aacc7c1969c5666cfae&d=404&s=200'
46
+ result[:x].should == 31.75
47
+ result[:y].should == 60.75
48
+ end
49
+
50
+ it "should return an object with a url if :use_face_for_gravatar not set" do
51
+ @app = EmailToFace::App.new(
52
+ :face_api_key => ENV['FACE_API_KEY'],
53
+ :face_api_secret => ENV['FACE_API_SECRET'])
27
54
  result = @app.convert("virulent@gmail.com")
28
- result[:url].should == 'http://www.gravatar.com/avatar.php?gravatar_id=c44b0f24cfce9aacc7c1969c5666cfae&d=404'
29
- result[:x].should == 30.63
30
- result[:y].should == 60.63
55
+ result[:url].should == 'http://www.gravatar.com/avatar.php?gravatar_id=c44b0f24cfce9aacc7c1969c5666cfae&d=404&s=200'
56
+ result[:x].should be_nil
57
+ result[:y].should be_nil
31
58
  end
32
59
  end
60
+
33
61
  end
34
62
 
35
63
  end
@@ -22,12 +22,16 @@ end
22
22
  describe EmailToFace::Gravatar do
23
23
  describe ".user_image" do
24
24
  it "returns the url for the user's image if it exists" do
25
- EmailToFace::Gravatar.user_image('virulent@gmail.com').should == 'http://www.gravatar.com/avatar.php?gravatar_id=c44b0f24cfce9aacc7c1969c5666cfae&d=404'
25
+ EmailToFace::Gravatar.user_image('virulent@gmail.com').should == 'http://www.gravatar.com/avatar.php?gravatar_id=c44b0f24cfce9aacc7c1969c5666cfae&d=404&s=200'
26
26
  end
27
27
 
28
28
  it "returns nil if the user does not exist" do
29
29
  EmailToFace::Gravatar.user_image('notanemail').should be_nil
30
30
  end
31
+
32
+ it "returns the proper size if fb_type is set" do
33
+ EmailToFace::Gravatar.user_image('virulent@gmail.com', 'small').should == 'http://www.gravatar.com/avatar.php?gravatar_id=c44b0f24cfce9aacc7c1969c5666cfae&d=404&s=50'
34
+ end
31
35
  end
32
36
  end
33
37
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: email_to_face
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 4
10
+ version: 0.0.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Julian Tescher
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-09-14 00:00:00 -07:00
18
+ date: 2011-09-15 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -60,7 +60,7 @@ dependencies:
60
60
  version: "0"
61
61
  type: :runtime
62
62
  version_requirements: *id003
63
- description: " A way to simply obtain a facebook or gravatar image from an email. "
63
+ description: " A way to simply obtain a Facebook or Gravatar image from an email. "
64
64
  email:
65
65
  - jatescher@gmail.com
66
66
  executables: []