Class: RestBaby::Client

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

Overview

Sends and receives data to a restful web service

Instance Method Summary (collapse)

Constructor Details

- (Client) initialize(url, headers = {})

Creates a new rest client

  • url = url of the rest service

eg. myrestservice.com:80/time

  • headers = default headers to use.

eg. '{ "Content-Type" => "application/json"}' can be multiple headers



27
28
29
30
31
32
# File 'lib/rest_baby.rb', line 27

def initialize(url, headers = {})
	@uri = URI.parse(url)
	@headers = headers
	@user = nil
	@password = nil
end

Instance Method Details

- (Object) delete(headers = {})

Basic web services Delete command

  • url = url to send the command to

  • headers = header parameters including authorization and Content-Type

The response from the rest server is returned



79
80
81
# File 'lib/rest_baby.rb', line 79

def delete(headers = {})
	return request(@uri, Net::HTTP::Delete.new(@uri.request_uri), headers)
end

- (Object) get(headers = {})

Basic web services Get command

  • url = url to send the command to

  • headers = header parameters including authorization and Content-Type

The response from the rest server is returned



58
59
60
# File 'lib/rest_baby.rb', line 58

def get(headers = {})
	return request(@uri, Net::HTTP::Get.new(@uri.request_uri), headers)
end

- (Object) get_code



116
117
118
# File 'lib/rest_baby.rb', line 116

def get_code
	@wsresponse.code
end

- (Object) post(body = nil, headers = {})

Basic web services Post command

  • url = url to send the command to

  • body = data to put in the body

  • headers = header parameters including authorization and Content-Type

The response from the rest server is returned



69
70
71
# File 'lib/rest_baby.rb', line 69

def post(body = nil, headers = {})
	return request(@uri, Net::HTTP::Post.new(@uri.request_uri), body, headers)
end

Pretty print the web services last response

The nice looking output is returned as a string



124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/rest_baby.rb', line 124

def print_last_response
	output = "CODE = #{@wsresponse.code}\n"
	output << "MESSAGE = #{@wsresponse.message}\n"
	@wsresponse.each { |key, value| output << "#{key} = #{value}\n"}
	begin
		output << "BODY = "
	  output << "#{@wsresponse.body}" 
	rescue
		output << "[Empty]"
	end
	return output
end

- (Object) put(body = nil, headers = {})

Basic web services Put command

  • url = url to send the command to

  • body = data to put in the body

  • headers = header parameters including authorization and Content-Type

The response from the rest server is returned



90
91
92
# File 'lib/rest_baby.rb', line 90

def put(body = nil, headers = {})
	return request(@uri, Net::HTTP::Put.new(@uri.request_uri), body, headers)
end

- (Object) request(uri, request, body = nil, headers)

Sending the web services command

  • url = url to send the command to

  • req = command to send as Net::HTTP:<command> class

  • body = data to put in the body

  • headers = header parameters including authorization and Content-Type

The response from the rest server is returned



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/rest_baby.rb', line 102

def request(uri, request, body = nil, headers)
	@headers.merge(headers).each { |key, value| request[key] = value }
	request.body = body unless body.nil?
	http = Net::HTTP.new(@uri.host, @uri.port)
	http.use_ssl = true if @uri.scheme == 'https'
	begin
		@wsresponse = http.request(request)
		# puts print_last_response
		return @wsresponse
	rescue Timeout::Error => e 
		raise e.message
	end
end

- (Object) set_auth(user, password)

Modifies the authentication for the rest client

  • user = authorized username

  • password = password for the user



47
48
49
50
# File 'lib/rest_baby.rb', line 47

def set_auth(user, password)
	new_url = "#{@uri.scheme}://#{user}:#{password}@#{@uri.host}:#{@uri.port}#{@uri.path}"
	@uri = URI.parse(new_url)
end

- (Object) set_headers(headers)

Modifies the headers by merging new headers with current headers.

  • headers = new headers to merge with current headers



38
39
40
# File 'lib/rest_baby.rb', line 38

def set_headers(headers)
	@headers = @headers.merge(headers)
end