mjfreshyfresh-mini_fb 0.1.17 → 0.1.18

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/mini_fb.rb +25 -13
  2. data/test/test_mini_fb.rb +18 -0
  3. metadata +1 -1
data/lib/mini_fb.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'digest/md5'
2
2
  require 'erb'
3
+ require 'hashie'
3
4
  require 'json' unless defined? JSON
4
5
  require 'net/http'
5
6
  require 'cgi' unless defined? Rack
@@ -138,6 +139,8 @@ module MiniFB
138
139
  # to hide value from simple introspection.
139
140
  def MiniFB.call( api_key, secret, method, kwargs )
140
141
 
142
+ kwargs = Hashie::Mash.new kwargs
143
+
141
144
  puts 'kwargs=' + kwargs.inspect if @@logging
142
145
 
143
146
  if secret.is_a? String
@@ -160,7 +163,6 @@ module MiniFB
160
163
 
161
164
  # Hash with secret
162
165
  arg_string = String.new
163
- kwargs.each { |k,v| (kwargs[k.to_s] = v ; kwargs.delete(k)) if k.is_a? Symbol }
164
166
  kwargs.sort.each { |kv| arg_string << kv[0] << "=" << kv[1].to_s }
165
167
  kwargs["sig"] = Digest::MD5.hexdigest( arg_string + secret.value.call )
166
168
 
@@ -179,23 +181,33 @@ module MiniFB
179
181
  rescue
180
182
  raise
181
183
  end
182
-
184
+
183
185
  # Handle response
184
- return response.body if custom_format
186
+
187
+ body = response.body
188
+ return body if custom_format
189
+ unescaped_attempt = false
185
190
 
186
191
  begin
187
- data = JSON.parse( response.body )
188
- puts 'response=' + data.inspect if @@logging
189
- if data.include?( "error_msg" ) then
190
- raise FaceBookError.new( data["error_code"] || 1, data["error_msg"] )
191
- end
192
-
192
+ data = JSON.parse body
193
+ puts 'response=' + data.inspect if @@logging
194
+ if data.include?( "error_msg" ) then
195
+ raise FaceBookError.new( data["error_code"] || 1, data["error_msg"] )
196
+ end
193
197
  rescue JSON::ParserError => ex
194
- return response.body if BAD_JSON_METHODS.include? kwargs["method"]
195
- return (response.body == 'true') if %w{true false}.include?(response.body) # Hack for Facebook boolean API calls with BAD JSON.
196
- raise BadJSONDataError, "#{kwargs["method"]} returned \"#{response.body}\""
198
+ return body if BAD_JSON_METHODS.include? kwargs["method"]
199
+ return (body == 'true') if %w{true false}.include?(body) # Hack for Facebook boolean API calls with BAD JSON.
200
+
201
+ unless unescaped_attempt
202
+ unescaped_attempt = true
203
+ body = body[/\A"(.*)"\z/m,1].gsub('\\', '')
204
+ retry
205
+ end
206
+
207
+ raise BadJSONDataError, "#{kwargs["method"]} returned \"#{body}\""
197
208
  end
198
- return data
209
+
210
+ data.is_a?(Hash) ? Hashie::Mash.new(data) : data
199
211
  end
200
212
 
201
213
  # Returns true is signature is valid, false otherwise.
data/test/test_mini_fb.rb CHANGED
@@ -41,9 +41,27 @@ class MiniFBTests < Test::Unit::TestCase
41
41
  test 'should return raw JSON if JSON format is directly specified' do
42
42
  results = @facebook_session.call 'Friends.get'
43
43
  results_raw = @facebook_session.call 'Friends.get', 'format' => 'JSON'
44
+ assert results_raw.is_a?(String)
44
45
  assert results == JSON.parse(results_raw)
45
46
  end
46
47
 
48
+ test 'should not create Hashie object if return is array' do
49
+ results = @facebook_session.call 'Friends.get'
50
+ assert results.is_a?(Array)
51
+ end
52
+
53
+ test 'should not create Hashie object if return is string' do
54
+ results = @facebook_session.call 'users.getLoggedInUser'
55
+ assert results.is_a?(String)
56
+ end
57
+
58
+ test 'should create Hashie object if return is Hash' do
59
+ results = @facebook_session.call 'Admin.getAppProperties', :properties => ['app_id', 'application_name'].to_json
60
+ assert results.is_a?(Hashie::Mash)
61
+ assert results.app_id.is_a?(Fixnum)
62
+ assert results.application_name.is_a?(String)
63
+ end
64
+
47
65
  test 'should return XML if format is XML' do
48
66
  results = @facebook_session.call 'Friends.get', 'format' => 'xml'
49
67
  returned_at_least_one_response_element = false
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mjfreshyfresh-mini_fb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.17
4
+ version: 0.1.18
5
5
  platform: ruby
6
6
  authors:
7
7
  - Travis Reeder