Class: Wiris::Http

Inherits:
Object
  • Object
show all
Defined in:
lib/src-generic/Http.rb

Constant Summary collapse

@@proxy =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Http

Returns a new instance of Http.



26
27
28
29
30
# File 'lib/src-generic/Http.rb', line 26

def initialize(url)
  @url = url
  @headers = Hash.new()
  @params = Hash.new()
end

Instance Attribute Details

#urlObject

The server myURL.



10
11
12
# File 'lib/src-generic/Http.rb', line 10

def url
  @url
end

Class Method Details

.proxyObject



22
23
24
# File 'lib/src-generic/Http.rb', line 22

def self.proxy
  @@proxy
end

.proxy=(proxy) ⇒ Object



18
19
20
# File 'lib/src-generic/Http.rb', line 18

def self.proxy=(proxy)
  @@proxy = proxy
end

Instance Method Details

#formEncode(params) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/src-generic/Http.rb', line 69

def formEncode(params)
  i = params.keys()
  sb = StringBuf.new()
  first = true
  while (i.hasNext())
     if(!first)
       sb.add("&")
     else
       first = false
     end
    key = i.next()
    sb.add(StringTools.urlEncode(key))
    sb.add("=")
    sb.add(StringTools.urlEncode(params.get(key)))
  end
  return sb.toString()
end

#onData(data) ⇒ Object



104
105
# File 'lib/src-generic/Http.rb', line 104

def onData(data)
end

#onError(msg) ⇒ Object



107
108
# File 'lib/src-generic/Http.rb', line 107

def onError(msg)
end

#request(post) ⇒ Object



32
33
34
35
36
37
38
39
40
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
# File 'lib/src-generic/Http.rb', line 32

def request(post)
  serializedParams = formEncode(@params)
  uri = URI.parse(@url)
  if !@@proxy.nil? && !@@proxy.host.nil? && @@proxy.host != ""
    host = @@proxy.host
    port = @@proxy.port
    auth = @@proxy.auth
    unless auth.nil?
      user = auth.user
      pass = auth.pass
    end
    @connection = Net::HTTP.new(uri.host, uri.port, host, port)
  else
    @connection = Net::HTTP.new(uri.host, uri.port)
  end

  if !post
    if (serializedParams != "")
        uri.query = serializedParams
    end
    request = Net::HTTP::Get.new(uri.request_uri)
  else
    request = Net::HTTP::Post.new(uri.request_uri)
    request.body = @postData != nil ? @postData : serializedParams
  end

  keys = @headers.keys()

  while keys.hasNext()
    key = keys.next()
    request.add_field(key, @headers.get(key))
  end

  response = @connection.request(request)
  onData(response.body)
end

#setHeader(header, value) ⇒ Object



88
89
90
# File 'lib/src-generic/Http.rb', line 88

def setHeader(header, value)
  @headers.set(header,value)
end

#setParameter(param, value) ⇒ Object

Sets a parameter to be sent in the request. Multiple parameters can be set

by making multiple setParameter calls.


96
97
98
# File 'lib/src-generic/Http.rb', line 96

def setParameter(param, value)
  @params.set(param,value)
end

#setPostData(data) ⇒ Object



100
101
102
# File 'lib/src-generic/Http.rb', line 100

def setPostData(data)
   @postData = data
end