mogli 0.0.9 → 0.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,22 +1,35 @@
1
1
  require "cgi"
2
+ require 'mogli/client'
3
+
2
4
  module Mogli
3
5
  class Authenticator
4
6
  attr_reader :client_id, :secret, :callback_url
5
-
7
+
6
8
  def initialize(client_id,secret,callback_url)
7
9
  @client_id = client_id
8
10
  @secret = secret
9
11
  @callback_url = callback_url
10
12
  end
11
-
13
+
12
14
  def authorize_url(options = {})
13
15
  options_part = "&" + options.map {|k,v| "#{k}=#{v.kind_of?(Array) ? v.join(',') : v}" }.join('&') unless options.empty?
14
16
  "https://graph.facebook.com/oauth/authorize?client_id=#{client_id}&redirect_uri=#{CGI.escape(callback_url)}#{options_part}"
15
17
  end
16
-
18
+
17
19
  def access_token_url(code)
18
20
  "https://graph.facebook.com/oauth/access_token?client_id=#{client_id}&redirect_uri=#{CGI.escape(callback_url)}&client_secret=#{secret}&code=#{CGI.escape(code)}"
19
21
  end
20
-
22
+
23
+ def get_access_token_for_session_key(session_keys)
24
+ keystr = session_keys.is_a?(Array) ?
25
+ session_keys.join(',') : session_keys
26
+ client = Mogli::Client.new
27
+ client.post("oauth/exchange_sessions", nil,
28
+ {:type => 'client_cred',
29
+ :client_id => client_id,
30
+ :client_secret => secret,
31
+ :sessions => keystr})
32
+ end
33
+
21
34
  end
22
35
  end
data/lib/mogli/client.rb CHANGED
@@ -6,16 +6,16 @@ module Mogli
6
6
  attr_reader :access_token
7
7
  attr_reader :default_params
8
8
  attr_reader :expiration
9
-
9
+
10
10
  include HTTParty
11
11
  include Mogli::Client::Event
12
- include Mogli::Client::User
12
+ include Mogli::Client::User
13
13
  class UnrecognizeableClassError < Exception; end
14
-
14
+
15
15
  def api_path(path)
16
16
  "https://graph.facebook.com/#{path}"
17
17
  end
18
-
18
+
19
19
  def initialize(access_token = nil,expiration=nil)
20
20
  @access_token = access_token
21
21
  # nil expiration means extended access
@@ -23,11 +23,11 @@ module Mogli
23
23
  @expiration = Time.at(expiration)
24
24
  @default_params = @access_token ? {:access_token=>access_token} : {}
25
25
  end
26
-
26
+
27
27
  def expired?
28
28
  expiration and expiration < Time.now
29
29
  end
30
-
30
+
31
31
  def self.create_from_code_and_authenticator(code,authenticator)
32
32
  post_data = get(authenticator.access_token_url(code))
33
33
  parts = post_data.split("&")
@@ -37,42 +37,49 @@ module Mogli
37
37
  end
38
38
  new(hash["access_token"],hash["expires"].to_s.to_i)
39
39
  end
40
-
40
+
41
+ def self.create_from_session_key(session_key, client_id, secret)
42
+ authenticator = Mogli::Authenticator.new(client_id, secret, nil)
43
+ access_data = authenticator.get_access_token_for_session_key(session_key)
44
+ new(access_data['access_token'],
45
+ Time.now.to_i + access_data['expires'].to_i)
46
+ end
47
+
41
48
  def post(path,klass,body_args)
42
- data = self.class.post(api_path(path),:body=>default_params.merge(body_args))
49
+ data = self.class.post(api_path(path),:body=>default_params.merge(body_args))
43
50
  map_data(data,klass)
44
51
  end
45
-
52
+
46
53
  def delete(path)
47
54
  self.class.delete(api_path(path),:query=>default_params)
48
55
  end
49
-
56
+
50
57
  def get_and_map(path,klass=nil,body_args = {})
51
58
  data = self.class.get(api_path(path),:query=>default_params.merge(body_args))
