NoRedInk/elm-rails/7.0.0

NOTE: this package is not compatible with Elm 0.19.1

Rails

Requests

type alias Error error =
{ http : Error
, rails : Maybe error
}

The kinds of errors a Rails server may return.

get : String -> Decoder val -> Request val

Send a GET request to the given URL. Specify how to decode the response.

import Http
import Json.Decode exposing (list, string, succeed)
import Rails


getHats : Cmd msg
getHats =
    list hatDecoder
        |> Rails.get "http://example.com/hat-categories.json"
        |> Http.send HandleGetHatsResponse
post : String -> Body -> Decoder val -> Request val

Send a POST request to the given URL. Specify how to decode the response.

import Http
import Json.Decode exposing (list, string, succeed)
import Rails


hats : Cmd msg
hats =
    list hatDecoder
        |> Rails.post "http://example.com/hat-categories/new" Http.emptyBody
        |> Http.send HandleResponse

NOTE: Rails typically expects an X-CSRF-Token header for POST requests, which this does not include. To have this header included automatically, add csrf-xhr to the page, before your Elm program gets initialized.

put : String -> Body -> Decoder val -> Request val

Send a PUT request to the given URL. Specify how to decode the response.

import Http
import Json.Decode exposing (list, string, succeed)
import Rails


hats : Cmd msg
hats =
    list hatDecoder
        |> Rails.put "http://example.com/hat-categories/5" revisedHatData
        |> Http.send HandleResponse

NOTE: Rails typically expects an X-CSRF-Token header for PUT requests, which this does not include. To have this header included automatically, add csrf-xhr to the page, before your Elm program gets initialized.

patch : String -> Body -> Decoder val -> Request val

Send a PATCH request to the given URL. Specify how to decode the response.

import Http
import Json.Decode exposing (list, string, succeed)
import Rails


hats : Cmd msg
hats =
    list hatDecoder
        |> Rails.patch "http://example.com/hat-categories/5" revisedHatData
        |> Http.send HandleResponse

NOTE: Rails typically expects an X-CSRF-Token header for PATCH requests, which this does not include. To have this header included automatically, add csrf-xhr to the page, before your Elm program gets initialized.

delete : String -> Body -> Decoder val -> Request val

Send a DELETE request to the given URL. Specify how to decode the response.

import Http
import Json.Decode exposing (list, string, succeed)
import Rails


hats : Cmd msg
hats =
    list hatDecoder
        |> Rails.delete "http://example.com/hat-categories/5" Http.emptyBody
        |> Http.send HandleResponse

NOTE: Rails typically expects an X-CSRF-Token header for DELETE requests, which this does not include. To have this header included automatically, add csrf-xhr to the page, before your Elm program gets initialized.

Decoder railsError
-> Result Error success
-> Result (Error railsError) success

Decode Rails-specific error information from a BadStatus response. (That is, a response whose status code is outside the 200 range.)

This is intended to be used with Http.send like so:

import Dict
import Http
import Json.Decode exposing (at, list, string)
import Json.Encode as Encode
import Rails
import Rails.Decode


requestHats : HatStyle -> Cmd Msg
requestHats style =
    let
        body =
            [ ( "style", encodeHatStyle style ) ]
                |> Encode.object
                |> Http.jsonBody


        getErrors =
            at [ "errors", "style" ] string
                |> Rails.decodeErrors
    in
    list string
        |> Rails.post url body
        |> Http.send (getErrors >> HandleResponse)
decodeRawErrors : Decoder railsError -> Error -> Error railsError

Decode Rails-specific error information from a BadStatus response. (That is, a response whose status code is outside the 200 range.)

See also, decodeErrors.

{ method : String
, headers : List Header
, url : String
, body : Body
, expect : Expect a
, timeout : Maybe Time
, withCredentials : Bool
}
-> Request a

Wraps Http.request while adding the following default headers:

  • Content-Type - "application/json"
  • Accept - "application/json, text/javascript, */*; q=0.01"
  • X-Requested-With - "XMLHttpRequest"

You can specify additional headers in the headers field of the configuration record.

import Dict
import Http
import Json.Decode exposing (list, string)
import Json.Encode as Encode
import Rails
import Rails.Decode


hatRequest : HatStyle -> Request (Result (ErrorList Field) Hat)
hatRequest style =
    let
        body =
            [ ( "style", encodeHatStyle style ) ]
                |> Encode.object
                |> Http.jsonBody
    in
    Rails.request
        { method = "POST"
        , headers = []
        , url = url
        , body = body
        , expect = Http.expectJson (list string)
        , timeout = Nothing
        , withCredentials = False
        }

NOTE: Rails typically expects an X-CSRF-Token header for requests other than GET, which this does not include. To have this header included automatically, add csrf-xhr to the page, before your Elm program gets initialized.