Class: JSONRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/foundation/JSONRequest.rb

Instance Method Summary collapse

Instance Method Details

#addQueryParameter(url, parameter, value) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/foundation/JSONRequest.rb', line 13

def addQueryParameter(url, parameter, value)
  if (parameter!=nil) && (value!=nil) then
    if url.index('?')!=nil then
      url << '&'
    else
      url << '?'
    end
    url << CGI::escape(parameter.to_s)
    url << '='
    url << CGI::escape(value.to_s)
  end
  return url
end

#delete(url, username, password) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/foundation/JSONRequest.rb', line 92

def delete(url, username, password)
  uri=URI.parse(url)
  req = Net::HTTP::Delete.new(uri.request_uri)
  req.basic_auth username, password
  response=HTTPResponse.new()
  begin
    httpresponse = Net::HTTP.new(uri.host, uri.port).start { |http| http.request(req) }
    response.setCode(httpresponse.code)
    response.setContent(httpresponse.body)
    response.setContentType(httpresponse.header['Content-Type'])
    response.setLocation(httpresponse.header['Location'])
  rescue => e
    print 'exception handled'
  end
  return response
end

#get(url, responsetype, username, password) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/foundation/JSONRequest.rb', line 27

def get(url, responsetype, username, password)
  uri=URI.parse(url)
  req = Net::HTTP::Get.new(uri.request_uri)
  if responsetype!=nil then
    req["Accept"]="application/json"
  end
  req.basic_auth username, password
  response=HTTPResponse.new()
  begin
    httpresponse = Net::HTTP.new(uri.host, uri.port).start { |http| http.request(req) }
    response.setCode(httpresponse.code)
    response.setContent(httpresponse.body)
    response.setContentType(httpresponse.header['Content-Type'])
    response.setLocation(httpresponse.header['Location'])
  rescue => e
    print 'exception handled'
  end
  return response
end

#initializedObject



9
10
11
# File 'lib/foundation/JSONRequest.rb', line 9

def initialized

end

#post(url, postdata, responsetype, username, password) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/foundation/JSONRequest.rb', line 47

def post(url, postdata, responsetype, username, password)
  uri=URI.parse(url)
  req = Net::HTTP::Post.new(uri.request_uri)
  if responsetype!=nil then
    req["Accept"]="application/json"
  end
  req.basic_auth username, password
  req.body=postdata
  response=HTTPResponse.new()
  begin
    httpresponse = Net::HTTP.new(uri.host, uri.port).start { |http| http.request(req) }
    response.setCode(httpresponse.code)
    response.setContent(httpresponse.body)
    response.setContentType(httpresponse.header['Content-Type'])
    response.setLocation(httpresponse.header['Location'])
  rescue => e
    print 'exception handled'
  end
  return response
end

#postMultipart(url, postdata, responsetype, username, password, boundary) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/foundation/JSONRequest.rb', line 68

def postMultipart(url, postdata, responsetype, username, password, boundary)
  uri=URI.parse(url)
  req = Net::HTTP::Post.new(uri.request_uri)
  if responsetype!=nil then
    req["Accept"]="application/json"
  end

  req["Content-Type"]='multipart/mixed; boundary="'+boundary+'"';

  req.basic_auth username, password
  req.body=postdata
  response=HTTPResponse.new()
  begin
    httpresponse = Net::HTTP.new(uri.host, uri.port).start { |http| http.request(req) }
    response.setCode(httpresponse.code)
    response.setContent(httpresponse.body)
    response.setContentType(httpresponse.header['Content-Type'])
    response.setLocation(httpresponse.header['Location'])
  rescue => e
    print 'exception handled'
  end
  return response
end