Class: HalClient::Form

Inherits:
Object
  • Object
show all
Defined in:
lib/hal_client/form.rb,
lib/hal_client/form/field.rb

Overview

A single [Dwolla HAL form](github.com/Dwolla/hal-forms). Instances of this class allow clients to complete and submit individual forms.

This is an incomplete implementation of the spec. Thus far it supports string and hidden fields with JSON encoding. Enhancements are requested.

Defined Under Namespace

Classes: Field

Instance Method Summary collapse

Constructor Details

#initialize(parsed_json, hal_client) ⇒ Form

Initializes a newly created form

parsed_json - a Hash create by parsing the JSON. hal_client - the ‘HalClient` with which to submit the new form.



19
20
21
22
23
24
25
# File 'lib/hal_client/form.rb', line 19

def initialize(parsed_json, hal_client)
  @hal_client = hal_client
  @target_tmpl = extract_target_tmpl(parsed_json)
  @method = extract_method(parsed_json)
  @content_type = extract_content_type(parsed_json)
  @fields = extract_fields(parsed_json)
end

Instance Method Details

#submit(answers = {}) ⇒ Object

Returns the ‘HalClient::Representation` returned from submitting the form.

answers - ‘Hash` containing the answer key to submit. Keys are

the field names; values are the values to submit.


38
39
40
41
42
43
44
# File 'lib/hal_client/form.rb', line 38

def submit(answers={})
  if :get == method
    hal_client.get(target_url(answers))
  else
    hal_client.public_send(method, target_url(answers), body(answers), "Content-Type" => content_type)
  end
end

#target_url(answers = {}) ⇒ Object

Returns the ‘Addressable::URI` to which this form is targeted



29
30
31
# File 'lib/hal_client/form.rb', line 29

def target_url(answers={})
  target_tmpl.expand(answers)
end