Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
service_m8 / lib / service_m8 / http_utils / request.rb
Size: Mime:
module ServiceM8
  class HttpUtils
    # Defines HTTP request methods
    module Request
      # Perform an HTTP GET request
      def get(path, options = {})
        request(:get, path, options)
      end

      # Perform an HTTP POST request
      def post(path, options = {})
        request(:post, path, options)
      end

      private

      # Perform an HTTP request
      def request(method, path, options)
        http_response = connection.send(method) do |request|
          case method
            when :get
              formatted_options = format_options(options)
              request.url(path, formatted_options)
            when :post, :put
              request.headers['Content-Type'] = 'application/json'
              request.body = options.to_json unless options.empty?
              request.url(path)
          end
          request.options.timeout = 120 # read timeout
          request.options.open_timeout = 300 # connection timeout
        end
        data = Response.create(http_response.body)
        response = Hash.new
        if http_response.headers.include? 'x-next-cursor'
          response['next-cursor'] = http_response.headers['x-next-cursor']
        end
        response['data'] = data
        response
      end

      # Format the Options before you send them off to ServiceM8
      def format_options(options)
        return if options.blank?
        opts = {}
        opts["$filter"] = options[:filter] if options.has_key?(:filter)
        opts["cursor"] = options[:cursor] if options.has_key?(:cursor)
        opts
      end
    end
  end
end