lib/api-client/dispatcher.rb in api-client-1.5.4 vs lib/api-client/dispatcher.rb in api-client-1.6.0
- old
+ new
@@ -1,72 +1,13 @@
-require "net/http"
-
-# ApiClient::Dispatcher provides methods to make requests using the native ruby library 'net/http'
+# ApiClient::Dispatcher provides methods to make requests
module ApiClient::Dispatcher
- # Make a get request and returns it.
- #
- # @param [String] url of the api request.
- # @param [Hash] header attributes of the request.
- # @return [HTTP] the response object.
- def self.get(url, header = {})
- initialize_connection(url)
- call { @http.get(@uri.path, header) }
- end
+ autoload :Typhoeus, 'api-client/dispatcher/typhoeus'
+ autoload :NetHttp, 'api-client/dispatcher/net-http'
- # Make a post request and returns it.
- #
- # @param [String] url of the api request.
- # @param [Hash] args attributes of object.
- # @param [Hash] header attributes of the request.
- # @return [HTTP] the response object.
- def self.post(url, args, header = {})
- initialize_connection(url)
- call { @http.post(@uri.path, args.to_json, { 'Content-Type' => 'application/json' }.merge(header)) }
- end
-
- # Make a put request and returns it.
- #
- # @param [String] url of the api request.
- # @param [Hash] args attributes of object.
- # @param [Hash] header attributes of the request.
- # @return [HTTP] the response object.
- def self.put(url, args, header = {})
- initialize_connection(url)
- call { @http.put(@uri.path, args.to_json, { 'Content-Type' => 'application/json' }.merge(header)) }
- end
-
- # Make a patch request and returns it.
- #
- # @param [String] url of the api request.
- # @param [Hash] args attributes of object.
- # @param [Hash] header attributes of the request.
- # @return [HTTP] the response object.
- def self.patch(url, args, header = {})
- initialize_connection(url)
- call { @http.patch(@uri.path, args.to_json, { 'Content-Type' => 'application/json' }.merge(header)) }
- end
-
- # Make a delete request and returns it.
- #
- # @param [String] url of the api request.
- # @param [Hash] header attributes of the request.
- # @return [HTTP] the response object.
- def self.delete(url, header = {})
- initialize_connection(url)
- call { @http.delete(@uri.path, header) }
- end
-
- protected
-
- def self.initialize_connection(url = '')
- @uri = URI(url)
- @http = Net::HTTP.new(@uri.host, @uri.port)
- end
-
- def self.call
- begin
- yield
- rescue Errno::ECONNREFUSED
- raise ApiClient::Exceptions::ConnectionRefused
+ def self.method_missing(method, *args)
+ if defined?(::Typhoeus)
+ Typhoeus.send(method, *args)
+ else
+ NetHttp.send(method, *args)
end
end
end
\ No newline at end of file