Class: Unirest::HttpRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/unirest/http_request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, url, headers = {}, body = nil, auth = nil) ⇒ HttpRequest

Returns a new instance of HttpRequest.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/unirest/http_request.rb', line 41

def initialize(method, url, headers = {}, body = nil, auth = nil)
  @method = method
  
  if (method == :get)
    if body.is_a?(Hash) && body.length > 0
      if url.include? "?"
        url += "&"
      else
        url += "?"
      end

      uri = Addressable::URI.new
      uri.query_values = body
      url += uri.query
    end
  else
     @body = body
  end

  unless url =~ URI.regexp
    raise "Invalid URL: " + url
  end
  
  @url = url.gsub /\s+/, '%20'
  
  @headers = {}

  if auth != nil && auth.is_a?(Hash)
    user = ""
    password = ""
    if auth[:user] != nil
      user = auth[:user]
    end
    if auth[:password] != nil
      password = auth[:password]
    end

    headers['Authorization'] = "Basic " + Base64.encode64(user + ":" + password)

  end


  # Make the header key lowercase
  headers.each_pair {|key, value| @headers[key.downcase] = value }
end

Instance Attribute Details

#authObject (readonly)

Returns the value of attribute auth.



35
36
37
# File 'lib/unirest/http_request.rb', line 35

def auth
  @auth
end

#bodyObject (readonly)

Returns the value of attribute body.



34
35
36
# File 'lib/unirest/http_request.rb', line 34

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



33
34
35
# File 'lib/unirest/http_request.rb', line 33

def headers
  @headers
end

#methodObject (readonly)

Returns the value of attribute method.



31
32
33
# File 'lib/unirest/http_request.rb', line 31

def method
  @method
end

#urlObject (readonly)

Returns the value of attribute url.



32
33
34
# File 'lib/unirest/http_request.rb', line 32

def url
  @url
end

Instance Method Details

#add_header(name, value) ⇒ Object



37
38
39
# File 'lib/unirest/http_request.rb', line 37

def add_header(name, value)
  @headers[name] = value
end