All Downloads are FREE. Search and download functionalities are using the official Maven repository.

scalaz.http.request.Method.scala Maven / Gradle / Ivy

The newest version!
package scalaz
package http
package request

import Util.Nel._
import Scalaz._

/**
 * HTTP request method.
 * RFC 2616 Section 9 Method Definitions.
 *
 * @author Tony Morris
 */
sealed trait Method {
  /**
   * A string representation of this request method.
   */
  val asString: String

  /**
   * A non-empty string representation of this request method.
   */
  lazy val asNonEmptyList: NonEmptyList[Char] = asString.charsNel.get

  /**
   * Returns true if this method is an extension method, false otherwise.
   */
  lazy val isExtension = this match {
    case ExtensionMethod(_) => true
    case _ => false
  }

  /**
   * Returns the result of the given function to this method if it is an extension method, otherwise returns the given
   * value.
   */
  def extension[X](f: NonEmptyList[Char] => X, x: => X) = this match {
    case ExtensionMethod(m) => f(m)
    case _ => x
  }
}

/**
 * A HTTP method whose name comes from toString().
 */
trait CaseMethod extends Method {
  override val asString = this.toString
}

/**
 * §
 */
case object OPTIONS extends CaseMethod

/**
 * §
 */
case object GET extends CaseMethod
/**
 * §
 */
case object HEAD extends CaseMethod
/**
 * §
 */
case object POST extends CaseMethod
/**
 * §
 */
case object PUT extends CaseMethod
/**
 * §
 */
case object DELETE extends CaseMethod
/**
 * §
 */
case object TRACE extends CaseMethod
/**
 * §
 */
case object CONNECT extends CaseMethod

private final case class ExtensionMethod(m: NonEmptyList[Char]) extends Method {
  val asString = m.mkString
}

trait Methods {
  /**
   * Converts the given non-empty string representation into a request method. If it is a known request method then that
   * is used, otherwise an extension method is returned.
   */
  implicit def NonEmptyListMethod(s: NonEmptyList[Char]): Method = StringMethod(s.mkString).get

  /**
   * Converts the given string representation into a request method. If it is a known request method then that
   * is used, otherwise an extension method is returned.
   */
  implicit def ListMethod : (List[Char] => Option[Method]) = StringMethod _ compose (_.mkString)

  /**
   * Returns a string representation of the given request method.
   */
  implicit def MethodString(m: Method) = m.asString

  /**
   * Converts the given non-empty string representation into a request method. If it is a known request method then that
   * is used, otherwise an extension method is returned.
   */
  implicit def StringMethod(s: String): Option[Method] = if(s.length == 0) None else Some(s.toLowerCase match {
    case "options" => OPTIONS
    case "get" => GET
    case "head" => HEAD
    case "post" => POST
    case "put" => PUT
    case "delete" => DELETE
    case "trace" => TRACE
    case "connect" => CONNECT
    case m => {
      val t : List[Char] = (s : scala.collection.immutable.StringOps).toList
      ExtensionMethod(nel(t.head, t.tail))
    }
  })
}

/**
 * HTTP request method.
 * RFC 2616 Section 9 Method Definitions.
 */
object Method extends Methods {
  /**
   * A list of known methods.
   */
  val methods = List(OPTIONS,GET,HEAD,POST,PUT,DELETE,TRACE,CONNECT)

  /**
   * An extractor that always matches with a non-empty string representation of this request method.
   */
  def unapply(m: Method) = Some(m.asNonEmptyList)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy