Class: EmailToFace::App

Inherits:
Object
  • Object
show all
Defined in:
lib/email_to_face/app.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ App

Returns a new instance of App.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/email_to_face/app.rb', line 5

def initialize(options={})

  # Initialize Facebook if params present
  if options[:facebook_user_token]

    Facebook.init(options[:facebook_user_token], options[:facebook_image_type])
    @fb_init = true
    @fb_type = options[:facebook_image_type]
  end

  # Initialize Face.com API if params present
  if options[:face_api_key] and options[:face_api_secret]
    FaceAPI.init(options[:face_api_key], options[:face_api_secret], options[:use_face_for_gravatar])
    @face_init = true
  end
end

Instance Attribute Details

#face_initObject

Returns the value of attribute face_init.



3
4
5
# File 'lib/email_to_face/app.rb', line 3

def face_init
  @face_init
end

#fb_initObject

Returns the value of attribute fb_init.



3
4
5
# File 'lib/email_to_face/app.rb', line 3

def fb_init
  @fb_init
end

#fb_typeObject

Returns the value of attribute fb_type.



3
4
5
# File 'lib/email_to_face/app.rb', line 3

def fb_type
  @fb_type
end

Instance Method Details

#convert(email) ⇒ Object

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/email_to_face/app.rb', line 22

def convert(email)
  raise ArgumentError, "must pass a valid email address" if email.nil? or !email.match(/^([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})$/i)

  # First query facebook if initialized
  if @fb_init
    image_url = Facebook.user_image(email)
  end

  # If not found and twitter is initialized
  if image_url.nil?
    image_url = Gravatar.user_image(email, @fb_type)
  end

  # If still no image found, return nil
  return nil if image_url.nil?

  # If present, grab face x,y from face.com
  if @face_init and xy = FaceAPI.get_center(image_url)
    { :url => image_url, :x => xy['x'], :y => xy['y'] }
  else
    { :url => image_url }
  end
end