Class: RestClient

Inherits:
Object
  • Object
show all
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

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

#getHTTPResponse

Perform the HTTP GET request with the given information set within the object

Returns:

  • Object containing all of the response information



127
128
129
# File 'lib/rest-client/restfulclient.rb', line 127

def get()
    return @http.request(@req)
end

#getBodyHash

Return the body that the webrequest is about the send

Returns:

  • JSON body of the web request



115
116
117
# File 'lib/rest-client/restfulclient.rb', line 115

def getBody()
    return @req.body
end

#getHostString

Return the URL’s HOST

Returns:

  • the URL’s HOST



75
76
77
# File 'lib/rest-client/restfulclient.rb', line 75

def getHost()
    return @uri.host
end

#getPathString

Return the URL’s PATH (URI)

Returns:

  • the URL’s URI



87
88
89
# File 'lib/rest-client/restfulclient.rb', line 87

def getPath()
    return @uri.path
end

#getPortNumber

Return the URL’s PORT

Returns:

  • the URL’s PORT



81
82
83
# File 'lib/rest-client/restfulclient.rb', line 81

def getPort()
    return @uri.port
end

#getQueryString

Return the URL’s Query string params

Returns:

  • the URL’s query string params



93
94
95
# File 'lib/rest-client/restfulclient.rb', line 93

def getQuery()
    return @uri.query
end

#getURIString

Return the URI of the web request

Returns:

  • URL



69
70
71
# File 'lib/rest-client/restfulclient.rb', line 69

def getURI()
    return @uri.to_s
end

#postHTTPResponse

Perform the HTTP Post request with the given information set within the object

Returns:

  • Object containing all of the response information



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

Parameters:

  • The body of the web request.



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

Parameters:

  • The params for the URL



99
100
101
# File 'lib/rest-client/restfulclient.rb', line 99

def setQueryStringParams(params)
    @uri.query = URI.encode_www_form( params )
end