Class: Translatomatic::HTTP::Client

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/translatomatic/http/client.rb

Overview

HTTP client

Defined Under Namespace

Classes: HttpRetryExecutor

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



7
8
9
10
11
# File 'lib/translatomatic/http/client.rb', line 7

def initialize(options = {})
  @redirects = 0
  @jar = ::HTTP::CookieJar.new
  @retry_delay = options[:retry_delay] || 2
end

Instance Method Details

#delete(url, options = {}) ⇒ Net::HTTP::Response

Send an HTTP DELETE request

Parameters:

  • url (String, URI)

    URL of the request

  • options (Hash<Symbol,Object>) (defaults to: {})

    Request options

Returns:

  • (Net::HTTP::Response)


43
44
45
# File 'lib/translatomatic/http/client.rb', line 43

def delete(url, options = {})
  send_request_with_method(:delete, url, options)
end

#get(url, query = nil, options = {}) ⇒ Net::HTTP::Response

Send an HTTP GET request

Parameters:

  • url (String, URI)

    URL of the request

  • query (Hash<String,String>) (defaults to: nil)

    Optional query parameters

  • options (Hash<Symbol,Object>) (defaults to: {})

    Request options

Returns:

  • (Net::HTTP::Response)


18
19
20
# File 'lib/translatomatic/http/client.rb', line 18

def get(url, query = nil, options = {})
  send_request_with_method(:get, url, options.merge(query: query))
end

#head(url, options = {}) ⇒ Net::HTTP::Response

Send an HTTP HEAD request

Parameters:

  • url (String, URI)

    URL of the request

  • options (Hash<Symbol,Object>) (defaults to: {})

    Request options

Returns:

  • (Net::HTTP::Response)


35
36
37
# File 'lib/translatomatic/http/client.rb', line 35

def head(url, options = {})
  send_request_with_method(:head, url, options)
end

#post(url, body, options = {}) ⇒ Net::HTTP::Response

Send an HTTP POST request

Parameters:

  • url (String, URI)

    URL of the request

  • body (String, Hash)

    Body of the request

  • options (Hash<Symbol,Object>) (defaults to: {})

    Request options

Returns:

  • (Net::HTTP::Response)


27
28
29
# File 'lib/translatomatic/http/client.rb', line 27

def post(url, body, options = {})
  send_request_with_method(:post, url, options.merge(body: body))
end

#start(url, _options = {}) ⇒ Object

Start an HTTP request. Yields the http object.

Parameters:

  • url (String, URI)

    URL of the request, requires host and port

Returns:

  • (Object)

    The result of the yielded block



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/translatomatic/http/client.rb', line 50

def start(url, _options = {})
  uri = url.respond_to?(:host) ? url : URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == 'https'
  # http.set_debug_output(Translatomatic.config.logger) if ENV['DEBUG']
  result = http.start do
    @http = http
    yield http
  end
  @http = nil
  result
end