Class: EaseHTTP::Connection
- Inherits:
-
Object
- Object
- EaseHTTP::Connection
- Defined in:
- lib/ease_http/connection.rb
Instance Method Summary collapse
-
#get(path, options = {}, &block) ⇒ Object
path - path to request options :headers :query.
-
#initialize(endpoint, options = {}) ⇒ Connection
constructor
A new instance of Connection.
-
#post(path, options = {}, &block) ⇒ Object
path - path to request options :headers :data.
Constructor Details
#initialize(endpoint, options = {}) ⇒ Connection
Returns a new instance of Connection.
8 9 10 11 12 13 14 15 16 |
# File 'lib/ease_http/connection.rb', line 8 def initialize(endpoint, ={}) @endpoint = endpoint uri = URI(endpoint) # timeout = options.delete(:timeout) || 0.2 @http = Net::HTTP.new(uri.host, uri.port) @http.use_ssl = uri.scheme == 'https' # @http.read_timeout = timeout end |
Instance Method Details
#get(path, options = {}, &block) ⇒ Object
path - path to request options
:headers
:query
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/ease_http/connection.rb', line 22 def get(path, ={}, &block) uri = URI.join(@endpoint, path) headers = .delete(:headers) query = .delete(:query) if query uri.query = URI.encode_www_form(query) end request = Net::HTTP::Get.new uri.request_uri, headers @http.request request, &block end |
#post(path, options = {}, &block) ⇒ Object
path - path to request options
:headers
:data
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/ease_http/connection.rb', line 39 def post(path, ={}, &block) headers = .delete :headers data = .delete :data content_type = (headers && headers['Content-Type']) || content_type(data) request = Net::HTTP::Post.new path, headers # convert hash data to array data data = data.inject([]) do |memo, (key, value)| if value.is_a?(File) memo << [key.to_s, value, { content_type: Utils.mime_type(value.path) }] else memo << [key.to_s, value] end end request.set_form data, content_type @http.request request, &block end |