Class: Pincers::Http::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/pincers/http/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_method, _uri) ⇒ Request

Returns a new instance of Request.



6
7
8
9
10
11
# File 'lib/pincers/http/request.rb', line 6

def initialize(_method, _uri)
  @method = _method
  @uri = _uri
  @headers = {}
  @data = nil
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



4
5
6
# File 'lib/pincers/http/request.rb', line 4

def data
  @data
end

#headersObject

Returns the value of attribute headers.



4
5
6
# File 'lib/pincers/http/request.rb', line 4

def headers
  @headers
end

#methodObject

Returns the value of attribute method.



4
5
6
# File 'lib/pincers/http/request.rb', line 4

def method
  @method
end

#uriObject

Returns the value of attribute uri.



4
5
6
# File 'lib/pincers/http/request.rb', line 4

def uri
  @uri
end

Instance Method Details

#clone_for_redirect(_location, _repeat = true) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# 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_typeObject



17
18
19
20
21
22
23
24
# 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) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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) ⇒ Object



26
27
28
29
# 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

#urlObject



13
14
15
# File 'lib/pincers/http/request.rb', line 13

def url
  @uri.to_s
end