class RestBaby::Client
Sends and receives data to a restful web service
Constants
- PARAM_SEPARATOR
- PARAM_STARTER
Attributes
Response Code
The password for the user (for authentication)
The user (for authentication)
The WebService Response from the last call
Public Class Methods
Creates a new rest client
@param url [String] url of the rest service eg. myrestservice.com:80/time @param headers [Hash] default headers to use. eg. '{ "Content-Type" => "application/json"}' Can be multiple headers separated by commas inside the brackets.
# File lib/rest_baby.rb, line 37 def initialize(url, headers = {}, user = nil, password = nil) @url = url @headers = headers @user = user @password = password @verbose = (ENV['DEBUG_HTTP'] != 'false') end
Public Instance Methods
Modifies the headers by merging new headers with current headers.
@param headers [Hash] new headers to merge with current headers
# File lib/rest_baby.rb, line 57 def add_headers(headers) @headers = @headers.merge(headers) end
Basic web services Delete command
@param path [String] Path of service to send the command to @param headers [Hash] header parameters including
authorization and Content-Type
@return The response from the rest server is returned
# File lib/rest_baby.rb, line 97 def delete(headers = {}, path = '', parameters = {}) full_path = [path, URI.encode_www_form(parameters)].join(PARAM_STARTER) uri = URI.parse("#{@url}#{full_path}") request = Net::HTTP::Delete.new(uri.request_uri) request.body = nil send_request(uri, request, headers) end
Basic web services Get command
@param headers [Hash] header parameters including authorization
and Content-Type
@param path [String] Path of service to send the command to @param parameters [Hash] query string that added as part of the URL @return The response from the rest server is returned
# File lib/rest_baby.rb, line 68 def get(headers = {}, path = '', parameters = {}) full_path = [path, URI.encode_www_form(parameters)].join(PARAM_STARTER) uri = URI.parse("#{@url}#{full_path}") request = Net::HTTP::Get.new(uri.request_uri) request.body = nil send_request(uri, request, headers) end
Basic web services Post command
@param path [String] Path of service to send the command to @param body [Any type of string] Data to put in the
body such as json or xml
@param headers [Hash] header parameters including authorization
and Content-Type
@return The response from the rest server is returned
# File lib/rest_baby.rb, line 84 def post(body = nil, headers = {}, path = '') uri = URI.parse("#{@url}#{path}") request = Net::HTTP::Post.new(uri.request_uri) request.body = body send_request(uri, request, headers) end
Basic web services Put command
@param path [String] Path of service to send the command to @param body [String] data to put in the body @param headers [Hash] header parameters including authorization
and Content-Type
@return Response from the rest server is returned
# File lib/rest_baby.rb, line 112 def put(body = nil, headers = {}, path = '') uri = URI.parse("#{@url}#{path}") request = Net::HTTP::Put.new(uri.request_uri) request.body = body send_request(uri, request, headers) end
Adds user/password to the rest client
@param user [String] user that has authorization @param password [String] authorized user's password
# File lib/rest_baby.rb, line 49 def set_auth(user, password) @user = user @password = password end
Private Instance Methods
# File lib/rest_baby.rb, line 159 def body_text(type, response_body) response_body ||= '[Empty]' case type when 'application/json' pretty_json(response_body) when 'application/xml' pretty_xml(response_body) else "#{response_body}\n<" end end
# File lib/rest_baby.rb, line 153 def header_text(message, pointer) headers = '' message.each { |key, value| headers << "#{pointer} #{key} = #{value}\n" } headers end
# File lib/rest_baby.rb, line 171 def pretty_json(json) json = JSON(json) unless json.class == Hash JSON.pretty_generate(json) end
# File lib/rest_baby.rb, line 176 def pretty_xml(xml) doc = Nokogiri.XML(xml) { |config| config.default_xml.noblanks } doc.to_xml(indent: 2) end
# File lib/rest_baby.rb, line 132 def print_request(request, uri) return nil unless @verbose query = uri.query == '' ? '' : "?#{uri.query}" puts ">> REQUEST\n"\ "> URL: #{uri.scheme}://#{uri.host}:#{uri.port}#{uri.path}#{query}\n"\ "> Headers:\n"\ "#{header_text(request, '> ')}\n"\ "> BODY =\n"\ "#{body_text(request['Content-Type'], request.body)}" end
# File lib/rest_baby.rb, line 143 def print_response(response) return nil unless @verbose puts "<< RESPONSE\n"\ "< CODE = #{response.code}\n"\ "< MESSAGE = #{response.message}\n"\ "#{header_text(response, ' <')}\n"\ "< BODY =\n"\ "#{body_text(response['Content-Type'], response.body)}" end
# File lib/rest_baby.rb, line 121 def send_request(uri, request, headers) @headers.merge(headers).each { |key, value| request[key] = value } request.basic_auth(@user, @password) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true if uri.scheme == 'https' print_request(request, uri) @wsresponse = http.request(request) print_response(@wsresponse) @wsresponse end