class ShafClient::Form

Public Instance Methods

[](key) click to toggle source
# File lib/shaf_client/form.rb, line 19
def [](key)
  values.fetch(key.to_sym)
end
[]=(key, value) click to toggle source
# File lib/shaf_client/form.rb, line 23
def []=(key, value)
  values.fetch(key.to_sym) # Raise KeyError unless key exist!
  values[key.to_sym] = value
end
content_type() click to toggle source
# File lib/shaf_client/form.rb, line 40
def content_type
  raise NotImplementedError
end
fields() click to toggle source
# File lib/shaf_client/form.rb, line 44
def fields
  raise NotImplementedError
end
http_method() click to toggle source
# File lib/shaf_client/form.rb, line 36
def http_method
  raise NotImplementedError
end
submit() click to toggle source
# File lib/shaf_client/form.rb, line 48
def submit
  client.send(
    http_method,
    target,
    payload: encoded_payload,
    headers: {'Content-Type' => content_type}
  )
end
target() click to toggle source
# File lib/shaf_client/form.rb, line 32
def target
  raise NotImplementedError
end
title() click to toggle source
# File lib/shaf_client/form.rb, line 28
def title
  raise NotImplementedError
end
valid?() click to toggle source
# File lib/shaf_client/form.rb, line 57
def valid?
  field_value_mapping

  fields.all? do |field|
    value = values[field.name.to_sym]
    field.valid? value
  end
end
values() click to toggle source
# File lib/shaf_client/form.rb, line 11
def values
  return @values if defined? @values

  @values = fields.each_with_object({}) do |field, values|
    values[field.name] = field.value
  end
end

Protected Instance Methods

<<(other) click to toggle source
Calls superclass method
# File lib/shaf_client/form.rb, line 68
def <<(other)
  @values = {}
  other.values.each do |key, value|
    @values[key] = value.dup
  end
  super
end

Private Instance Methods

encoded_payload() click to toggle source
# File lib/shaf_client/form.rb, line 78
def encoded_payload
  if content_type&.downcase == 'application/x-www-form-urlencoded'
    URI.encode_www_form(values)
  else
    JSON.generate(values) 
  end
end
field_names() click to toggle source
# File lib/shaf_client/form.rb, line 86
def field_names
  fields.map { |f| f.name.to_sym }
end
field_value_mapping() click to toggle source
# File lib/shaf_client/form.rb, line 90
def field_value_mapping
  field_names.each_with_object({}) do |name, mapping|
    mapping[name.to_sym] = values[name.to_sym]
  end
end