email_to_face 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .DS_Store
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in email_to_face.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "email_to_face/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "email_to_face"
7
+ s.version = EmailToFace::VERSION
8
+ s.authors = ["Julian Tescher"]
9
+ s.email = ["jatescher@gmail.com"]
10
+ s.homepage = ""
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. }
13
+
14
+ s.rubyforge_project = "email_to_face"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ s.add_development_dependency "rspec"
23
+
24
+ # s.add_runtime_dependency "rest-client"
25
+ s.add_runtime_dependency "json"
26
+ s.add_runtime_dependency "typhoeus"
27
+ s.add_runtime_dependency "face"
28
+ end
@@ -0,0 +1,66 @@
1
+ require "json"
2
+ require 'typhoeus'
3
+ require 'face'
4
+ require "email_to_face/version"
5
+ require 'email_to_face/app'
6
+
7
+ module EmailToFace
8
+ class Facebook
9
+
10
+ def self.init(fb_access_token)
11
+ @access_token = fb_access_token
12
+ end
13
+
14
+ def self.user_image(email)
15
+ begin
16
+ # Query the graph for the user e.g. email@gmail.com&type=user&access_token=token
17
+ uri = URI.encode("https://graph.facebook.com/search?q=#{email}&type=user&access_token=#{@access_token}")
18
+ response = Typhoeus::Request.get(uri, :disable_ssl_peer_verification => true)
19
+ result = JSON.parse(response.body)
20
+ rescue Exception => e
21
+ puts e.inspect
22
+ return nil
23
+ end
24
+
25
+ # Raise error if one is returned
26
+ raise result["error"]["message"] if result["error"]
27
+
28
+ # Return either the url, or nil
29
+ result['data'] == [] ? nil : "https://graph.facebook.com/#{result['data'][0]['id']}/picture?type=large"
30
+ end
31
+
32
+ end
33
+
34
+ class Gravatar
35
+
36
+ def self.user_image(email)
37
+ begin
38
+ uri = URI.encode("http://www.gravatar.com/avatar.php?gravatar_id=#{Digest::MD5::hexdigest(email)}&d=404")
39
+ response = Typhoeus::Request.get(uri)
40
+ response.code == 200 ? uri : nil
41
+ rescue Exception => e
42
+ puts e.inspect
43
+ return nil
44
+ end
45
+ end
46
+
47
+ end
48
+
49
+ class FaceAPI
50
+
51
+ def self.init(face_api_key, face_api_secret)
52
+ @client = Face.get_client(:api_key => face_api_key, :api_secret => face_api_secret)
53
+ end
54
+
55
+ def self.get_center(url)
56
+ begin
57
+ result = @client.faces_detect(:urls => url)
58
+ result['photos'][0]['tags'].empty? ? nil : result['photos'][0]['tags'][0]['center']
59
+ rescue Exception => e
60
+ puts e.inspect
61
+ return nil
62
+ end
63
+ end
64
+
65
+ end
66
+ end
@@ -0,0 +1,45 @@
1
+ module EmailToFace
2
+ class App
3
+ attr_accessor :fb_init, :face_init
4
+
5
+ def initialize(options={})
6
+
7
+ # Initialize Facebook if params present
8
+ if options[:facebook_user_token]
9
+ Facebook.init(options[:facebook_user_token])
10
+ @fb_init = true
11
+ end
12
+
13
+ # Initialize Face.com API if params present
14
+ if options[:face_api_key] and options[:face_api_secret]
15
+ FaceAPI.init(options[:face_api_key], options[:face_api_secret])
16
+ @face_init = true
17
+ end
18
+ end
19
+
20
+ def convert(email)
21
+ raise ArgumentError, "must pass a valid email address" if email.nil? or !email.match(/^([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})$/i)
22
+
23
+ # First query facebook if initialized
24
+ if @fb_init
25
+ image_url = Facebook.user_image(email)
26
+ end
27
+
28
+ # If not found and twitter is initialized
29
+ if image_url.nil?
30
+ image_url = Gravatar.user_image(email)
31
+ end
32
+
33
+ # If still no image found, return nil
34
+ return nil if image_url.nil?
35
+
36
+ # If present, grab face x,y from face.com
37
+ if @face_init
38
+ xy = FaceAPI.get_center(image_url)
39
+ { :url => image_url, :x => xy['x'], :y => xy['y'] }
40
+ else
41
+ { :url => image_url }
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,3 @@
1
+ module EmailToFace
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe EmailToFace::App do
4
+ before :all do
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'])
9
+ end
10
+
11
+ describe "#convert" do
12
+ it "should raise an error if email is nil or malformed" do
13
+ expect { @app.convert(nil) } .to raise_error(ArgumentError)
14
+ expect { @app.convert('@email.com') } .to raise_error(ArgumentError)
15
+ expect { @app.convert('good@email.com') } .to_not raise_error(ArgumentError)
16
+ end
17
+ context 'for facebook' do
18
+ it "should return an object with a url and center x,y if valid" do
19
+ result = @app.convert("pat2man@gmail.com")
20
+ result[:url].should == 'https://graph.facebook.com/518026574/picture?type=large'
21
+ result[:x].should == 48.89
22
+ result[:y].should == 42.29
23
+ end
24
+ end
25
+ context 'for gravatar' do
26
+ it "should return an object with a url and center x,y if valid" do
27
+ 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
31
+ end
32
+ end
33
+ end
34
+
35
+ end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+
3
+ describe EmailToFace::Facebook do
4
+ before :each do
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'])
9
+ end
10
+
11
+ describe ".user_image" do
12
+ it "returns the url for the Facebook user's image if it exists" do
13
+ EmailToFace::Facebook.user_image('pat2man@gmail.com').should == 'https://graph.facebook.com/518026574/picture?type=large'
14
+ end
15
+
16
+ it "returns nil if the user does not exist" do
17
+ EmailToFace::Facebook.user_image('notanemail').should be_nil
18
+ end
19
+ end
20
+ end
21
+
22
+ describe EmailToFace::Gravatar do
23
+ describe ".user_image" do
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'
26
+ end
27
+
28
+ it "returns nil if the user does not exist" do
29
+ EmailToFace::Gravatar.user_image('notanemail').should be_nil
30
+ end
31
+ end
32
+ end
33
+
34
+ describe EmailToFace::FaceAPI do
35
+ before :each do
36
+ @app = EmailToFace::App.new(
37
+ :facebook_user_token => ENV['FB_USER_TOKEN'],
38
+ :face_api_key => ENV['FACE_API_KEY'],
39
+ :face_api_secret => ENV['FACE_API_SECRET'])
40
+ end
41
+
42
+ describe ".get_center" do
43
+ it "returns the proper face.com response" do
44
+ result = EmailToFace::FaceAPI.get_center('https://graph.facebook.com/10736346/picture?type=large')
45
+ result.should == {"x"=>48.89, "y"=>38.1}
46
+ end
47
+ end
48
+
49
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH << File.expand_path('../../lib', __FILE__)
2
+ require 'email_to_face'
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: email_to_face
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Julian Tescher
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-09-13 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: json
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :runtime
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: typhoeus
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ type: :runtime
62
+ version_requirements: *id003
63
+ - !ruby/object:Gem::Dependency
64
+ name: face
65
+ prerelease: false
66
+ requirement: &id004 !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ hash: 3
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ type: :runtime
76
+ version_requirements: *id004
77
+ description: " A way to simply obtain a facebook, or gravatar image from an email. "
78
+ email:
79
+ - jatescher@gmail.com
80
+ executables: []
81
+
82
+ extensions: []
83
+
84
+ extra_rdoc_files: []
85
+
86
+ files:
87
+ - .gitignore
88
+ - Gemfile
89
+ - Rakefile
90
+ - email_to_face.gemspec
91
+ - lib/email_to_face.rb
92
+ - lib/email_to_face/app.rb
93
+ - lib/email_to_face/version.rb
94
+ - spec/lib/email_to_face/app_spec.rb
95
+ - spec/lib/email_to_face_spec.rb
96
+ - spec/spec_helper.rb
97
+ has_rdoc: true
98
+ homepage: ""
99
+ licenses: []
100
+
101
+ post_install_message:
102
+ rdoc_options: []
103
+
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ hash: 3
112
+ segments:
113
+ - 0
114
+ version: "0"
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ hash: 3
121
+ segments:
122
+ - 0
123
+ version: "0"
124
+ requirements: []
125
+
126
+ rubyforge_project: email_to_face
127
+ rubygems_version: 1.6.1
128
+ signing_key:
129
+ specification_version: 3
130
+ summary: Email to user image tool
131
+ test_files:
132
+ - spec/lib/email_to_face/app_spec.rb
133
+ - spec/lib/email_to_face_spec.rb
134
+ - spec/spec_helper.rb