Class: Ippon::FormData::URLEncoded
- Inherits:
-
Object
- Object
- Ippon::FormData::URLEncoded
- Defined in:
- lib/ippon/form_data.rb
Overview
Represents a URL encoded (application/x-www-form-urlencoded) form.
Class Method Summary collapse
-
.parse(str) ⇒ URLEncoded
A parsed version of.
Instance Method Summary collapse
-
#[](name) ⇒ String | nil
Finds a field value; returning nil if it doesn’t exist.
-
#each_for(name) {|value| ... } ⇒ Object
private
Yields every value for a field.
-
#fetch(name) { ... } ⇒ String
Finds a field value in a given scope; yielding the block if it doesn’t exist.
-
#fetch_all(name) ⇒ Array<String>
Returns all values for a field.
-
#initialize(pairs = []) ⇒ URLEncoded
constructor
private
Creates a new instance.
Constructor Details
#initialize(pairs = []) ⇒ URLEncoded
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Creates a new instance. For now prefer parse.
73 74 75 |
# File 'lib/ippon/form_data.rb', line 73 def initialize(pairs = []) @pairs = pairs end |
Class Method Details
.parse(str) ⇒ URLEncoded
Returns a parsed version of.
66 67 68 |
# File 'lib/ippon/form_data.rb', line 66 def self.parse(str) new(URI.decode_www_form(str)) end |
Instance Method Details
#[](name) ⇒ String | nil
Finds a field value; returning nil if it doesn’t exist.
95 96 97 |
# File 'lib/ippon/form_data.rb', line 95 def [](name) fetch(name) { nil } end |
#each_for(name) {|value| ... } ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Yields every value for a field.
83 84 85 86 87 88 89 |
# File 'lib/ippon/form_data.rb', line 83 def each_for(name) name = name.to_s @pairs.each do |k, v| yield v if name == k end self end |
#fetch(name) { ... } ⇒ String
Finds a field value in a given scope; yielding the block if it doesn’t exist.
106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/ippon/form_data.rb', line 106 def fetch(name) name = name.to_s each_for(name) do |value| return value end if block_given? yield else raise KeyError, "name not found: #{name}" end end |
#fetch_all(name) ⇒ Array<String>
Returns all values for a field.
123 124 125 126 127 128 129 |
# File 'lib/ippon/form_data.rb', line 123 def fetch_all(name) result = [] each_for(name) do |value| result << value end result end |