lib/afmotion/http.rb in afmotion-0.9.0 vs lib/afmotion/http.rb in afmotion-2.0.0.rc1

- old
+ new

@@ -1,92 +1,52 @@ -module AFMotion - module HTTPBuilder - def self.included(base) - AFMotion::HTTP_METHODS.each do |method| - base.send(:define_singleton_method, method, -> (request_or_url, parameters = {}, &callback) do - request = request_or_url - if !request.is_a?(NSURLRequest) - request = NSMutableURLRequest.requestWithURL(request_or_url.to_url) - request.HTTPMethod = method.upcase - if [:get, :head].member? method.downcase.to_sym - request.HTTPShouldUsePipelining = true - end - # SEE NSURLRequest_params.rb - request.parameters = parameters.merge(__encoding__: self.parameter_encoding) - end +motion_require 'version' - operation = (self.request_module.for_request(request) do |result| - callback.call(result) - end) - - operation.start - operation - end) +module AFMotion + class HTTP + def self.operation_manager + @operation_manager ||= begin + manager = AFHTTPRequestOperationManager.manager + configure_manager(manager) + manager end end - end - module HTTP - include AFMotion::HTTPBuilder - - module_function - def request_module - AFMotion::Operation::HTTP + def self.configure_manager(manager) end + end - def parameter_encoding - AFFormURLParameterEncoding + class JSON < HTTP + def self.configure_manager(manager) + manager.json! end end - module JSON - include AFMotion::HTTPBuilder - - module_function - def request_module - AFMotion::Operation::JSON + class XML < HTTP + def self.configure_manager(manager) + manager.xml! end - - def parameter_encoding - AFJSONParameterEncoding - end end - module XML - include AFMotion::HTTPBuilder - - module_function - def request_module - AFMotion::Operation::XML + class PLIST < HTTP + def self.configure_manager(manager) + manager.plist! end - - def parameter_encoding - AFFormURLParameterEncoding - end end - module PLIST - include AFMotion::HTTPBuilder - - module_function - def request_module - AFMotion::Operation::PLIST + class Image < HTTP + def self.configure_manager(operation) + operation.image! end - - def parameter_encoding - AFPropertyListParameterEncoding - end end - module Image - include AFMotion::HTTPBuilder - - module_function - def request_module - AFMotion::Operation::Image - end - - def parameter_encoding - AFFormURLParameterEncoding + [HTTP, JSON, XML, PLIST, Image].each do |base| + AFMotion::HTTP_METHODS.each do |method_name| + method_signature = "#{method_name.to_s.upcase}:parameters:success:failure:" + base.define_singleton_method(method_name, -> (url, parameters = {}, &callback) do + base.operation_manager.send(method_signature, url, + parameters, + AFMotion::Operation.success_block_for_http_method(method_name, callback), + AFMotion::Operation.failure_block(callback)) + end) end end end \ No newline at end of file