Class: Translatomatic::HTTPRequest
- Inherits:
-
Object
- Object
- Translatomatic::HTTPRequest
- Defined in:
- lib/translatomatic/http_request.rb
Overview
HTTP request wrapper for Net::HTTP functionality
Defined Under Namespace
Instance Attribute Summary collapse
-
#multipart_boundary ⇒ String
The text to use to denote multipart boundaries.
Instance Method Summary collapse
-
#file(*args) ⇒ FileParam
Create a file parameter for a multipart POST request.
-
#get(query = nil) ⇒ Net::HTTP::Response
Send a HTTP GET request.
-
#initialize(url) ⇒ Translatomatic::HTTPRequest
constructor
Create a new request.
-
#param(*args) ⇒ Param
Create a parameter for a multipart POST request.
-
#post(body, options = {}) ⇒ Net::HTTP::Response
Send an HTTP POST request.
-
#start(options = {}) ⇒ Object
Start the HTTP request.
Constructor Details
#initialize(url) ⇒ Translatomatic::HTTPRequest
Returns Create a new request.
15 16 17 18 |
# File 'lib/translatomatic/http_request.rb', line 15 def initialize(url) @uri = url.respond_to?(:host) ? url : URI.parse(url) @multipart_boundary = SecureRandom.hex(16) end |
Instance Attribute Details
#multipart_boundary ⇒ String
Returns the text to use to denote multipart boundaries. By default, a random hexadecimal string is used.
11 12 13 |
# File 'lib/translatomatic/http_request.rb', line 11 def multipart_boundary @multipart_boundary end |
Instance Method Details
#file(*args) ⇒ FileParam
Create a file parameter for a multipart POST request
72 73 74 |
# File 'lib/translatomatic/http_request.rb', line 72 def file(*args) FileParam.new(*args) end |
#get(query = nil) ⇒ Net::HTTP::Response
Send a HTTP GET request
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/translatomatic/http_request.rb', line 37 def get(query = nil) uri = @uri if query uri = @uri.dup uri.query = URI.encode_www_form(query) end request = Net::HTTP::Get.new(uri) request['User-Agent'] = USER_AGENT send_request(request) end |
#param(*args) ⇒ Param
Create a parameter for a multipart POST request
78 79 80 |
# File 'lib/translatomatic/http_request.rb', line 78 def param(*args) Param.new(*args) end |
#post(body, options = {}) ⇒ Net::HTTP::Response
Send an HTTP POST request
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/translatomatic/http_request.rb', line 51 def post(body, = {}) request = Net::HTTP::Post.new(@uri) request['User-Agent'] = USER_AGENT content_type = [:content_type] if [:multipart] content_type = "multipart/form-data; boundary=#{@multipart_boundary}" request.body = multipartify(body) elsif body.kind_of?(Hash) # set_form_data does url encoding request.set_form_data(body) else request.body = body end request.content_type = content_type if content_type send_request(request) end |
#start(options = {}) ⇒ Object
Start the HTTP request. Yields a http object.
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/translatomatic/http_request.rb', line 23 def start( = {}) = .merge(use_ssl: @uri.scheme == "https") result = nil Net::HTTP.start(@uri.host, @uri.port, ) do |http| @http = http result = yield http end @http = nil result end |