52
59
  map_data(data,klass)
53
60
  end
54
-
61
+
55
62
  def get_and_map_url(url,klass=nil)
56
63
  data = self.class.get(url)
57
64
  map_data(data,klass)
58
65
  end
59
-
66
+
60
67
  def map_data(data,klass=nil)
61
68
  raise_error_if_necessary(data)
62
69
  hash_or_array = extract_hash_or_array(data,klass)
63
70
  hash_or_array = map_to_class(hash_or_array,klass) if klass
64
71
  hash_or_array
65
72
  end
66
-
73
+
67
74
  #protected
68
-
75
+
69
76
  def extract_hash_or_array(hash_or_array,klass)
70
77
  return nil if hash_or_array == false
71
78
  return hash_or_array if hash_or_array.nil? or hash_or_array.kind_of?(Array)
72
79
  return extract_fetching_array(hash_or_array,klass) if hash_or_array.has_key?("data")
73
80
  return hash_or_array
74
81
  end
75
-
82
+
76
83
  def extract_fetching_array(hash,klass)
77
84
  f = Mogli::FetchingArray.new
78
85
  f.concat(hash["data"])
@@ -84,7 +91,7 @@ module Mogli
84
91
  end
85
92
  f
86
93
  end
87
-
94
+
88
95
  def map_to_class(hash_or_array,klass)
89
96
  return nil if hash_or_array.nil?
90
97
  if hash_or_array.kind_of?(Array)
@@ -93,7 +100,7 @@ module Mogli
93
100
  hash_or_array = create_instance(klass,hash_or_array)
94
101
  end
95
102
  end
96
-
103
+
97
104
  def create_instance(klass,data)
98
105
  klass = determine_class(klass,data)
99
106
  if klass.nil?
@@ -101,16 +108,16 @@ module Mogli
101
108
  end
102
109
  klass.new(data,self)
103
110
  end
104
-
111
+
105
112
  def constantize_string(klass)
106
113
  klass.is_a?(String) ? Mogli.const_get(klass) : klass
107
114
  end
108
-
115
+
109
116
  def determine_class(klass_or_klasses,data)
110
117
  klasses = Array(klass_or_klasses).map { |k| constantize_string(k)}
111
118
  klasses.detect {|klass| klass.recognize?(data)} || klasses.first
112
119
  end
113
-
120
+
114
121
  def raise_error_if_necessary(data)
115
122
  if data.kind_of?(Hash)
116
123
  if data.keys.size == 1 and data["error"]
@@ -120,22 +127,22 @@ module Mogli
120
127
  end
121
128
  end
122
129
  end
123
-
130
+
124
131
  def fields_to_serialize
125
132
  [:access_token,:default_params,:expiration]
126
133
  end
127
-
134
+
128
135
  # Only serialize the bare minimum to recreate the session.
129
136
  def marshal_load(variables)#:nodoc:
130
137
  fields_to_serialize.each_with_index{|field, index| instance_variable_set("@#{field}", variables[index])}
131
138
  end
132
139
 
133
- # Only serialize the bare minimum to recreate the session.
140
+ # Only serialize the bare minimum to recreate the session.
134
141
  def marshal_dump#:nodoc:
135
142
  fields_to_serialize.map{|field| send(field)}
136
143
  end
137
144
 
138
- # Only serialize the bare minimum to recreate the session.
145
+ # Only serialize the bare minimum to recreate the session.
139
146
  def to_yaml( opts = {} )#nodoc
140
147
  YAML::quick_emit(self.object_id, opts) do |out|
141
148
  out.map(taguri) do |map|
@@ -145,6 +152,6 @@ module Mogli
145
152
  end
146
153
  end
147
154
  end
148
-
155
+
149
156
  end
150
157
  end
data/lib/mogli/model.rb CHANGED
@@ -3,7 +3,7 @@ module Mogli
3
3
  def client=(val)
4
4
  @client=val
5
5
  end
6
-
6
+
7
7
  def client
8
8
  @client || Mogli::Client.new
9
9
  end
@@ -12,7 +12,7 @@ module Mogli
12
12
  self.client=client
