refacebook 0.4.5 → 0.4.6
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +0 -1
- data/VERSION.yml +1 -1
- data/lib/refacebook.rb +50 -14
- data/spec/api_spec.rb +10 -4
- metadata +1 -1
data/Rakefile
CHANGED
data/VERSION.yml
CHANGED
data/lib/refacebook.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'net/http'
|
2
2
|
require 'uri'
|
3
3
|
require 'md5'
|
4
|
+
require 'cgi'
|
4
5
|
require 'json'
|
5
6
|
|
6
7
|
module ReFacebook
|
@@ -68,14 +69,12 @@ module ReFacebook
|
|
68
69
|
# If you create a session update the session_key with the session value so that
|
69
70
|
# all the calls become authenticated.
|
70
71
|
#
|
71
|
-
# Example
|
72
|
-
#
|
73
|
-
#
|
74
|
-
#
|
75
|
-
#
|
76
|
-
#
|
77
|
-
# app_properties = @api.admin_getAppProperties :properties => ['application_name','callback_url']
|
78
|
-
# </pre></code>
|
72
|
+
# Example code:
|
73
|
+
# # This is without a parameter
|
74
|
+
# @api = API.new 'MY_API_KEY, 'MY_SECRET_KEY'
|
75
|
+
# token = @api.auth_createToken
|
76
|
+
# # This is with a parameter
|
77
|
+
# app_properties = @api.admin_getAppProperties :properties => ['application_name','callback_url']
|
79
78
|
class API
|
80
79
|
attr_accessor :session_key
|
81
80
|
|
@@ -85,18 +84,49 @@ module ReFacebook
|
|
85
84
|
@api_key = api_key
|
86
85
|
@secret = secret
|
87
86
|
@session_key = nil
|
87
|
+
|
88
|
+
# For batch operations.
|
89
|
+
@in_batch = false
|
90
|
+
@method_feed = []
|
88
91
|
end
|
89
92
|
|
90
|
-
#
|
91
|
-
|
92
|
-
|
93
|
+
# Run a batch call to the API. This doesn't return a APIError if there are
|
94
|
+
# errors since some of the calls could have valid responses. Please catch
|
95
|
+
# the errors yourselves!
|
96
|
+
#
|
97
|
+
# Example code:
|
98
|
+
# ret = @api.batch do |b|
|
99
|
+
# b.auth_createToken
|
100
|
+
# b.admin_getAppProperties :properties => ['application_name','callback_url']
|
101
|
+
# end
|
102
|
+
def batch &block
|
103
|
+
@in_batch = true
|
104
|
+
block.call(self)
|
105
|
+
@in_batch = false
|
106
|
+
|
107
|
+
ret = self.batch_run :call_id => Time.now, :method_feed => @method_feed.to_json
|
108
|
+
|
109
|
+
@method_feed = []
|
110
|
+
|
111
|
+
ret
|
93
112
|
end
|
94
113
|
|
95
114
|
def method_missing method, *args
|
96
115
|
request = {}
|
97
116
|
|
98
117
|
args[0].each do |k,v|
|
99
|
-
request[k.to_s] =
|
118
|
+
request[k.to_s] = \
|
119
|
+
if v.kind_of?(Array) && @in_batch
|
120
|
+
# If we are in a batch should return a escaped string
|
121
|
+
# since we will be within an another array
|
122
|
+
CGI.escape(v.to_json)
|
123
|
+
elsif v.kind_of?(Array) && !@in_batch
|
124
|
+
v.to_json
|
125
|
+
elsif v.kind_of?(Time)
|
126
|
+
v.to_f
|
127
|
+
else
|
128
|
+
v
|
129
|
+
end
|
100
130
|
end if args[0]
|
101
131
|
|
102
132
|
request['api_key'] = @api_key
|
@@ -107,14 +137,20 @@ module ReFacebook
|
|
107
137
|
|
108
138
|
request['sig'] = generate_sig(request.sort)
|
109
139
|
|
140
|
+
if @in_batch
|
141
|
+
# If we are in a batch call just return the formatted request for method_feed
|
142
|
+
@method_feed << request.collect { |k,v| "#{k}=#{v}" }.join('&')
|
143
|
+
return
|
144
|
+
end
|
145
|
+
|
110
146
|
req = Net::HTTP.post_form(URI.parse(APIRestServer), request)
|
111
147
|
ret = JSON.parse("[#{req.body}]")[0]
|
112
148
|
|
113
|
-
if ret.
|
149
|
+
if ret.kind_of?(Hash) and ret.has_key?('error_code')
|
114
150
|
raise APIError.new(ret), ret['error_msg']
|
115
151
|
end
|
116
152
|
|
117
|
-
ret
|
153
|
+
return ret
|
118
154
|
end
|
119
155
|
|
120
156
|
private
|
data/spec/api_spec.rb
CHANGED
@@ -53,10 +53,16 @@ describe ReFacebook::API do
|
|
53
53
|
end
|
54
54
|
|
55
55
|
it "does a batch.run" do
|
56
|
-
|
57
|
-
|
56
|
+
ret = @api.batch do |b|
|
57
|
+
b.auth_createToken
|
58
|
+
b.admin_getAppProperties :properties => ['application_name','callback_url']
|
59
|
+
end
|
60
|
+
|
61
|
+
ret.length.should == 2
|
58
62
|
|
59
|
-
|
60
|
-
|
63
|
+
# TODO: Error in JSON.parse. Right now just check that application_name is availiable
|
64
|
+
app_properties = ret[1]
|
65
|
+
app_properties['application_name'].should == 'application_name'
|
66
|
+
app_properties['traytwo'].should == 'traytwo'
|
61
67
|
end
|
62
68
|
end
|