NoRedInk/elm-rails/4.0.3

NOTE: this package is not compatible with Elm 0.19.1

Rails

Http

type Error error
= HttpError Error
| RailsError error

The kinds of errors a Rails server may return.

get : ResponseDecoder error value -> String -> Task (Error error) value

Send a GET request to the given URL. You also specify how to decode the response.

import Json.Decode (list, string)


hats : Task (Error (List String)) (List String)
hats =
  get (decoder (list string) (succeed ())) "http://example.com/hat-categories.json"
ResponseDecoder error value
-> String
-> Body
-> Task (Error error) value

Send a POST request to the given URL. You also specify how to decode the response.

import Json.Decode (list, string) import Http

hats : Task (Error (List String)) (List String) hats = post (decoder (list string) (succeed ())) "http://example.com/hat-categories.json" Http.empty

send : String -> String -> Body -> Task RawError Response

Utility for working with Rails. Wraps Http.send, passing an Authenticity Token along with the type of request. Suitable for use with fromJson:

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


hats : HatStyle -> Task (Error (List String)) (List String)
hats style =


  let
    payload =
      Encode.object
        [ ( "style", encodeHatStyle style ) ]


    body =
      Http.string (Encode.encode 0 payload)


    success =
      list string


    failure =
      Dict.fromList [ ("style", HatStyle) ]
        |> Rails.Decode.errors
  in
    send "POST" url body
      |> fromJson (decoder success failure)
ResponseDecoder error value
-> Task RawError Response
-> Task (Error error) value

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 parsing fails, return an Http.UnexpectedPayload wrapped in HttpError * If the status code is outside the 200 range, try to parse with the given decoder.success. * If parsing fails, return an Http.BadResponse wrapped in HttpError

always : Decoder value -> ResponseDecoder value value

Returns a decoder suitable for passing to fromJson, which uses the same decoder for both success and failure responses.

decoder : Decoder value -> Decoder error -> ResponseDecoder error value

Returns a decoder suitable for passing to fromJson.

Tokens

csrfToken : Maybe 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).