13
13
  super(hash||{})
14
14
  end
15
-
15
+
16
16
  def post_params
17
17
  post_params = {}
18
18
  self.class.creation_keys.each do |key|
@@ -20,12 +20,12 @@ module Mogli
20
20
  end
21
21
  post_params
22
22
  end
23
-
23
+
24
24
  def destroy
25
25
  client.delete(id)
26
26
  freeze
27
27
  end
28
-
28
+
29
29
  def self.included(other)
30
30
  other.extend(ClassMethods)
31
31
  end
@@ -34,28 +34,28 @@ module Mogli
34
34
  method_as_s = method.to_s
35
35
  if method_as_s.to_s[-1].chr == "="
36
36
  warn_about_invalid_property(method_as_s.chop)
37
- else
37
+ else
38
38
  super
39
39
  end
40
40
  end
41
41
  def warn_about_invalid_property(property)
42
42
  puts "Warning: property #{property} doesn't exist for class #{self.class.name}"
43
43
  end
44
-
44
+
45
45
  def self.define_properties(*args)
46
46
  args.each do |arg|
47
47
  property arg
48
48
  end
49
49
  end
50
-
50
+
51
51
  def self.creation_properties(*args)
52
52
  @creation_properties = args
53
53
  end
54
-
54
+
55
55
  def self.creation_keys
56
56
  @creation_properties || []
57
57
  end
58
-
58
+
59
59
  def self.hash_populating_accessor(method_name,*klass)
60
60
  define_method "#{method_name}=" do |hash|
61
61
  instance_variable_set("@#{method_name}",client.map_data(hash,klass))
@@ -63,19 +63,20 @@ module Mogli
63
63
  define_method "#{method_name}" do
64
64
  instance_variable_get "@#{method_name}"
65
65
  end
66
-
66
+
67
67
  add_creation_method(method_name,klass)
68
-
68
+
69
69
  end
70
-
70
+
71
71
  def self.add_creation_method(name,klass)
72
- define_method "#{name}_create" do |arg|
72
+ define_method "#{name}_create" do |*args|
73
+ arg = args.first
73
74
  params = arg.nil? ? {} : arg.post_params
74
75
  klass_to_send = arg.nil? ? nil : klass
75
76
  client.post("#{id}/#{name}", klass_to_send, params)
76
77
  end
77
78
  end
78
-
79
+
79
80
  def self.has_association(name,klass)
80
81
  define_method name do |*fields|
81
82
  body_args = fields.empty? ? {} : {:fields => fields}
@@ -85,23 +86,23 @@ module Mogli
85
86
  end
86
87
  return ret
87
88
  end
88
-
89
+
89
90
  add_creation_method(name,klass)
90
91
  end
91
-
92
+
92
93
  def fetch()
93
94
  raise ArgumentError.new("You cannot fetch models without a populated id attribute") if id.nil?
94
- other = self.class.find(id,client)
95
- merge!(other)
95
+ other = self.class.find(id,client)
96
+ merge!(other) if other
96
97
  end
97
-
98
+
98
99
  def self.recognize?(data)
99
100
  true
100
101
  end
101
-
102
+
102
103
  def self.find(id,client=nil, *fields)
103
104
  body_args = fields.empty? ? {} : {:fields => fields}
104
105
  (client||Mogli::Client.new).get_and_map(id,self, body_args)
105
106
  end
106
107
  end
107
- end
108
+ end
data/lib/mogli/user.rb CHANGED
@@ -3,7 +3,7 @@ module Mogli
3
3
  class User < Profile
4
4
 
5
5
  define_properties :first_name, :last_name, :link, :about, :birthday, :gender,
6
- :email, :website, :timezone, :updated_time, :verified
6
+ :email, :website, :timezone, :updated_time, :verified, :political, :relationship_status
7
7
 
8
8
  def self.recognize?(hash)
9
9
  !hash.has_key?("category")
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mogli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.0.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Mangino
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-05-26 00:00:00 -04:00
12
+ date: 2010-06-28 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency