class Pincers::Http::Request

Attributes

data[RW]
headers[RW]
method[RW]
uri[RW]

Public Class Methods

new(_method, _uri) click to toggle source
# File lib/pincers/http/request.rb, line 6
def initialize(_method, _uri)
  @method = _method
  @uri = _uri
  @headers = {}
  @data = nil
end

Public Instance Methods

clone_for_redirect(_location, _repeat = true) click to toggle source
# File lib/pincers/http/request.rb, line 53
def clone_for_redirect(_location, _repeat = true)
  clone = self.class.new(_repeat ? method : :get, _location)

  if _repeat
    clone.headers = clone.headers.clone
    clone.data = data
  end

  clone
end
native_type() click to toggle source
# File lib/pincers/http/request.rb, line 17
def native_type
  case @method
  when :get then Net::HTTP::Get
  when :post then Net::HTTP::Post
  when :put then Net::HTTP::Put
  when :delete then Net::HTTP::Delete
  else nil end
end
set_form_data(_pairs, _encoding = nil) click to toggle source
# File lib/pincers/http/request.rb, line 31
def set_form_data(_pairs, _encoding = nil)
  _pairs = Utils.hash_to_pairs(_pairs) if _pairs.is_a? Hash
  encoding = default_encoding_for(_pairs)
  encoding = _encoding if !_encoding.nil? && encoding == Utils::FORM_URLENCODED

  if method == :get
    raise EncodingNotSupported, encoding if encoding != Utils::FORM_URLENCODED
    set_query _pairs
  else
    headers['Content-Type'] = encoding

    self.data = case encoding
    when Utils::FORM_URLENCODED
      Utils.encode_urlencoded _pairs
    when Utils::FORM_MULTIPART
      Utils.encode_multipart _pairs
    else
      raise Pincers::MissingFeatureError.new "form encoding: #{_encoding}"
    end
  end
end
set_query(_pairs) click to toggle source
# File lib/pincers/http/request.rb, line 26
def set_query(_pairs)
  _pairs = Utils.hash_to_pairs(_pairs) if _pairs.is_a? Hash
  @uri.query = Utils.encode_urlencoded(_pairs)
end
url() click to toggle source
# File lib/pincers/http/request.rb, line 13
def url
  @uri.to_s
end

Private Instance Methods

default_encoding_for(_pairs) click to toggle source
# File lib/pincers/http/request.rb, line 66
def default_encoding_for(_pairs)
  has_files  = _pairs.any? { |p| p[1].is_a? IO }
  has_files ? Utils::FORM_MULTIPART : Utils::FORM_URLENCODED
end