class Infosimples::Data::HTTP
Infosimples::Data::HTTP
exposes common HTTP
routines that can be used by the Infosimples::Data
API client.
Public Class Methods
prepare_multipart_data(payload)
click to toggle source
Prepare the multipart data to be sent via a :multipart request.
@param [Hash] payload Data
to be prepared via a multipart post.
@return [String, String] Boundary and body for the multipart post.
# File lib/infosimples/data/http.rb, line 60 def self.prepare_multipart_data(payload) boundary = 'randomstr' + rand(1_000_000).to_s # a random unique string content = [] payload.each do |param, value| content << '--' + boundary content << "Content-Disposition: form-data; name=\"#{param}\"" content << '' content << value end content << '--' + boundary + '--' content << '' [boundary, content.join("\r\n")] end
request(options = {})
click to toggle source
Perform an HTTP
request with support for multipart requests.
@param [Hash] options Options hash. @param options [String] url URL to be requested. @param options [Symbol] method HTTP
method (:get, :post, :multipart). @param options [Hash] payload Data
to be sent through the HTTP
request. @param options [Integer] http_timeout HTTP
open/read timeout in seconds.
@return [String] Response body of the HTTP
request.
# File lib/infosimples/data/http.rb, line 16 def self.request(options = {}) uri = URI(options[:url]) method = options[:method] || :get payload = options[:payload] || {} timeout = options[:http_timeout] headers = { 'User-Agent' => Infosimples::Data::USER_AGENT } case method when :get uri.query = URI.encode_www_form(payload) req = Net::HTTP::Get.new(uri.request_uri, headers) when :post req = Net::HTTP::Post.new(uri.request_uri, headers) req.set_form_data(payload) when :multipart req = Net::HTTP::Post.new(uri.request_uri, headers) boundary, body = prepare_multipart_data(payload) req.content_type = "multipart/form-data; boundary=#{boundary}" req.body = body else fail Infosimples::Data::ArgumentError, "Illegal HTTP method (#{method})" end http = Net::HTTP.new(uri.hostname, uri.port) http.use_ssl = true if (uri.scheme == 'https') http.open_timeout = timeout http.read_timeout = timeout + 10 http.max_retries = 0 if http.respond_to?(:max_retries) # fix max_retries res = http.request(req) res.body rescue Net::OpenTimeout, Net::ReadTimeout raise Infosimples::Data::Timeout end