lib/motion-resource/requests.rb in motion-resource-0.1.3 vs lib/motion-resource/requests.rb in motion-resource-0.1.4
- old
+ new
@@ -27,11 +27,31 @@
def on_auth_failure(&block)
@on_auth_failure = block
end
- private
+ protected
+
+ def decode_response(response, url, options)
+ if response.ok?
+ body = response.body.to_str.strip rescue nil
+ logger.log "response: #{body}"
+ if body.blank?
+ return {}
+ else
+ return BubbleWrap::JSON.parse(body)
+ end
+ else
+ if response.status_code.to_s =~ /401/ && @on_auth_failure
+ @on_auth_failure.call
+ end
+ return nil
+ end
+ end
+
+ private
+
def complete_url(fragment)
if fragment[0..3] == "http"
return fragment
end
(self.root_url || MotionResource::Base.root_url) + fragment
@@ -41,33 +61,22 @@
url = complete_url(url)
options = call_options
options.merge!(MotionResource::Base.default_url_options || {})
if query = options.delete(:query)
- url.build_query_string!(query)
+ url = self.url_encoder.build_query_string(url, query)
end
if self.default_url_options
options.merge!(self.default_url_options)
end
- url.insert_extension!(self.extension)
+ url = self.url_encoder.insert_extension(url, self.extension)
logger.log "#{method.upcase} #{url}"
+ logger.log "payload: #{options[:payload]}" if options[:payload]
BubbleWrap::HTTP.send(method, url, options) do |response|
- if response.ok?
- body = response.body.to_str.strip rescue nil
- logger.log "response: #{body}"
- if body.blank?
- block.call(response, {})
- else
- block.call response, BubbleWrap::JSON.parse(body)
- end
- elsif response.status_code.to_s =~ /401/ && @on_auth_failure
- @on_auth_failure.call
- else
- block.call response, nil
- end
+ block.call response, decode_response(response, url, options)
end
end
end
end
end