NoRedInk/elm-rails/6.1.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 Json.Decode exposing (list, string, succeed)
import Http
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 Json.Decode exposing (list, string, succeed)
import Http
import Rails




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

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

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




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

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

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




hats : Cmd msg
hats =
    list hatDecoder
        |> Rails.delete "http://example.com/hat-categories/5" Http.emptyBody
        |> Http.send HandleResponse
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 Json.Decode exposing (list, string, at)
import Json.Encode as Encode
import Http
import Rails.Decode
import Rails




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:

  • X-CSRF-Token - set to csrfToken if it's an Ok and this request isn't a GET
  • 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 Json.Decode exposing (list, string)
import Json.Encode as Encode
import Http
import Rails.Decode
import Rails




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
            }

Customizing

csrfToken : Result String String

If there was a <meta name="csrf-token"> tag in the page's <head> when elm-rails loaded, returns the value its content attribute had at that time.

Rails expects this value in the `X-CSRF-Token` header for non-`GET` requests as
a [CSRF countermeasure](http://guides.rubyonrails.org/security.html#csrf-countermeasures).