mini_fb 0.2.2 → 0.2.3

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.
Files changed (3) hide show
  1. data/README.markdown +11 -1
  2. data/lib/mini_fb.rb +47 -9
  3. metadata +16 -5
data/README.markdown CHANGED
@@ -28,7 +28,7 @@ Get a MiniFB::Session:
28
28
 
29
29
  Then it makes it a bit easier to use call for a particular user/session.
30
30
 
31
- response = @fb.calls("stream.get")
31
+ response = @fb.call("stream.get")
32
32
 
33
33
  With the session, you can then get the user information for the session/uid.
34
34
 
@@ -73,6 +73,16 @@ Define an fb_connect method in your login/sessions controller like so:
73
73
  end
74
74
 
75
75
 
76
+ Photo Uploads
77
+ -------------
78
+
79
+ This is as simple as calling:
80
+
81
+ @fb.call("photos.upload", "file_name"=>"<full path to file>")
82
+
83
+ The file_name parameter will be used as the file data.
84
+
85
+
76
86
  Support
77
87
  --------
78
88
 
data/lib/mini_fb.rb CHANGED
@@ -142,41 +142,52 @@ module MiniFB
142
142
 
143
143
  # Prepare arguments for call
144
144
  call_id = kwargs.fetch("call_id", true)
145
- if call_id == true then
145
+ if call_id == true
146
146
  kwargs["call_id"] = Time.now.tv_sec.to_s
147
147
  else
148
148
  kwargs.delete("call_id")
149
149
  end
150
150
 
151
- custom_format = kwargs.include?("format") or kwargs.include?("callback")
151
+ custom_format = kwargs.include?("format") || kwargs.include?("callback")
152
152
  kwargs["format"] ||= "JSON"
153
153
  kwargs["v"] ||= FB_API_VERSION
154
154
  kwargs["api_key"]||= api_key
155
155
  kwargs["method"] ||= method
156
156
 
157
+ file_name = kwargs.delete("filename")
158
+
157
159
  # Hash with secret
158
160
  arg_string = String.new
159
161
  # todo: convert symbols to strings, symbols break the next line
160
162
  kwargs.sort.each { |kv| arg_string << kv[0] << "=" << kv[1].to_s }
161
163
  kwargs["sig"] = Digest::MD5.hexdigest( arg_string + secret.value.call )
162
164
 
163
- # Call website with POST request
164
- begin
165
- response = Net::HTTP.post_form( URI.parse(FB_URL), kwargs )
166
- rescue SocketError => err
167
- raise IOError.new( "Cannot connect to the facebook server: " + err )
165
+ fb_method = kwargs["method"].downcase
166
+ if fb_method == "photos.upload"
167
+ # Then we need a multipart post
168
+ response = MiniFB.post_upload(file_name, kwargs)
169
+ else
170
+
171
+ begin
172
+ response = Net::HTTP.post_form( URI.parse(FB_URL), kwargs )
173
+ rescue SocketError => err
174
+ # why are we catching this and throwing as different error? hmmm..
175
+ # raise IOError.new( "Cannot connect to the facebook server: " + err )
176
+ raise err
177
+ end
168
178
  end
169
179
 
180
+
170
181
  # Handle response
171
182
  return response.body if custom_format
172
183
 
173
- fb_method = kwargs["method"].downcase
184
+
174
185
  body = response.body
175
186
 
176
187
  puts 'response=' + body.inspect if @@logging
177
188
  begin
178
189
  data = JSON.parse( body )
179
- if data.include?( "error_msg" ) then
190
+ if data.include?( "error_msg" )
180
191
  raise FaceBookError.new( data["error_code"] || 1, data["error_msg"] )
181
192
  end
182
193
 
@@ -193,6 +204,33 @@ module MiniFB
193
204
  return data
194
205
  end
195
206
 
207
+ def MiniFB.post_upload(filename, kwargs)
208
+ content = File.open(filename, 'rb') { |f| f.read }
209
+ boundary = Digest::MD5.hexdigest(content)
210
+ header = {'Content-type' => "multipart/form-data, boundary=#{boundary}"}
211
+
212
+ # Build query
213
+ query = ''
214
+ kwargs.each { |a, v|
215
+ query <<
216
+ "--#{boundary}\r\n" <<
217
+ "Content-Disposition: form-data; name=\"#{a}\"\r\n\r\n" <<
218
+ "#{v}\r\n"
219
+ }
220
+ query <<
221
+ "--#{boundary}\r\n" <<
222
+ "Content-Disposition: form-data; filename=\"#{File.basename(filename)}\"\r\n" <<
223
+ "Content-Transfer-Encoding: binary\r\n" <<
224
+ "Content-Type: image/jpeg\r\n\r\n" <<
225
+ content <<
226
+ "\r\n" <<
227
+ "--#{boundary}--"
228
+
229
+ # Call Facebook with POST multipart/form-data request
230
+ uri = URI.parse(FB_URL)
231
+ Net::HTTP.start(uri.host) {|http| http.post uri.path, query, header}
232
+ end
233
+
196
234
  # Returns true is signature is valid, false otherwise.
197
235
  def MiniFB.verify_signature( secret, arguments )
198
236
  signature = arguments.delete( "fb_sig" )
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 2
9
- version: 0.2.2
8
+ - 3
9
+ version: 0.2.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Travis Reeder
@@ -15,10 +15,21 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-03-02 00:00:00 -08:00
18
+ date: 2010-04-05 00:00:00 -07:00
19
19
  default_executable:
20
- dependencies: []
21
-
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rest-client
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :runtime
32
+ version_requirements: *id001
22
33
  description: Tiny facebook library
23
34
  email: travis@appoxy.com
24
35
  executables: []