NoRedInk/elm-rails/3.0.0

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 :
String
-> 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 : String -> Task (Error (List String)) (List String)
hats authToken =
    get (decoder (list string) (succeed ())) "http://example.com/hat-categories.json"
String
-> 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 : String -> Task (Error (List String)) (List String)
hats authToken =
    post (decoder (list string) (succeed ())) "http://example.com/hat-categories.json" Http.empty
send : String -> 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 : String -> HatStyle -> Task (Error (List String)) (List String)
hats authToken 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 authToken "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.