NoRedInk / elm-rails / Rails

Requests

get : { url : String, expect : Expect msg } -> Platform.Cmd.Cmd msg

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

import Rails

getHats : Cmd msg
getHats =
    Rails.get
        { url = "https://example.com/hats"
        , expect = Rails.expectJson HandleGetHatsResponse hatsDecoder
        }

post : { url : String, body : Http.Body, expect : Expect msg } -> Platform.Cmd.Cmd msg

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

import Http
import Rails

createHat : Hat -> Cmd msg
createHat =
    Rails.post
        { url = "https://example.com/hats"
        , body = Http.jsonBody (encodeHat hat)
        , expect = Rails.expectJson HandleNewHatResponse hatDecoder
        }

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 : { url : String, body : Http.Body, expect : Expect msg } -> Platform.Cmd.Cmd msg

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

import Http
import Rails

updateHat : Hat -> Cmd msg
updateHat hat =
    Rails.put
        { url = "https://example.com/hats/" ++ String.fromInt hat.id
        , body = Http.jsonBody (encodeHat hat)
        , expect = Rails.expectJson HandleUpdatedHatResponse hatDecoder
        }

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 : { url : String, body : Http.Body, expect : Expect msg } -> Platform.Cmd.Cmd msg

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

import Http
import Json.Encode exposing (object, string)
import Rails

updateHatDescription : Int -> String -> Cmd msg
updateHatDescription id description =
    Rails.patch
        { url = "https://example.com/hats/" ++ String.fromInt hat.id
        , body = Http.jsonBody (object [ ( "description", string description ) ])
        , expect = Rails.expectJson HandleUpdatedHatResponse hatDecoder
        }

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 : { url : String, body : Http.Body, expect : Expect msg } -> Platform.Cmd.Cmd msg

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

import Http
import Rails

destroyHat : Hat -> Cmd msg
destroyHat =
    Rails.delete
        { url = "https://example.com/hats/" ++ String.fromInt hat.id
        , body = Http.emptyBody
        , expect = Rails.expectEmptyBody HandleDeletedHatResponse
        }

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.

request : { method : String, headers : List Http.Header, url : String, body : Http.Body, expect : Expect msg, timeout : Maybe Basics.Float, tracker : Maybe String } -> Platform.Cmd.Cmd msg

Wraps Http.request while adding the following default headers:

You can specify additional headers in the headers field of the configuration record. The delete example above would look lik this:

import Http
import Rails

destroyHat : Hat -> Cmd msg
destroyHat =
    Rails.request
        { method = "DELETE"
        , headers = []
        , url = "https://example.com/hats/" ++ String.fromInt hat.id
        , body = Http.emptyBody
        , expect = Rails.expectEmptyBody HandleDeletedHatResponse
        , timeout = Nothing
        , tracker = Nothing
        }

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

Expectations


type Expect msg

Expect that the response body will look a certain way. Similar to Http.Expect, but wrapped here so we know what headers to set in requests.


type Response error success
    = Success success
    | HttpError Http.Error
    | AppError Http.Metadata error

The kinds of responses a Rails server may return.

expectString : (Result Http.Error String -> msg) -> Expect msg

Expect Rails to return some string data. If this will be JSON, use expectJson instead!

expectEmptyBody : (Result Http.Error () -> msg) -> Expect msg

Expect Rails to return an empty body. Note that we don't actually enforce the body is empty; we just discard it. Pairs well with delete.

expectJson : (Result Http.Error success -> msg) -> Json.Decode.Decoder success -> Expect msg

Expect Rails to return JSON.

expectJsonErrors : (Response error success -> msg) -> Json.Decode.Decoder error -> Json.Decode.Decoder success -> Expect msg

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

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

createHat : Hat -> Cmd Msg
createHat hat =
    let
        errorsDecoder =
            Rails.Decode.decodeErrors (at [ "errors", "style" ] string)
    in
    Rails.post
        { url = "https://example.com/hats"
        , body = Http.jsonBody (encodeHat hat)
        , expect = Rails.expectJsonErrors HandleNewHatResponse errorsDecoder hatDecoder
        }