Class: RestClient
- Inherits:
-
Object
- Object
- RestClient
- Defined in:
- lib/rest-client/restfulclient.rb
Overview
Class used to perform HTTP Post and Get operations over the HTTP protocol
Instance Method Summary collapse
-
#get ⇒ HTTPResponse
Perform the HTTP GET request with the given information set within the object.
-
#getBody ⇒ Hash
Return the body that the webrequest is about the send.
-
#getHost ⇒ String
Return the URL’s HOST.
-
#getPath ⇒ String
Return the URL’s PATH (URI).
-
#getPort ⇒ Number
Return the URL’s PORT.
-
#getQuery ⇒ String
Return the URL’s Query string params.
-
#getURI ⇒ String
Return the URI of the web request.
-
#initialize(parameters) ⇒ RestClient
constructor
A new instance of RestClient.
-
#post ⇒ HTTPResponse
Perform the HTTP Post request with the given information set within the object.
-
#setBody(content) ⇒ Object
Set the URL’s Body An example of this is the following data: {“action”:“coreui_Repository”, “method”:“update” }.to_json.
-
#setQueryStringParams(params) ⇒ Object
Set the URL’s Query string params.
Constructor Details
#initialize(parameters) ⇒ RestClient
Returns a new instance of RestClient.
24 25 26 27 28 29 30 31 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 |
# File 'lib/rest-client/restfulclient.rb', line 24 def initialize(parameters) if parameters.fetch(:uri, nil).nil? raise 'uri cannot be empty' end if parameters.fetch(:headers, nil).nil? raise 'headers cannot be empty' end if parameters.fetch(:rest_type, nil).nil? raise 'rest_type cannot be empty' end if parameters.fetch(:ssl, nil).nil? raise 'ssl cannot be empty' end @uri = URI(parameters.fetch(:uri)) @http = Net::HTTP.new(@uri.host, @uri.port) if parameters.fetch(:ssl) === true @http.use_ssl = true end if parameters.fetch(:params, nil).nil? if RestTypes::POST === parameters.fetch(:rest_type) @req = Net::HTTP::Post.new(@uri.path, parameters.fetch(:headers)) elsif RestTypes::GET === parameters.fetch(:rest_type) @req = Net::HTTP::Get.new(@uri.path, parameters.fetch(:headers)) end else @uri.query = URI.encode_www_form(parameters.fetch(:params)) if RestTypes::POST === parameters.fetch(:rest_type) @req = Net::HTTP::Post.new(@uri.path + '?' + @uri.query, parameters.fetch(:headers)) elsif RestTypes::GET === parameters.fetch(:rest_type) @req = Net::HTTP::Get.new(@uri.path + '?' + @uri.query, parameters.fetch(:headers)) end end end |
Instance Method Details
#get ⇒ HTTPResponse
Perform the HTTP GET request with the given information set within the object
127 128 129 |
# File 'lib/rest-client/restfulclient.rb', line 127 def get() return @http.request(@req) end |
#getBody ⇒ Hash
Return the body that the webrequest is about the send
115 116 117 |
# File 'lib/rest-client/restfulclient.rb', line 115 def getBody() return @req.body end |
#getHost ⇒ String
Return the URL’s HOST
75 76 77 |
# File 'lib/rest-client/restfulclient.rb', line 75 def getHost() return @uri.host end |
#getPath ⇒ String
Return the URL’s PATH (URI)
87 88 89 |
# File 'lib/rest-client/restfulclient.rb', line 87 def getPath() return @uri.path end |
#getPort ⇒ Number
Return the URL’s PORT
81 82 83 |
# File 'lib/rest-client/restfulclient.rb', line 81 def getPort() return @uri.port end |
#getQuery ⇒ String
Return the URL’s Query string params
93 94 95 |
# File 'lib/rest-client/restfulclient.rb', line 93 def getQuery() return @uri.query end |
#getURI ⇒ String
Return the URI of the web request
69 70 71 |
# File 'lib/rest-client/restfulclient.rb', line 69 def getURI() return @uri.to_s end |
#post ⇒ HTTPResponse
Perform the HTTP Post request with the given information set within the object
121 122 123 |
# File 'lib/rest-client/restfulclient.rb', line 121 def post() return @http.request(@req) end |
#setBody(content) ⇒ Object
Set the URL’s Body An example of this is the following data:
"method":"update"
.to_json
109 110 111 |
# File 'lib/rest-client/restfulclient.rb', line 109 def setBody(content) @req.body = content end |
#setQueryStringParams(params) ⇒ Object
Set the URL’s Query string params
99 100 101 |
# File 'lib/rest-client/restfulclient.rb', line 99 def setQueryStringParams(params) @uri.query = URI.encode_www_form( params ) end |