Class: EmailToFace::Facebook

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

Class Method Summary collapse

Class Method Details

.init(fb_access_token, facebook_image_type) ⇒ Object



10
11
12
13
# File 'lib/email_to_face.rb', line 10

def self.init(fb_access_token, facebook_image_type)
  @access_token = fb_access_token
  @image_type = facebook_image_type
end

.user_image(email) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/email_to_face.rb', line 15

def self.user_image(email)
  begin
    # Query the graph for the user e.g. [email protected]&type=user&access_token=token
    url = URI.encode("https://graph.facebook.com/search?q=#{email}&type=user&access_token=#{@access_token}")
    uri = URI.parse(url)

    req = Net::HTTP.new(uri.host, 443)
    req.use_ssl = true

    response = req.request_get(uri.path + '?' + uri.query)
    result = JSON.parse(response.body)
  rescue Exception => e
    puts e.inspect
    return nil
  end

  # Raise error if one is returned
  raise result["error"]["message"] if result["error"]

  # Return either the url, or nil
  result['data'] == [] ? nil : "https://graph.facebook.com/#{result['data'][0]['id']}/picture?type=#{@image_type || 'large'}"
end