Class: Ippon::FormData::URLEncoded

Inherits:
Object
  • Object
show all
Defined in:
lib/ippon/form_data.rb

Overview

Represents a URL encoded (application/x-www-form-urlencoded) form.

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • str (String)

Returns:



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.

Parameters:

  • name (#to_s)

Returns:

  • (String | nil)


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.

Parameters:

  • name (#to_s)

Yields:

  • (value)

    every value

Returns:

  • self



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.

Parameters:

  • name (#to_s)

Yields:

  • if the field doesn’t exist.

Returns:

  • (String)

Raises:

  • (KeyError)

    if the field doesn’t exist and no block is given.



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.

Parameters:

  • name (#to_s)

Returns:

  • (Array<String>)


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