fbrails 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -9,12 +9,14 @@ auth = Fbrails::Auth.new(app_id,app_secret,app_url)
9
9
  auth.redirect_url # get a redirect page to server-flow authorization
10
10
 
11
11
  token = auth.token(code) # getting token from code which send back from facebook
12
+ # => {:token => "TOKEN", :expires => "TIME_WHEN_EXPIRES"}
12
13
 
14
+ Fbrails::Auth.token_valid?(token) # return true of false
15
+ OR
16
+ Fbrails::Auth.token_valid?(token,expire_time) # return true of false
13
17
 
14
18
 
15
19
 
16
20
  @graph = Fbrails::Graph.new(token)
17
21
 
18
- @graph.token_valid? # return true of false
19
-
20
22
  @graph.me # return hash with your info or false
data/fbrails.gemspec CHANGED
@@ -6,7 +6,7 @@ require "fbrails/version"
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "fbrails"
8
8
  s.version = Fbrails::VERSION
9
- s.authors = ["Dmitry Gruzd"]
9
+ s.authors = ["Dmitry Gruzd", "Vitaly Mineev"]
10
10
  s.email = ["donotsendhere@gmail.com"]
11
11
  s.homepage = ""
12
12
  s.summary = %q{Small gem for auth & getting info from facebook}
@@ -1,3 +1,3 @@
1
1
  module Fbrails
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/fbrails.rb CHANGED
@@ -3,6 +3,9 @@ require 'net/https'
3
3
  require 'json'
4
4
  module Fbrails
5
5
 
6
+ class FailedToGet < StandardError
7
+ end
8
+
6
9
 
7
10
  class Auth
8
11
  def initialize(app_id, secret, app_url)
@@ -13,13 +16,34 @@ module Fbrails
13
16
 
14
17
  def token(code)
15
18
  url = "https://graph.facebook.com/oauth/access_token?client_id=#{@app_id}&redirect_uri=#{@app_url}&client_secret=#{@secret}&code=#{code}"
16
- resp = self.get(url)
19
+ resp = Fbrails.get(url,true)
17
20
  if resp.include?("access_token=")
18
- token = resp.body.gsub(/access_token=/, "").gsub(/&expires=\d+/,"")
21
+ hash = Hash.new
22
+ expire = resp.scan(/\d+/).last.to_i
23
+ hash[:expires] = Time.new + expire
24
+ hash[:token] = resp.gsub(/access_token=/, "").gsub(/&expires=\d+/,"")
25
+ return hash
19
26
  else
20
27
  nil
21
28
  end
22
29
  end
30
+
31
+ def self.token_valid?(token,expires = false)
32
+ if expires != false && expires < Time.new
33
+ return false
34
+ end
35
+
36
+ url = "https://graph.facebook.com/me/?access_token=#{token}"
37
+ result = Fbrails.get(url)
38
+ if result == false
39
+ return false
40
+ elsif result.has_key?("id")
41
+ return true
42
+ else
43
+ return nil
44
+ end
45
+
46
+ end
23
47
 
24
48
  def redirect_url
25
49
  if !@app_id.blank? && !@app_url.blank?
@@ -36,18 +60,6 @@ end
36
60
  end
37
61
 
38
62
 
39
- def token_valid?
40
- url = "https://graph.facebook.com/me/?access_token=#{@token}"
41
- result = Fbrails.get(url)
42
- if result == false
43
- return false
44
- elsif result.has_key?("id")
45
- return true
46
- else
47
- return nil
48
- end
49
-
50
- end
51
63
 
52
64
  def me
53
65
  url = "https://graph.facebook.com/me/?access_token=#{@token}"
@@ -56,16 +68,20 @@ end
56
68
  end
57
69
 
58
70
 
59
- def self.get(url)
71
+ def self.get(url,raw = false)
60
72
  uri = URI.parse(url)
61
73
  http = Net::HTTP.new(uri.host,uri.port)
62
74
  http.use_ssl = true
63
75
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
64
76
  request = Net::HTTP::Get.new(uri.request_uri)
65
77
  resp = http.request(request)
78
+ if raw
79
+ return resp.body
80
+ end
81
+
66
82
  result = JSON.parse(resp.body)
67
83
  if result.has_key?("error")
68
- return false
84
+ raise FailedToGet, "Failed to get, probably token expired"
69
85
  else
70
86
  return result
71
87
  end
metadata CHANGED
@@ -1,15 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fbrails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Dmitry Gruzd
9
+ - Vitaly Mineev
9
10
  autorequire:
10
11
  bindir: bin
11
12
  cert_chain: []
12
- date: 2011-10-02 00:00:00.000000000Z
13
+ date: 2011-10-03 00:00:00.000000000Z
13
14
  dependencies: []
14
15
  description: Small gem for auth & getting info from facebook
15
16
  email: