NoRedInk/elm-rails/5.0.0

NOTE: this package is not compatible with Elm 0.19.1

Rails

Requests

type Error error
= HttpError Error
| RailsError error

The kinds of errors a Rails server may return.

get :
String
-> ResponseDecoder error success
-> Request (Result error success)

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 =
    Rails.decoder (list string) (succeed ())
        |> Rails.get "http://example.com/hat-categories.json"
        |> Rails.send HandleGetHatsResponse
String
-> Body
-> ResponseDecoder error success
-> Request (Result error success)

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 =
    Rails.decoder (list string) (succeed ())
        |> Rails.post "http://example.com/hat-categories/new" Http.emptyBody
        |> Rails.send HandleResponse
(Result (Error error) success -> msg)
-> Request (Result error success)
-> Cmd msg

Send an HTTP request to Rails. This uses request under the hood, which means that a CSRF token will automatically be passed, among other things. The Error handler uses a Rails.Error, which includes more information than the standard Http.Error.

import Dict
import Json.Decode exposing (list, string)
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


        success =
            list string


        failure =
            Dict.fromList [ ( "style", HatStyle ) ]
                |> Rails.Decode.errors
    in
        Rails.decoder success failure
            |> Rails.post url body
            |> Rails.send HandleResponse
{ 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"

    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

          success =
              list string
    
    
          failure =
              Dict.fromList [ ( "style", HatStyle ) ]
                  |> Rails.Decode.errors
      in
          Rails.request
              { method = "POST"
              , headers = []
              , url = url
              , body = body
              , expect = expectRailsJson (Rails.decoder success failure)
              , timeout = Nothing
              , withCredentials = False
              }

Decoding

decoder : Decoder success -> Decoder error -> ResponseDecoder error success

Returns a ResponseDecoder.

always : Decoder success -> ResponseDecoder success success

Returns a ResponseDecoder which uses the same decoder for both success and failures.

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).
expectRailsJson : ResponseDecoder error success -> Expect (Result error success)

Think Http.fromJson, but with additional effort to parse a non-20x response as JSON.

* If the status code is in the 200 range, try to parse with the given decoder.success. * If that succeeds, the result is Ok with the result. * If the status code is outside the 200 range, try to parse with the given decoder.failure. * If that succeeds, the result is Err with the result. * If either parsing fails, the request as a whole fails.