Module: Swiftype::Request

Included in:
Client
Defined in:
lib/swiftype/request.rb

Instance Method Summary collapse

Instance Method Details

#delete(path, params = {}) ⇒ Object


25
26
27
# File 'lib/swiftype/request.rb', line 25

def delete(path, params={})
  request(:delete, path, params)
end

#get(path, params = {}) ⇒ Object


13
14
15
# File 'lib/swiftype/request.rb', line 13

def get(path, params={})
  request(:get, path, params)
end

#poll(options = {}) ⇒ Object

Poll a block with backoff until a timeout is reached.

Parameters:

  • options (Hash) (defaults to: {})

    optional arguments

Options Hash (options):

  • :timeout (Numeric) — default: 10

    Number of seconds to wait before timing out

Yield Returns:

  • a truthy value to return from poll

  • (false)

    to continue polling.

Returns:

  • the truthy value returned from the block.

Raises:

  • (Timeout::Error)

    when the timeout expires


40
41
42
43
44
45
46
47
48
49
50
# File 'lib/swiftype/request.rb', line 40

def poll(options={})
  timeout = options[:timeout] || 10
  delay = 0.05
  Timeout.timeout(timeout) do
    while true
      res = yield
      return res if res
      sleep delay *= 2
    end
  end
end

#post(path, params = {}) ⇒ Object


17
18
19
# File 'lib/swiftype/request.rb', line 17

def post(path, params={})
  request(:post, path, params)
end

#put(path, params = {}) ⇒ Object


21
22
23
# File 'lib/swiftype/request.rb', line 21

def put(path, params={})
  request(:put, path, params)
end

#request(method, path, params = {}) ⇒ Object

Construct and send a request to the API.

Raises:

  • (Timeout::Error)

    when the timeout expires


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/swiftype/request.rb', line 55

def request(method, path, params={})
  uri = URI.parse("#{Swiftype.endpoint}#{path}")

  request = build_request(method, uri, params)
  http = Net::HTTP.new(uri.host, uri.port)
  http.open_timeout = open_timeout

  if uri.scheme == 'https'
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_PEER
    http.ca_file = File.join(File.dirname(__FILE__), '..', 'data', 'ca-bundle.crt')
  end

  response = nil
  Timeout.timeout(overall_timeout) do
    response = http.request(request)
  end

  handle_errors(response)

  JSON.parse(response.body) if response.body && response.body.strip != ''
end