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    
Size: Mime:
module FieldEdgeApi
  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
        return Response.create(http_response.body)
      end

      # Format the Options before you send them off to Kickserv
      def format_options(options)
        return if options.blank?
        opts = {}
        options.each do |key, value|
          if key == :afterLastModifiedDate
            opts[:afterLastModifiedDate] = options[:afterLastModifiedDate] if options.has_key?(:afterLastModifiedDate)
          elsif key == :afterTransactionDate
            opts[:afterTransactionDate] = format_fields(options[:afterTransactionDate]) if options.has_key?(:afterTransactionDate)
          elsif key == :afterOriginalContractDate
            opts[:afterOriginalContractDate] = format_fields(options[:afterOriginalContractDate]) if options.has_key?(:afterOriginalContractDate)
          elsif key == :offset
            opts[:offset] = format_fields(options[:offset]) if options.has_key?(:offset)
          else
            opts[key] = options[key]
          end
        end
        opts
      end

      # Format the fields to a format that the Kickserv likes
      # @param [Array or String] fields can be specified as an Array or String
      # @return String
      def format_fields(fields)
        if fields.instance_of?(Array)
          return fields.join(",")
        else
          return fields.to_s
        end
      end
    end
  end
end