module SwiftypeAppSearch::Request

Attributes

last_request[RW]

Public Instance Methods

delete(path, params={}) click to toggle source
# File lib/swiftype-app-search/request.rb, line 31
def delete(path, params={})
  request(:delete, path, params)
end
get(path, params={}) click to toggle source
# File lib/swiftype-app-search/request.rb, line 15
def get(path, params={})
  request(:get, path, params)
end
patch(path, params={}) click to toggle source
# File lib/swiftype-app-search/request.rb, line 27
def patch(path, params={})
  request(:patch, path, params)
end
post(path, params={}) click to toggle source
# File lib/swiftype-app-search/request.rb, line 19
def post(path, params={})
  request(:post, path, params)
end
put(path, params={}) click to toggle source
# File lib/swiftype-app-search/request.rb, line 23
def put(path, params={})
  request(:put, path, params)
end
request(method, path, params = {}) click to toggle source

Construct and send a request to the API.

@raise [Timeout::Error] when the timeout expires

# File lib/swiftype-app-search/request.rb, line 38
def request(method, path, params = {})
  Timeout.timeout(overall_timeout) do
    uri = URI.parse("#{api_endpoint}#{path}")

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

    http.set_debug_output(STDERR) if debug?

    if uri.scheme == 'https'
      http.use_ssl = true
      # st_ssl_verify_none provides a means to disable SSL verification for debugging purposes. An example
      # is Charles, which uses a self-signed certificate in order to inspect https traffic. This will
      # not be part of this client's public API, this is more of a development enablement option
      http.verify_mode = ENV['st_ssl_verify_none'] == 'true' ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER
      http.ca_file = File.join(File.dirname(__FILE__), '..', 'data', 'ca-bundle.crt')
      http.ssl_timeout = open_timeout
    end

    @last_request = request

    response = http.request(request)
    response_json = parse_response(response)

    case response
    when Net::HTTPSuccess
      return response_json
    when Net::HTTPBadRequest
      raise SwiftypeAppSearch::BadRequest, response_json
    when Net::HTTPUnauthorized
      raise SwiftypeAppSearch::InvalidCredentials, response_json
    when Net::HTTPNotFound
      raise SwiftypeAppSearch::NonExistentRecord, response_json
    when Net::HTTPForbidden
      raise SwiftypeAppSearch::Forbidden, response_json
    when Net::HTTPRequestEntityTooLarge
      raise SwiftypeAppSearch::RequestEntityTooLarge, response_json
    else
      raise SwiftypeAppSearch::UnexpectedHTTPException.new(response, response_json)
    end
  end
end

Private Instance Methods

build_request(method, uri, params) click to toggle source
# File lib/swiftype-app-search/request.rb, line 120
def build_request(method, uri, params)
  klass = case method
          when :get
            Net::HTTP::Get
          when :post
            Net::HTTP::Post
          when :put
            Net::HTTP::Put
          when :patch
            Net::HTTP::Patch
          when :delete
            Net::HTTP::Delete
          end

  req = klass.new(uri.request_uri)
  req.body = serialize_json(params) unless params.length == 0

  req['X-Swiftype-Client'] = CLIENT_NAME
  req['X-Swiftype-Client-Version'] = CLIENT_VERSION
  req['Content-Type'] = 'application/json'
  req['Authorization'] = "Bearer #{api_key}"

  req
end
clean_atom(atom) click to toggle source
# File lib/swiftype-app-search/request.rb, line 112
def clean_atom(atom)
  if atom.is_a?(Time)
    atom.to_datetime
  else
    atom
  end
end
clean_json(object) click to toggle source
# File lib/swiftype-app-search/request.rb, line 98
def clean_json(object)
  case object
  when Hash
    object.inject({}) do |builder, (key, value)|
      builder[key] = clean_json(value)
      builder
    end
  when Enumerable
    object.map { |value| clean_json(value) }
  else
    clean_atom(object)
  end
end
debug?() click to toggle source
# File lib/swiftype-app-search/request.rb, line 90
def debug?
  @debug ||= (ENV['AS_DEBUG'] == 'true')
end
parse_response(response) click to toggle source
# File lib/swiftype-app-search/request.rb, line 85
def parse_response(response)
  body = response.body.to_s.strip
  body == '' ? {} : JSON.parse(body)
end
serialize_json(object) click to toggle source
# File lib/swiftype-app-search/request.rb, line 94
def serialize_json(object)
  JSON.generate(clean_json(object))
end