Class: Amber::Switch::Content::FormData

Inherits:
Amber::Switch::Content show all
Includes:
Amber::Switch::ContentDelegate
Defined in:
lib/amber/switch/content/form_data.rb

Constant Summary

Constants inherited from Amber::Switch::Content

FORM_DATA_CONTENT, JSON_CONTENT, TEXT_CONTENT

Instance Attribute Summary

Attributes inherited from Amber::Switch::Content

#type

Instance Method Summary collapse

Methods inherited from Amber::Switch::Content

#body, #body=, #data

Constructor Details

#initialize(data = {}) ⇒ FormData

Returns a new instance of FormData.



4
5
6
# File 'lib/amber/switch/content/form_data.rb', line 4

def initialize(data = {})
  super Amber::Switch::Content::FORM_DATA_CONTENT, data
end

Instance Method Details

#data=(data) ⇒ Object



8
9
10
11
12
# File 'lib/amber/switch/content/form_data.rb', line 8

def data=(data)
  if data.is_a? Hash
    super
  end
end

#deserialize(data) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/amber/switch/content/form_data.rb', line 27

def deserialize(data)
  form_data = {}
  form_data_string = data.to_s
  form_data_items = form_data_string.split '&'
  form_data_items.each do |form_data_item|
    info = form_data_item.split '='
    if info.length == 2
      key = URI.encode info[0]
      value = URI.decode info[1]
      form_data[key] = value
    end
  end
  form_data
end

#serialize(data) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/amber/switch/content/form_data.rb', line 14

def serialize(data)
  form_data_string = ""
  if data.is_a? Hash
    form_data_items = []
    data.each do |key, value|
      form_data_item = URI.encode(key) + "=" + URI.encode(value)
      form_data_items.push form_data_item
    end
    form_data_string = form_data_items.join "&"
  end
  form_data_string
end